SQL Server触发器
何为触发器:触发器是数据库服务器中发生事件时自动执行的特种存储过程,这是帮助文档上的定义,就认为是一种特殊的存储过程也行,经常用于强制执行业务规则和数据完整性。
在正式操作之前先把触发器原理给说说,理解好触发器的机制是对更好的运用触发器自己好处很明显。
1、原理:
在执行触发器的过程中会产生两张临时表INSERTED和DELETED,这两张表的作用简单说就是拿来记录数据的,主我们可以在触发器执行过程中更好的对数据进行操作
对表执行INSERT和UPDATE操作时都会在临时表INSERTED中拷贝一份所增加的数据。
对表执行DELETE和UPDATE操作时都会在临时表DELETED中拷贝一份所删除的数据
对表执行UPDATE操作时,首先把UPDATE前的数据DELETE到DELETED临时表中,然后再把所要更新的数据插入表中,最后把更新后的数据拷贝到INSERTED临时表中
触发器分为两种AFTER和INSTEADOF
2、AFTER
字面意思就是在对表执行INSERT、UPDATE、DELETE操作后触发的触发器了,这里以INSERT结合触发器原理简单描述其过程
当有INSERT语句要执行时,首先直接执行INSERT语句,再就是AFTER执行触发器里面的操作(把数据拷贝到临时表INSERTED中,然后进行我们想要的操作,最后完成同样删除了临时表)
下面就开始进行实例演示了:
--学生
createtableStudent
(
StudentNointprimarykey,
StudentNamevarchar(20)notnull
)
--书本
createtableBook
(
BookIdintidentity(1,1),
BookNamevarchar(30),
OwnerintforeignkeyreferencesStudent(StudentNo)
)
insertintoStudentvalues(1,'ShepherlDeng')
insertintoStudentvalues(2,'Divi')
insertintoStudentvalues(3,'Lili')
insertintoBookvalues('Book--01',1)
insertintoBookvalues('Book--02',2)
insertintoBookvalues('Book--01',3)
insertintoBookvalues('Book--02',1)
insertintoBookvalues('Book--03',2)
insertintoBookvalues('Book--03',3)
insertintoBookvalues('Book--04',1)
insertintoBookvalues('Book--04',2)
insertintoBookvalues('Book--01',3)
--学生
createtableStudent
(
StudentNointprimarykey,
StudentNamevarchar(20)notnull
)
--书本
createtableBook
(
BookIdintidentity(1,1),
BookNamevarchar(30),
OwnerintforeignkeyreferencesStudent(StudentNo)
)
insertintoStudentvalues(1,'ShepherlDeng')
insertintoStudentvalues(2,'Divi')
insertintoStudentvalues(3,'Lili')
insertintoBookvalues('Book--01',1)
insertintoBookvalues('Book--02',2)
insertintoBookvalues('Book--01',3)
insertintoBookvalues('Book--02',1)
insertintoBookvalues('Book--03',2)
insertintoBookvalues('Book--03',3)
insertintoBookvalues('Book--04',1)
insertintoBookvalues('Book--04',2)
insertintoBookvalues('Book--01',3)
如果我们有这样一个业务需求,就是当有一个学生时,必须买Book--01这一本书
于是我们可以写个简单的AFTER触发器
CREATETRIGGERitStudent
onStudent
afterInsert
as
begin
declare@studentNoint
select@studentNo=StudentNofromInserted
insertintoBookvalues('Book--01',@studentNo)
end
当我们增加一条记录时就会自动在Book表中增加一条记录,这里其它操作也很相像所以不多说了只要知道是在操作守后再执行,重点放在INSTEADOF触发器上。
3、INSTEADOF
字面意思为取代,难道说当在一张表上定义了这样的触发器后,对表所做的INSERT、UPDATE、DELETE操作会被替换掉而不执行了?呵呵…这种触发器执行过程为:
当对表执行INSERT等操作时,并不直接执行这些操作而是转到触发器里面来执行触发器所定义的操作语句(应该说是一起执行的更合适);
演示仍然为上面的所创建的表首先我们演示一个级联删除,当我们在Student表中删除一个被Book引用记录时因为有完整无缺约束我们无法删除这是INSTEADOF就有作用了
CREATETRIGGERdtStudent
onStudent
insteadofDelete
as
begin
declare@studentNoint
select@studentNo=StudentNofromDeleted
deleteBookwhereOwner=@studentNo
end
呵呵这是不是很爽呢……
那我们再来演示一个UPDATE的INSTEADOF触发器的操作
CreateTriggerutStudent
onStudent
insteadofUpdate
as
begin
declare@studentNoint,
@studentNoldint
ifupdate(StudentNo)
begin
select@studentNold=StudentNofromDeleted
select@studentNo=StudentNofromInserted
updateBooksetOwner=@studentNowhereOwner=@studentNold
end
end
这会是什么结果呢?执行后就会发现当你更改StudentNo是它的子表Book中的Owner也会一起改变了…
我还是觉得触发器能解决的好像存储过程也可以,以前也用存储过程写级联删除感觉要比这个要繁琐点,还有人说触发器是高手用的,总之各有各的应用场景了看怎么用了。