一﹕ 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活。所以触发器可以用来实现对表实施复杂的完整性约`束。
二﹕ SQL Server为每个触发器都创建了两个专用表﹕Inserted表和Deleted表。这两个表由系统来维护﹐它们存在于内存中而不是在数据库中。这两个表的结构总是与被该触发器作用的表的结构相同。触发器执行 完成后﹐与该触发器相关的这两个表也被删除。
- Deleted表存放由于执行Delete或Update语句而要从表中删除的所有行。
- Inserted表存放由于执行Insert或Update语句而要向表中插入的所有行。
对表的操作 |
Inserted逻辑表 |
Deleted逻辑表 |
增加记录(insert) |
存放增加的记录 |
无 |
删除记录(delete) |
无 |
存放被删除的记录 |
修改记录(update) |
存放更新后的记录 |
存放更新前的记录 |
三﹕Instead of 和 After触发器
SQL Server2000提供了两种触发器﹕Instead of 和After 触发器。这两种触发器的差别在于他们被激活的同﹕
- Instead of触发器用于替代引起触发器执行的T-SQL语句。除表之外﹐Instead of 触发器也可以用于视图﹐用来扩展视图可以支持的更新操作。
- After触发器在一个Insert,Update或Deleted语句之后执行﹐进行约束检查等动作都在After触发器被激活之前发生。After触发器只能用于表。
- 一个表或视图的每一个修改动作(insert,update和delete)都可以有一个instead of 触发器﹐一个表的每个修改动作都可以有多个After触发器。
四﹕触发器的执行过程
- 如果一个Insert﹑update或者delete语句违反了约束﹐那幺After触发器不会执行﹐因为对约束的检查是在After触发器被激动之前发生的。所以After触发器不能超越约束。
- Instead of 触发器可以取代激发它的操作来执行。它在Inserted表和Deleted表刚刚建立﹐其它任何操作还没有发生时被执行。因为Instead of 触发器在约束之前执行﹐所以它可以对约束进行一些预处理。
五﹕使用T-SQL语句来创建触发器
--基本语句如下﹕ create trigger trigger_name on {table_name | view_name} {for | After | Instead of } [ insert, update,delete ] as sql_statement
六﹕删除触发器:
--基本语句如下﹕ drop trigger trigger_name
七:查看数据库中已有触发器:
-- 查看数据库已有触发器 use jxcSoftware go select * from sysobjects where xtype='TR' -- 查看单个触发器 exec sp_helptext '触发器名'
八﹕修改触发器:
--基本语句如下﹕ alter trigger trigger_name on {table_name | view_name} {for | After | Instead of } [ insert, update,delete ] as sql_statement
九﹕相关示例﹕
Ø 创建触发器 语法 create trigger tgr_name on table_name with encrypion –加密触发器 for update... as Transact-SQL # 创建insert类型触发器 --创建insert插入类型触发器 if (object_id('tgr_classes_insert', 'tr') is not null) drop trigger tgr_classes_insert go create trigger tgr_classes_insert on classes for insert --插入触发 as --定义变量 declare @id ), @temp int; --在inserted表中查询已经插入记录信息 select @id = id, @name = name from inserted; set @name = @name + convert(varchar, @id); ; insert into student values(@name, + @id, @temp, @id); print '添加学生成功!'; go --插入数据 insert into classes values('5班', getDate()); --查询数据 select * from classes; select * from student order by id; insert触发器,会在inserted表中添加一条刚插入的记录。 # 创建delete类型触发器 --delete删除类型触发器 if (object_id('tgr_classes_delete', 'TR') is not null) drop trigger tgr_classes_delete go create trigger tgr_classes_delete on classes for delete --删除触发 as print '备份数据中……'; if (object_id('classesBackup', 'U') is not null) --存在classesBackup,直接插入数据 insert into classesBackup select name, createDate from deleted; else --不存在classesBackup创建再插入 select * into classesBackup from deleted; print '备份数据成功!'; go -- --不显示影响行数 --set nocount on; delete classes where name = '5班'; --查询数据 select * from classes; select * from classesBackup; delete触发器会在删除数据的时候,将刚才删除的数据保存在deleted表中。 # 创建update类型触发器 --update更新类型触发器 if (object_id('tgr_classes_update', 'TR') is not null) drop trigger tgr_classes_update go create trigger tgr_classes_update on classes for update as declare @oldName varchar(), @newName varchar(); --更新前的数据 select @oldName = name from deleted; if (exists (select * from student where name like '%'+ @oldName + '%')) begin --更新后的数据 select @newName = name from inserted; update student set name = replace(name, @oldName, @newName) where name like '%'+ @oldName + '%'; print '级联修改数据成功!'; end else print '无需修改student表!'; go --查询数据 select * from student order by id; select * from classes; update classes set name = '五班' where name = '5班'; update触发器会在更新数据后,将更新前的数据保存在deleted表中,更新后的数据保存在inserted表中。 # update更新列级触发器 if (object_id('tgr_classes_update_column', 'TR') is not null) drop trigger tgr_classes_update_column go create trigger tgr_classes_update_column on classes for update as --列级触发器:是否更新了班级创建时间 if (update(createDate)) begin raisError(, ); rollback tran; end go --测试 select * from student order by id; select * from classes; update classes ; update classes ; 更新列级触发器可以用update是否判断更新列记录; # instead of类型触发器 instead of触发器表示并不执行其定义的操作(insert、update、delete)而仅是执行触发器本身的内容。 创建语法 create trigger tgr_name on table_name with encryption instead of update... as T-SQL # 创建instead of触发器 if (object_id('tgr_classes_inteadOf', 'TR') is not null) drop trigger tgr_classes_inteadOf go create trigger tgr_classes_inteadOf on classes instead of delete/*, update, insert*/ as declare @id ); --查询被删除的信息,病赋值 select @id = id, @name = name from deleted; print 'id: ' + convert(varchar, @id) + ', name: ' + @name; --先删除student的信息 delete student where cid = @id; --再删除classes的信息 delete classes where id = @id; print '删除[ id: ' + convert(varchar, @id) + ', name: ' + @name + ' ] 的信息成功!'; go --test select * from student order by id; select * from classes; delete classes ; # 显示自定义消息raiserror if (object_id('tgr_message', 'TR') is not null) drop trigger tgr_message go create trigger tgr_message on student after insert, update , ); go --test insert into student values(, , ); update student where name = 'lucy'; select * from student order by id; # 修改触发器 alter trigger tgr_message on student after delete , ); go --test delete from student where name = 'lucy'; # 启用、禁用触发器 --禁用触发器 disable trigger tgr_message on student; --启用触发器 enable trigger tgr_message on student; # 查询创建的触发器信息 --查询已存在的触发器 select * from sys.triggers; select * from sys.objects where type = 'TR'; --查看触发器触发事件 select te.* from sys.trigger_events te join sys.triggers t on t.object_id = te.object_id and t.name = 'tgr_valid_data'; --查看创建触发器语句 exec sp_helptext 'tgr_message'; # 示例,验证插入数据 if ((object_id('tgr_valid_data', 'TR') is not null)) drop trigger tgr_valid_data go create trigger tgr_valid_data on student after insert as declare @age int, @name varchar(); select @name = s.name, @age = s.age from inserted s; ) begin raisError(, ); rollback tran; end go --test insert into student values(, , ); insert into student values(, , ); select * from student order by id; # 示例,操作日志 if (object_id('log', 'U') is not null) drop table log go create table log( id , ) primary key, action varchar(), createDate datetime default getDate() ) go if (exists (select * from sys.objects where name = 'tgr_student_log')) drop trigger tgr_student_log go create trigger tgr_student_log on student after insert, update, delete as from deleted))) begin insert into log(action) values('updated'); end from deleted)) begin insert into log(action) values('inserted'); end from deleted)) begin insert into log(action) values('deleted'); end go --test insert into student values(, , ); update student where name = 'king'; delete student where name = 'king'; select * from log; select * from student order by id;