首页 > 代码库 > 20141111--SQL触发器

20141111--SQL触发器

---------------------触发器-------------------------------触发器本质上还是一个存储过程,trigger--只不过不是通过exec调用执行,而是通过增删改查数据库的操作来触发--针对主外键表操作时 将关联的表一起修改--触发器 方便记忆的命名格式:Tr(缩写)_(表名)_(增/删/改)--instead of 在...之前  等于 替换操作,只执行触发器里面的语句--for (after) 在...之后  先执行语句在执行触发器内的语句select *from Student--学生信息alter trigger Tr_student_deleteon studentinstead of deleteas    delete from score where sno=109    delete from Student where Sno=109godelete from Student where Sno=109--------------------参数-------------------------create trigger tr_dongtaion teacherinstead of deleteasbegin    declare @tno char(10)    set @tno=(select tno from deleted)--deleted虚拟表,    update teacher set tname=刘燕 where tno=@tnoendgodrop trigger tr_dongtaiselect * from Teacherdelete from Teacher where Tno=804-----------------------------------------create trigger tr_teacher_inserton teacher for insertasbegin    declare @tno varchar(50)    set @tno =(select tno from inserted)    delete from teachar where tno=@tnoend--------------------禁用,开启alter table teacher disable trigger all--禁用所有触发器alter table teacher enable trigger all--开启所有触发器--all改成触发器名 可以开启或禁用单个触发器

 

20141111--SQL触发器