触发器:一种特殊的存储过程,特殊在它不能手动进行调用,只能通过操作自动去触发
完成一些复杂的约束(是普通约束做不到的)
操作类型分类:
insert触发器:由insert插入操作来触发
update触发器:由update修改操作来触发
delete触发器:由delete删除操作来触发
触发时间分类:
for(after):先执行sql语句,然后再执行触发器
instead of:替代触发器,先执行触发器
临时表:只能触发器里面用
---------------------------------------------------------------------------------------------------------------
insert操作 delete操作 修改操作
---------------------------------------------------------------------------------------------------------------
inserted 存放插入的数据 修改后的数据
---------------------------------------------------------------------------------------------------------------
deleted 存放删除的数据 修改前的数据
---------------------------------------------------------------------------------------------------------------
创建两个表做演示:
--表1 if exists(select * from sys.objects where name='b1') begin drop table b1 end go create table b1 ( name varchar(20) not null, pwd varchar(20) not null ) --表2 if exists(select * from sys.objects where name='b2') begin drop table b2 end go create table b2 ( Id int primary key identity(1,1), username varchar(20), ) select * from b1 select * from b2
执行原理:选中insert语句,点击执行,把sql语句执行后,才会执行触发器
if exists(select * from sys.objects where name='trigger_insertrecordinfo') begin drop trigger trigger_insertrecordinfo --删除触发器 end go create trigger trigger_insertrecordinfo --创建触发器 on b2 for insert as update b1 set name='张三' where name= ( select username from b2 where username='admin' ) go insert into b1 values('admin','123456') insert into b2 values('admin1') select * from b1 select * from b2
示例二,创建触发器,实现自动备份的效果(对b2表进行删除后,将删除后的数据自动备份到新表中(只备份最新的一次))
if exists(select * from sys.objects where name='trigger_delrecordinfo') begin drop trigger trigger_delrecordinfo end go create trigger trigger_delrecordinfo on b2 for delete as if exists(select * from sys.objects where name='newtable') begin drop table newtable end select * into newtable from deleted --将删除后的数据 备份到表newtable里面 go --用delete from 来删除b2数据,不会删除表,数据会自动备份给新表newtable delete from b2 select * from newtable --备份了b2的数据,显示出来 select * from b2 --显示b2没有数据了
示例三:创建触发器,实现换机功能(修改上机记录时,自动修改计算机状态)
if exists(select * from sys.objects where name='trigger_updaterecordinfo') begin drop trigger trigger_updaterecordinfo end go create trigger trigger_updaterecordinfo on b2 for update as --对调两台计算机的状态 declare @oldpcid varchar(20),@newpcid varchar(20) select @newpcid=username from inserted --修改后的 select @oldpcid=username from deleted--修改前的 update b1 set name='以上机' where pwd=@newpcid update b1 set name='未上线' where pwd=@oldpcid go update b2 set username='123456' where Id=2 and username is not null select * from b1 select * from b2
示例四:创建触发器,实现级联删除的功能
if exists(select * from sys.objects where name='trigger_delstu') begin drop trigger trigger_delstu end go create trigger trigger_delstu on b2 instead of delete as declare @stuno varchar(20) select @stuno=username from deleted --判断:丛表有引用就删除丛表 if exists(select * from b1 where pwd=@stuno) begin delete from b1 where pwd=@stuno end --删除主表 delete from b2 where username=@stuno go delete from b2 where username='123456' select * from b1 select * from b2
标签:触发器,--,SQLServer,trigger,b2,where,select From: https://www.cnblogs.com/longxinyv/p/16751948.html