20141111--SQL触发器

---------------------触发器-----------------------------
--触发器本质上还是一个存储过程,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改成触发器名 可以开启或禁用单个触发器

 

你可能感兴趣的:(sql)