码迷,mamicode.com
首页 > 数据库 > 详细

20141111--SQL触发器

时间:2014-11-11 18:11:21      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   sp   for   数据   div   on   

---------------------触发器-----------------------------
--触发器本质上还是一个存储过程,trigger
--只不过不是通过exec调用执行,而是通过增删改查数据库的操作来触发
--针对主外键表操作时 将关联的表一起修改
--触发器 方便记忆的命名格式:Tr(缩写)_(表名)_(增/删/改)
--instead of 在...之前  等于 替换操作,只执行触发器里面的语句
--for (after) 在...之后  先执行语句在执行触发器内的语句
select *from Student--学生信息

alter trigger Tr_student_delete
on student
instead of delete
as
    delete from score where sno=109
    delete from Student where Sno=109
go
delete from Student where Sno=109

--------------------参数-------------------------
create trigger tr_dongtai
on teacher
instead of delete
as
begin
    declare @tno char(10)
    set @tno=(select tno from deleted)--deleted虚拟表,
    update teacher set tname=刘燕 where tno=@tno
end
go
drop trigger tr_dongtai
select * from Teacher
delete from Teacher where Tno=804
-----------------------------------------
create trigger tr_teacher_insert
on teacher 
for insert
as
begin
    declare @tno varchar(50)
    set @tno =(select tno from inserted)
    delete from teachar where tno=@tno
end
--------------------禁用,开启
alter table teacher disable trigger all--禁用所有触发器
alter table teacher enable trigger all--开启所有触发器
--all改成触发器名 可以开启或禁用单个触发器

 

20141111--SQL触发器

标签:style   blog   color   ar   sp   for   数据   div   on   

原文地址:http://www.cnblogs.com/Tirisfal/p/4089475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!