SQL Server 觸發(fā)器詳情
1、什么是觸發(fā)器
觸發(fā)器對表進(jìn)行插入、更新、刪除的時候會自動執(zhí)行的特殊存儲過程。觸發(fā)器一般用在check約束更加復(fù)雜的約束上面。觸發(fā)器和普通的存儲過程的區(qū)別是:觸發(fā)器是當(dāng)對某一個表進(jìn)行操作。諸如:update、insert、delete這些操作的時候,系統(tǒng)會自動調(diào)用執(zhí)行該表上對應(yīng)的觸發(fā)器。SQL Server 2005中觸發(fā)器可以分為兩類:DML觸發(fā)器和DDL觸發(fā)器,其中DDL觸發(fā)器它們會影響多種數(shù)據(jù)定義語言語句而激發(fā),這些語句有create、alter、drop語句。
2、 DML觸發(fā)器分為
2.1 after觸發(fā)器(之后觸發(fā))
a、 insert觸發(fā)器
b、 update觸發(fā)器
c、 delete觸發(fā)器
2.2 instead of 觸發(fā)器 (之前觸發(fā))
其中after觸發(fā)器要求只有執(zhí)行某一操作insert、update、delete之后觸發(fā)器才被觸發(fā),且只能定義在表上。而instead of觸發(fā)器表示并不執(zhí)行其定義的操作(insert、update、delete)而僅是執(zhí)行觸發(fā)器本身。既可以在表上定義instead of觸發(fā)器,也可以在視圖上定義。
觸發(fā)器有兩個特殊的表:插入表(instered表)和刪除表(deleted表)。這兩張是邏輯表也是虛表。有系統(tǒng)在內(nèi)存中創(chuàng)建者兩張表,不會存儲在數(shù)據(jù)庫中。而且兩張表的都是只讀的,只能讀取數(shù)據(jù)而不能修改數(shù)據(jù)。這兩張表的結(jié)果總是與被改觸發(fā)器應(yīng)用的表的結(jié)構(gòu)相同。當(dāng)觸發(fā)器完成工作后,這兩張表就會被刪除。Inserted表的數(shù)據(jù)是插入或是修改后的數(shù)據(jù),而deleted表的數(shù)據(jù)是更新前的或是刪除的數(shù)據(jù)。
|
對表的操作 |
Inserted邏輯表 |
Deleted邏輯表 |
|
增加記錄(insert) |
存放增加的記錄 |
無 |
|
刪除記錄(delete) |
無 |
存放被刪除的記錄 |
|
修改記錄(update) |
存放更新后的記錄 |
存放更新前的記錄 |
Update數(shù)據(jù)的時候就是先刪除表記錄,然后增加一條記錄。這樣在inserted和deleted表就都有update后的數(shù)據(jù)記錄了。注意的是:觸發(fā)器本身就是一個事務(wù),所以在觸發(fā)器里面可以對修改數(shù)據(jù)進(jìn)行一些特殊的檢查。如果不滿足可以利用事務(wù)回滾,撤銷操作。
3、 創(chuàng)建觸發(fā)器
語法
create trigger tgr_name
on table_name
with encrypion –加密觸發(fā)器
for update...
as
Transact-SQL
3.1 創(chuàng)建insert類型觸發(fā)器
--創(chuàng)建insert插入類型觸發(fā)器
if (object_id('tgr_classes_insert', 'tr') is not null)
drop trigger tgr_classes_insert
go
create trigger tgr_classes_insert
on classes
for insert --插入觸發(fā)
as
--定義變量
declare @id int, @name varchar(20), @temp int;
--在inserted表中查詢已經(jīng)插入記錄信息
select @id = id, @name = name from inserted;
set @name = @name + convert(varchar, @id);
set @temp = @id / 2;
insert into student values(@name, 18 + @id, @temp, @id);
print '添加學(xué)生成功!';
go
--插入數(shù)據(jù)
insert into classes values('5班', getDate());
--查詢數(shù)據(jù)
select * from classes;
select * from student order by id;
insert觸發(fā)器,會在inserted表中添加一條剛插入的記錄。
3.2 創(chuàng)建delete類型觸發(fā)器
--delete刪除類型觸發(fā)器
if (object_id('tgr_classes_delete', 'TR') is not null)
drop trigger tgr_classes_delete
go
create trigger tgr_classes_delete
on classes
for delete --刪除觸發(fā)
as
print '備份數(shù)據(jù)中……';
if (object_id('classesBackup', 'U') is not null)
--存在classesBackup,直接插入數(shù)據(jù)
insert into classesBackup select name, createDate from deleted;
else
--不存在classesBackup創(chuàng)建再插入
select * into classesBackup from deleted;
print '備份數(shù)據(jù)成功!';
go
--
--不顯示影響行數(shù)
--set nocount on;
delete classes where name = '5班';
--查詢數(shù)據(jù)
select * from classes;
select * from classesBackup;
delete觸發(fā)器會在刪除數(shù)據(jù)的時候,將剛才刪除的數(shù)據(jù)保存在deleted表中。
3.3 創(chuàng)建update類型觸發(fā)器
--update更新類型觸發(fā)器
if (object_id('tgr_classes_update', 'TR') is not null)
drop trigger tgr_classes_update
go
create trigger tgr_classes_update
on classes
for update
as
declare @oldName varchar(20), @newName varchar(20);
--更新前的數(shù)據(jù)
select @oldName = name from deleted;
if (exists (select * from student where name like '%'+ @oldName + '%'))
begin
--更新后的數(shù)據(jù)
select @newName = name from inserted;
update student set name = replace(name, @oldName, @newName) where name like '%'+ @oldName + '%';
print '級聯(lián)修改數(shù)據(jù)成功!';
end
else
print '無需修改student表!';
go
--查詢數(shù)據(jù)
select * from student order by id;
select * from classes;
update classes set name = '五班' where name = '5班';
update觸發(fā)器會在更新數(shù)據(jù)后,將更新前的數(shù)據(jù)保存在deleted表中,更新后的數(shù)據(jù)保存在inserted表中。
3.4 update更新列級觸發(fā)器
if (object_id('tgr_classes_update_column', 'TR') is not null)
drop trigger tgr_classes_update_column
go
create trigger tgr_classes_update_column
on classes
for update
as
--列級觸發(fā)器:是否更新了班級創(chuàng)建時間
if (update(createDate))
begin
raisError('系統(tǒng)提示:班級創(chuàng)建時間不能修改!', 16, 11);
rollback tran;
end
go
--測試
select * from student order by id;
select * from classes;
update classes set createDate = getDate() where id = 3;
update classes set name = '四班' where id = 7;
更新列級觸發(fā)器可以用update是否判斷更新列記錄;
4、instead of類型觸發(fā)器
instead of觸發(fā)器表示并不執(zhí)行其定義的操作(insert、update、delete)而僅是執(zhí)行觸發(fā)器本身的內(nèi)容。
創(chuàng)建語法:
create trigger tgr_name
on table_name
with encryption
instead of update...
as
T-SQL
4.1 創(chuàng)建instead of觸發(fā)器
if (object_id('tgr_classes_inteadOf', 'TR') is not null)
drop trigger tgr_classes_inteadOf
go
create trigger tgr_classes_inteadOf
on classes
instead of delete/*, update, insert*/
as
declare @id int, @name varchar(20);
--查詢被刪除的信息,病賦值
select @id = id, @name = name from deleted;
print 'id: ' + convert(varchar, @id) + ', name: ' + @name;
--先刪除student的信息
delete student where cid = @id;
--再刪除classes的信息
delete classes where id = @id;
print '刪除[ id: ' + convert(varchar, @id) + ', name: ' + @name + ' ] 的信息成功!';
go
--test
select * from student order by id;
select * from classes;
delete classes where id = 7;
4.2 顯示自定義消息raiserror
if (object_id('tgr_message', 'TR') is not null)
drop trigger tgr_message
go
create trigger tgr_message
on student
after insert, update
as raisError('tgr_message觸發(fā)器被觸發(fā)', 16, 10);
go
--test
insert into student values('lily', 22, 1, 7);
update student set sex = 0 where name = 'lucy';
select * from student order by id;
4.3 修改觸發(fā)器
alter trigger tgr_message
on student
after delete
as raisError('tgr_message觸發(fā)器被觸發(fā)', 16, 10);
go
--test
delete from student where name = 'lucy';
4.4 啟用、禁用觸發(fā)器
--禁用觸發(fā)器 disable trigger tgr_message on student; --啟用觸發(fā)器 enable trigger tgr_message on student;
4.5 查詢創(chuàng)建的觸發(fā)器信息
--查詢已存在的觸發(fā)器 select * from sys.triggers; select * from sys.objects where type = 'TR'; --查看觸發(fā)器觸發(fā)事件 select te.* from sys.trigger_events te join sys.triggers t on t.object_id = te.object_id where t.parent_class = 0 and t.name = 'tgr_valid_data'; --查看創(chuàng)建觸發(fā)器語句 exec sp_helptext 'tgr_message';
4.6 示例,驗(yàn)證插入數(shù)據(jù)
if ((object_id('tgr_valid_data', 'TR') is not null))
drop trigger tgr_valid_data
go
create trigger tgr_valid_data
on student
after insert
as
declare @age int,
@name varchar(20);
select @name = s.name, @age = s.age from inserted s;
if (@age < 18)
begin
raisError('插入新數(shù)據(jù)的age有問題', 16, 1);
rollback tran;
end
go
--test
insert into student values('forest', 2, 0, 7);
insert into student values('forest', 22, 0, 7);
select * from student order by id;
4.7 示例,操作日志
if (object_id('log', 'U') is not null)
drop table log
go
create table log(
id int identity(1, 1) primary key,
action varchar(20),
createDate datetime default getDate()
)
go
if (exists (select * from sys.objects where name = 'tgr_student_log'))
drop trigger tgr_student_log
go
create trigger tgr_student_log
on student
after insert, update, delete
as
if ((exists (select 1 from inserted)) and (exists (select 1 from deleted)))
begin
insert into log(action) values('updated');
end
else if (exists (select 1 from inserted) and not exists (select 1 from deleted))
begin
insert into log(action) values('inserted');
end
else if (not exists (select 1 from inserted) and exists (select 1 from deleted))
begin
insert into log(action) values('deleted');
end
go
--test
insert into student values('king', 22, 1, 7);
update student set sex = 0 where name = 'king';
delete student where name = 'king';
select * from log;
select * from student order by id;
到此這篇關(guān)于SQL Server 觸發(fā)器詳情的文章就介紹到這了,更多相關(guān)SQL Server 觸發(fā)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
sql server使用公用表表達(dá)式CTE通過遞歸方式編寫通用函數(shù)自動生成連續(xù)數(shù)字和日期
CTE是在內(nèi)存中準(zhǔn)備好數(shù)據(jù),而不是每次一條往返服務(wù)器和客戶端一次。如果需要再插入到臨時表的話就是全部數(shù)據(jù)一次性插入。 這篇文章主要介紹了sql server使用公用表表達(dá)式CTE通過遞歸方式編寫通用函數(shù)自動生成連續(xù)數(shù)字和日期 ,需要的朋友可以參考下2019-07-07
sqlserver數(shù)據(jù)庫使用存儲過程和dbmail實(shí)現(xiàn)定時發(fā)送郵件
這篇文章主要介紹了sqlserver數(shù)據(jù)庫存儲過程和Job實(shí)現(xiàn)定時從數(shù)據(jù)庫發(fā)送郵件的功能,大家參考使用吧2014-01-01
Sql Server 存儲過程調(diào)用存儲過程接收輸出參數(shù)返回值
這篇文章主要介紹了Sql Server 存儲過程調(diào)用存儲過程接收輸出參數(shù)返回值,需要的朋友可以參考下2017-06-06
SQL?Server?2019完整安裝教程(最新最詳細(xì)!)
SQL Server是一款Microsoft公司推出的關(guān)系型數(shù)據(jù)庫管理系統(tǒng),下面這篇文章主要給大家介紹了關(guān)于SQL?Server?2019完整安裝教程的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),本文介紹的是最新最詳細(xì),需要的朋友可以參考下2023-02-02
SQL Server 數(shù)據(jù)庫管理常用的SQL和T-SQL語句
SQL Server 數(shù)據(jù)庫管理常用的SQL和T-SQL語句...2006-07-07
SQL Server中通過擴(kuò)展存儲過程實(shí)現(xiàn)數(shù)據(jù)庫的遠(yuǎn)程備份與恢復(fù)
SQL Server中通過擴(kuò)展存儲過程實(shí)現(xiàn)數(shù)據(jù)庫的遠(yuǎn)程備份與恢復(fù)實(shí)現(xiàn)方法,需要的朋友可以參考下2012-05-05

