IF EXISTS(SELECT * FROM sysobjects
WHERE name='tr_Users_OnUpdate' AND TYPE='TR')
DROP TRIGGER tr_Users_OnUpdate
GO --这里呢创建触发器与存储过程类似(都是DDL)
--先判断如否存在同名触发器就删除然后重建
CREATE TRIGGER tr_Users_OnUpdate
ON Users FOR UPDATE
AS PRINT ‘Users表已发生修改'
GO
--添加留言的触发器
IF EXISTS(SELECT name FROM sysobjects WHERE name='tr_LeaveWord_Add' AND TYPE='TR')
DROP TRIGGER tr_LeaveWord_Add
GO
CREATE TRIGGER tr_LeaveWord_Add
ON WordInfo FOR INSERT
AS UPDATE UserInfo SET LeaveCount=LeaveCount+1
WHERE UserID=(SELECT TOP 1 UserID FROM Inserted)
GO
create trigger trigMessageList
on table1
after insert,update
as raiserror('数据一致性验证',16,1)
此时如果我们手动打开table1,输入一条,根本就插入不了,我们只能写一条如下的SQL语句插入一条数据
insert into table1(user_name)values('23')
结果数据是插入成功了,但是会出显如下图所示的提示:
下面我再创建一个稍微有点复杂的after触发器:
--该触发器作用是:如果向table1中插入数据时,
--先检查要向table1插入的这条的USER_ID是否USERS(我创建的另一个表)表中的USER_ID字段中有,如果不存在则不允许插入
create trigger trigInsertedMessages
on table1
after insert
as
if exists (select * from inserted a where a.user_id not in (select USER_ID from USERS))
begin
raiserror('数据一致性验证',16,1)
rollback transaction
end
inserted of触发器和after触发器写法差不多,在此就不多写了。
下面创建一个简单的DDL触发器:
复制代码
--它的作用是:防止数据库Test(我创建的一个实验数据库)中的任一表被修改或删除
create trigger trigDB
on database
for drop_table,alter_table
as
print '你一定要禁用触发器“trigDB"才能删除或修改这个数据库的表'
rollback
删除触发器:
drop trigger trigDB
ON DATABASE
一定要记得加上ON DATABASE这条,否则就删除不了。
最后再来条开启和禁用触发器的语句:
disable trigger trigDB on database --禁用触发器
enable trigger trigDB on database --开启触发器
create trigger tgr_nameon table_namewith encrypion –加密触发器 for update...as Transact-SQL
# 创建insert类型触发器
--创建insert插入类型触发器if (object_id('tgr_classes_insert', 'tr') is not null) drop trigger tgr_classes_insertgocreate trigger tgr_classes_inserton classes for insert --插入触发as --定义变量 declare @id int, @name varchar
(20), @temp int; --在inserted表中查询已经插入记录信息 select @id = id, @name = name from inserted; set @name = @name + convert(varchar, @id); set @temp = @id / 2; insert into student values(@name, 18 + @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_deletegocreate trigger tgr_classes_deleteon 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_updategocreate trigger tgr_classes_updateon classes for updateas declare @oldName varchar(20), @newName varchar(20); --更新
前的数据 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_columngocreate trigger tgr_classes_update_columnon classes for updateas --列级触发器:是否更新了班级创建时间 if (update
(createDate)) begin raisError('系统提示:班级创建时间不能修改!', 16, 11); rollback tran; endgo--测试select * from student order by id;select * from classes;update classes set createDate = getDate() where id =
3;update classes set name = '四班' where id = 7; 更新列级触发器可以用update是否判断更新列记录;
create trigger tgr_nameon table_namewith encryption instead of update...as T-SQL
# 创建instead of触发器
if (object_id('tgr_classes_inteadOf', 'TR') is not null) drop trigger tgr_classes_inteadOfgocreate trigger tgr_classes_inteadOfon classes instead of delete/*, update, insert*/as declare @id int, @name varchar(20); --查询被删
除的信息,病赋值 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--testselect * from student order by id;select * from classes;delete classes where id = 7;
# 显示自定义消息raiserror
if (object_id('tgr_message', 'TR') is not null) drop trigger tgr_messagegocreate trigger tgr_messageon student after insert, updateas raisError('tgr_message触发器被触发', 16, 10);go--testinsert into student values('lily', 22, 1,
7);update student set sex = 0 where name = 'lucy';select * from student order by id;
# 修改触发器
alter trigger tgr_messageon studentafter deleteas raisError('tgr_message触发器被触发', 16, 10);go--testdelete 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 ton t.object_id = te.object_idwhere t.parent_class = 0 and t.name =
if ((object_id('tgr_valid_data', 'TR') is not null)) drop trigger tgr_valid_datagocreate trigger tgr_valid_dataon studentafter insertas declare @age int, @name varchar(20); select @name = s.name, @age = s.age from
inserted s; if (@age < 18) begin raisError('插入新数据的age有问题', 16, 1); rollback tran; endgo--testinsert into student values('forest', 2, 0, 7);insert into student values('forest', 22, 0, 7);select * from
student order by id;
# 示例,操作日志
if (object_id('log', 'U') is not null) drop table loggocreate table log( id int identity(1, 1) primary key, action varchar(20), createDate datetime default getDate())goif (exists (select * from sys.objects where name =
'tgr_student_log')) drop trigger tgr_student_loggocreate trigger tgr_student_logon studentafter insert, update, deleteas if ((exists (select 1 from inserted)) and (exists (select 1 from deleted))) begin insert into log
(action) values('updated'); end else if (exists (select 1 from inserted) and not exists (select 1 from deleted)) begin insert into log(action) values('inserted'); end else if (not exists (select 1 from inserted) and
exists (select 1 from deleted)) begin insert into log(action) values('deleted'); endgo--testinsert into student values('king', 22, 1, 7);update student set sex = 0 where name = 'king';delete student where name =
'king';select * from log;select * from student order by id;
alter trigger trigger_学生_Delete
on 学生
instead of Delete
as
begin
select 学号, 姓名 from deleted
end
delete from 学生 where 学号 = 4
上例中定义了“trigger学生_Delete”触发器,该触发器从“delete”表中打印出所要删除的学生.在执行“delete”操作后,会发现“学号 = 4”的学生并未被删除, 原因在于“trigger学生Delete”替代了所要执行的“delete from 学生 where 学号 = 4”语句,而
alter trigger trigger_学生_Delete
on 学生
instead of Delete
as
begin
if not exists(select * from 借书记录, deleted where 借书记录.学号 = deleted.学号)
delete from 学生 where 学生.学号 in (select 学号 from deleted)
end
10. “After”触发器
10.1 在“订单”表中建立触发器,当向“订单”表中插入一条订单记录时,检查“商品”表的货品状态“状态”是否为1(正在整理),则不能往“订单”表加入该订单。
create trigger trigger_订单_insert
on 订单
after insert
as
if (select 状态 from 商品, inserted where 商品.pid = inserted.pid)=1
begin
print 'the goods is being processed'
print 'the order cannot be committed'
rollback transaction --回滚,避免加入
end
该示例中“pid”为商品编码
该示例的if判断严格来讲是不准确的,因为“订单”表如果每次插入一条记录,该判断没有问题;如果一次插入多条记录,则“select 状态”返回的是多行。
10.2 在“订单”表建立一个插入触发器,在添加一条订单时,减少“商品”表相应的货品记录中的库存。
create trigger trigger_订单_insert2
on 订单
after insert
as
update 商品 set 数量 = 数量 - inserted.数量
from 商品, inserted
where 商品.pid = inserted.pid
10.3 在“商品”表建立删除触发器,实现“商品”表和“订单”表的级联删除。
create trigger goodsdelete trigger_商品_delete
on 商品
after delete
as
delete from 订单 where 订单.pid in (select pid from deleted)
10.4 在“订单”表建立一个更新触发器,监视“订单”表的“订单日期”列,使其不能被“update”.
create trigger trigger_订单_update
on 订单
after update
as
if update(订单日期)
begin
raiserror('订单日期不能手动修改',10,1)
rollback transaction
end
10.5 在“订单”表建立一个插入触发器,保证向“订单”表插入的货品必须要在“商品”表中一定存在。
create trigger trigger_订单_insert3
on 订单
after insert
as
if (select count(*) from 商品, inserted where 商品.pid = inserted.pid)=0
begin
print '商品不存在'
rollback transaction
end
10.6 “订单”表建立一个插入触发器,保证向“订单”表插入的货品信息要在“订单日志”表中添加
alter trigger trigger_订单_insert
on 订单
for insert
as
insert into 订单日志 select inserted.Id, inserted.pid,inserted.数量 from inserted
触发器的分类
1
DML( 数据操纵语言 Data Manipulation Language)触发器:是指触发器在数据库中发生DML事件时将启用。DML事件即指在表或视图中修改数据的insert、update、delete语句。
2
DDL(数据定义语言 Data Definition Language)触发器:是指当服务器或数据库中发生(DDL事件时将启用。DDL事件即指在表或索引中的create、alter、drop语句也。
3
登陆触发器:是指当用户登录SQL SERVER实例建立会话时触发。
DML触发器介绍
1
在SQL SERVER 2008中,DML触发器的实现使用两个逻辑表DELETED和INSERTED。这两个表是建立在数据库服务器的内存中,我们只有只读的权限。DELETED和INSERED表的结构和触发器所在的数据表的结构是一样的。当触发器执行完成后,它们也就会被自动删除:INSERED
4.不能让两个触发器触发两个表,并使之循环!如:触发器a触发b表的insert操作,并往d表insert数据;触发器c触发d表的insert操作,并往表b insert数据;这样是不行的!会报错如下:Can't update table 'test' in stored function/trigger because it is
already used by statement which invoked this stored function/trigger.(第三点也会报这样的错误!)
6.如果要在触发update的同时,修改该数据加1!用传统的方法update table set id = old.id + 1;这样肯定是不行的,违背了第三点,会报错:Can't update table 'test' in stored function/trigger because it is already used by statement which invoked
this stored function/trigger.注意,这个错在触发器编译的时候,是能够通过的,但是在更新的时候,数据库会抛这个异常!
如果一定要这样做,那该怎么办呢?借助关键字new和old.
SET new.id = new.id + 1;
Abstract Factory:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 Adapter:将一个类的接口转换成客户希望的另外一个接口。A d a p t e r模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 Bridge:将抽象部分与它的实现部分分离,使它们都可以独立地变化。 Builder:将一个复杂对象的构建与它的表示分离,使得同
import java.util.LinkedList;
public class CaseInsensitiveTrie {
/**
字典树的Java实现。实现了插入、查询以及深度优先遍历。
Trie tree's java implementation.(Insert,Search,DFS)
Problem Description
Igna
/*
2013年3月11日20:37:32
地点:北京潘家园
功能:完成用户格式化输入多个值
目的:学习scanf函数的使用
*/
# include <stdio.h>
int main(void)
{
int i, j, k;
printf("please input three number:\n"); //提示用
数据表中有记录的time字段(属性为timestamp)其值为:“0000-00-00 00:00:00”
程序使用select 语句从中取数据时出现以下异常:
java.sql.SQLException:Value '0000-00-00' can not be represented as java.sql.Date
java.sql.SQLException: Valu