![數(shù)據(jù)庫 觸發(fā)器(超贊)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/e8725b70-d410-43d3-ac84-734e4ad3d495/e8725b70-d410-43d3-ac84-734e4ad3d4951.gif)
![數(shù)據(jù)庫 觸發(fā)器(超贊)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/e8725b70-d410-43d3-ac84-734e4ad3d495/e8725b70-d410-43d3-ac84-734e4ad3d4952.gif)
![數(shù)據(jù)庫 觸發(fā)器(超贊)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/e8725b70-d410-43d3-ac84-734e4ad3d495/e8725b70-d410-43d3-ac84-734e4ad3d4953.gif)
![數(shù)據(jù)庫 觸發(fā)器(超贊)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/e8725b70-d410-43d3-ac84-734e4ad3d495/e8725b70-d410-43d3-ac84-734e4ad3d4954.gif)
![數(shù)據(jù)庫 觸發(fā)器(超贊)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/e8725b70-d410-43d3-ac84-734e4ad3d495/e8725b70-d410-43d3-ac84-734e4ad3d4955.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、數(shù)據(jù)庫觸發(fā)器案例一、課堂演示案例例一:創(chuàng)建一個簡單的insert觸發(fā)器先創(chuàng)建一個數(shù)據(jù)庫備用create database sampledbgouse sampledbgo在新創(chuàng)建的庫中創(chuàng)建一個表備用create table aa( a int, b int)go在新創(chuàng)建的表上創(chuàng)建一個insert觸發(fā)器use sampledbgoif exists(select name from sysobjects where name ='tr_intoa' and type='tr')drop trigger tr_intoagocreate trigger tr_int
2、oa on aafor insertasprint 'success inserted one row!'查看這個觸發(fā)器的定義文本sp_helptext checkpubdate查看這個觸發(fā)器的信息sp_help checkpubdate驗證這個觸發(fā)器的工作情況insert into aa values (1,2)-例二:創(chuàng)建一個觸發(fā)器監(jiān)視insert操作,若插入的記錄中版權(quán)費超過30,則提示用戶,并回滾此操作。use pubsgoif exists(select name from sysobjects where name ='CheckRoyalty' a
3、nd type='tr')drop trigger CheckRoyaltygocreate trigger checkroyaltyon royschedfor insert asif (select royalty from inserted) > 30beginprint 'royaltytrigger:版權(quán)費不能超過 30' print '請將版權(quán)費修改為小于 30 的值' rollback transactionendinsert into roysched values ('BU1032',2,5,90)sele
4、ct * from roysched where title_id='BU1032'-例三:創(chuàng)建一個觸發(fā)器監(jiān)視insert操作,若插入的記錄中出版日期小于當(dāng)前日期,則提示用戶,并回滾此操作。use pubsgoif exists(select name from sysobjects where name ='checkpubdate' and type='tr')drop trigger checkpubdategocreate trigger checkpubdateon titlesfor insert as if (select pubd
5、ate from inserted) < getdate()begin select * from inserted -查看內(nèi)存表中的數(shù)據(jù) print '出版日期小于當(dāng)前日期' rollback transactionend觸發(fā)器示例測試insert into titles(title_id,title,type,pubdate)values('SW0001','test book','business','1990-1-1')select * from inserted-例四:列級update觸發(fā)器示例us
6、e pubsgoif exists(select name from sysobjects where name ='NoUpdatePayterms' and type='tr')drop trigger NoUpdatePaytermsgoCREATE TRIGGER NoUpdatePaytermsON sales FOR UPDATE ASIF UPDATE (payterms)BEGIN PRINT '不能修改訂單的付費條款'ROLLBACK TRANSACTIONEND測試觸發(fā)器的工作情況update sales set qty=8
7、where stor_id='6380' and ord_num='6871' and title_id='BU1032'update sales set payterms='aa' where stor_id='6380' and ord_num='6871' and title_id='BU1032'-例五:表級update觸發(fā)器實例use pubsgoif exists(select name from sysobjects where name ='NoUpdateD
8、iscount' and type='tr')drop trigger NoUpdateDiscountgocreate trigger NoUpdateDiscounton discounts for update asif (select discount from inserted) > 12begin select * from inserted -查看內(nèi)存表中的數(shù)據(jù) select * from deleted -查看內(nèi)存表中的數(shù)據(jù) print '不能指定大于 12% 的折扣' rollback transaction end表級 UPDA
9、TE 觸發(fā)器測試update discounts set discount = 20 where stor_id = '8042'-例六:列級update 觸發(fā)器示例use northwindgo建立登記修改人帳號的表create table who_change( change_date datetime, change_column varchar(50), who varchar(50)go建立觸發(fā)器use northwindgoif exists(select name from sysobjects where name ='tr_orderdetail_in
10、supd' and type='tr')drop trigger tr_orderdetail_insupdgocreate trigger tr_orderdetail_insupdonorder detailsfor updateasif update (unitprice)begin insert who_change values (getdate(),'unitprice updated',user_name()endelse if update (Quantity) begin insert who_change values(getdate
11、(),'quantity updated',user_name() endelse if update(discount)begin insert who_change values (getdate(),'discount updated',user_name()endgo測試觸發(fā)器的工作情況update order details set unitprice=2 where orderid=10248 and productid=1update order details set Quantity=4 where orderid=10248 and prod
12、uctid=1update order details set discount=0 where orderid=10248 and productid=1-例七:觸發(fā)器只能在當(dāng)前數(shù)據(jù)庫中創(chuàng)建。 但是,觸發(fā)器可以引用其他數(shù)據(jù)庫中的對象。(示例)use sampledbgo創(chuàng)建表test備用create table test( aa int, bb int)go向test表中插入一些數(shù)據(jù)備用insert into test values (1001,0)insert into test values (1002,0)insert into test values (1003,0)創(chuàng)建另一個庫備用
13、create database testdbgouse testdbgo在庫testdb中再創(chuàng)建一個表備用create table test_11( aa int, bb int)go在testdb庫中的表test_11上創(chuàng)建一個insert觸發(fā)器use testdbgoif exists(select name from sysobjects where name ='tri_test' and type='tr')drop trigger tri_testgocreate trigger tri_test on test_11for insert as se
14、t bb=bb+(select bb from inserted) where aa= (select aa from inserted)測試觸發(fā)器的工作情況insert into test_11 values (1002,2)insert into test_11 values (1001,1)-例八:DELETE 觸發(fā)器示例use testdbgoif exists(select name from sysobjects where name ='NoDelete9901' and type='tr')drop trigger NoDelete9901goc
15、reate trigger NoDelete9901on pub_info for delete ASif (select pub_id from deleted) = '9901'begin print '不能刪除出版商 9901 的詳細(xì)信息' rollback transaction endDELETE 觸發(fā)器示例測試delete pub_info where pub_id = '9901'-例九:視圖上的 INSTEAD OF 觸發(fā)器示例use pubsgoselect * into bak_employee from employeese
16、lect * into bak_publishers from publisherscreate view Emp_pubasselect emp_id, lname, job_id, pub_name from bak_employee e, bak_publishers pwhere e.pub_id = p.pub_idcreate trigger del_empon Emp_pubinstead of deleteas select * from deleted -查看內(nèi)存表中的數(shù)據(jù) delete bak_publishers where emp_id in (select emp_i
17、d from deleted)視圖上的 INSTEAD OF 觸發(fā)器示例測試delete Emp_pub-例十:表上的INSTEAD OF 觸發(fā)器示例use pubsgoif exists(select name from sysobjects where name ='tri_deltitle' and type='tr')drop trigger tri_deltitlegocreate trigger tri_deltitle on titlesinstead of deleteasprint '不允許刪除!'delete from tit
18、les where title_id='BU1032'-例十一:禁用觸發(fā)器嵌套exec sp_configure 'nested trigger', 0例十二:啟用觸發(fā)器嵌套exec sp_configure 'nested trigger', 1-例十三:觸發(fā)器嵌套示例use sampledbgo建立觸發(fā)器create table testa( a_id char(1), a_name char(2)insert into testa values('1','1')insert into testa values
19、('2','2')insert into testa values('3','3')create table testb( b_id char(1), b_name char(2)insert into testb values('1','1')insert into testb values('2','2')insert into testb values('3','3')create table testc( c_id char(1
20、), c_name char(2)insert into testc values('1','1')insert into testc values('2','2')insert into testc values('3','3')觸發(fā)器嵌套示例(1)create trigger del_testaon testainstead of deleteas delete testb where b_id in (select a_id from deleted)create trigger del_te
21、stbon testbinstead of deleteas delete testc where c_id in (select b_id from deleted)觸發(fā)器嵌套示例測試(1)delete testa where a_id = '1'- drop trigger del_testa2- drop trigger del_testb2-觸發(fā)器嵌套示例(2)create trigger del_testa2on testafor deleteas delete testb where b_id in (select a_id from deleted)create
22、trigger del_testb2on testbfor delete as delete testc where c_id in (select b_id from deleted)觸發(fā)器嵌套示例測試(1)delete testa where a_id = '1'-例十四:觸發(fā)器綜合應(yīng)用創(chuàng)建觸發(fā)器use northwindif exists(select name from sysobjects where name ='tr_product_update' and type='tr')drop trigger tr_product_upda
23、tegouse northwindgocreate trigger tr_product_update on productsfor updateasdeclare msg varchar(100)select msg = str(rowcount)+'employees updated by this statement'print msgreturngo管理觸發(fā)器use northwindgosp_helptrigger products,deleteinerted和deleted表實現(xiàn)級聯(lián)修改多數(shù)據(jù)表的觸發(fā)器use northwindgocreate trigger tr_suppliers_delon suppliersfor deleteasif rowcount
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度金融科技股權(quán)融資與風(fēng)險控制合同范本
- 2025年中國北京葡萄酒行業(yè)市場運行現(xiàn)狀及未來發(fā)展預(yù)測報告
- 2025年角接成套節(jié)流裝置項目投資可行性研究分析報告
- 2025年度教育機(jī)構(gòu)融資抵押合同模板
- 2025年洛北春磁化項目投資可行性研究分析報告
- 民事強(qiáng)制執(zhí)行申請書范本
- 中國不銹鋼冷軋帶項目投資可行性研究報告
- 2025年存折打印機(jī)項目可行性研究報告
- 2025年冷凍食品冷鏈物流綠色物流技術(shù)應(yīng)用合同
- 影片行業(yè)市場發(fā)展監(jiān)測及投資戰(zhàn)略規(guī)劃研究報告
- 《消防機(jī)器人相關(guān)技術(shù)研究》
- 2024年考研政治真題及答案
- 【直播薪資考核】短視頻直播電商部門崗位職責(zé)及績效考核指標(biāo)管理實施辦法-市場營銷策劃-直播公司團(tuán)隊管理
- 項目設(shè)計報告范文高中
- 《千年古村上甘棠》課件
- 部編版小學(xué)語文二年級下冊電子課文《小馬過河》
- 《醫(yī)療機(jī)構(gòu)工作人員廉潔從業(yè)九項準(zhǔn)則》專題解讀
- 愛車講堂 課件
- 成立商會的可行性報告5則范文
- 市場監(jiān)督管理局反電信網(wǎng)絡(luò)詐騙工作總結(jié)
- 2024-2030年中國免疫細(xì)胞存儲行業(yè)發(fā)展模式及投資戰(zhàn)略分析報告
評論
0/150
提交評論