首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL Server 2005系列教授教养_ 触发器

2012-08-31 
SQL Server 2005系列教学_ 触发器触发器:本质是一个存储过程 常见的触发器类型:插入触发器? insert删除触

SQL Server 2005系列教学_ 触发器
触发器:
本质是一个存储过程 常见的触发器类型:
插入触发器? insert
删除触发器? delete
更新触发器? update插入触发器:语法:create trigger 名称
on 表名? for insert
as
begin
?? 代码
end
select? * from usersinsert into users values ('张三','男',20,40)例:禁止插入新记录alter trigger inse01
on users for insert
as
begin
? if exists( select? * from? inserted)?? 或者写成 count(*)
??? rollback
?? print '禁止插入新员工信息'
endinsert into users values ('张三风','男',20,40)
select? * from users--simpletrigger.sql
--简单的触发器
use northwind例:
alter trigger 简单触发器 on dufei for insert as
declare @ID int,@username nvarchar(50)
set @ID = @@identity
set @username = (select username from dufei whereID=@ID)
print '最后写入的记录的编号是:' + convert(nvarchar,@ID) + '? 用户名是:' + @username
go
insert dufei (username) values('吴小华')但如果在print 后加一个rollback结果就不同了。说明是手插入到表中了,但又回滚了。select @@identity
create table dufei
(id? int identity(1,1),
username varchar(10))
--drop trigger reminderinsert into employees(LastName,FirstName)
values('hahaha1','xixixi1')select * from employees--禁止触发器
alter table employees disable trigger reminder
?
--启用触发器
alter table employees enable trigger reminder
select * from employees解除触发器:(删除触发器)
drop trigger 名称
drop trigger inse01
禁止插入行政部新员工alter trigger inse02
on yuangong for insert
as
begin
if exists(select * from inserted where 部门='行政部' )
begin
? rollback
? print? '你的权限不够,不能插入行政部新员工!'
end
endselect? * from yuangong
delete from yuangong? where 姓名='于洋'insert into yuangong values? ('0001','于洋','男',20,'2007-12-26','河南',3000,2000,500,1,'123456','保安部')count(*)
create trigger inse03
on yuangong for insert
as
begin
???? if (select count(*) from inserted? where 部门='' )>0
???? begin
????????? rollback
????????? print? '不能插入新记录'
???? end
enddrop trigger inse03insert into yuangong values? ('0001','于洋','男',20,'2007-12-26','河南',3000,2000,500,1,'123456','保安部')
删除触发器:
?create trigger 名称
?on 表名 for delete
?as
?begin
?? 代码
? end用到一个临时表: deleted 存放的是准备删除的记录禁止删除技术部的员工? alter trigger del01
? on? yuangong for delete
? as
? begin
???? if exists(select * from deleted? where 部门='人事部')
???? begin
???????? rollback
???????? print? '不能删除人事部员工'
???? end
? endselect * from yuangong
delete from yuangong where 编号='0006'
更新触发器:
用到临时表: 两个:
inserted
deleted禁止用户更新:
create? trigger upd01
on yuangong for update
as
begin
??? if exists(select? * from inserted)
??? begin
??????? rollback
??????? print? '不允许更改数据!'
??? end
end禁止更新基本工资和奖金create? trigger upd01
on yuangong for update
as
begin
??? if update(基本工资) or update(奖金)
??? begin
??????? rollback
??????? print? '不允许更改数据!'
??? end
end?select? * from yuangongupdate yuangong set 年龄=30? where 姓名='于洋'
Create unique ix_name?? on hr (姓,名)
create [unique][clustered][nonstered] index on 数据表名称(列名称)
只允许更改姓名,性别,年龄create trigger upda02
on yuangong for update
as
begin
?? if update(姓名) or update(性别)? or update (年龄)
??? begin
??????? commit
???? end
??? else
??? begin
????? rollback
????? print? '不能修改姓名,性别,年龄之外的数据'
???? end
end
测试:select * from yuangong
update yuangong set? 姓名='于小洋',性别 ='女',年龄=30?? where 姓名='于洋'update yuangong set 编号='0008' where 姓名='于小洋'
create? table 学生表
(
姓名 nvarchar(8) not null primary key,
性别 nvarchar(2) not null ,
年龄 tinyint
)
create table 人数表
(
性别 nvarchar(2),
人数 int
)
insert 人数表 values ('女',0)
insert 人数表 values ('男',0)drop TABLE 学生表
select? * from 学生表
select? * from 人数表create trigger test2
on 学生表 for insert
as
begin
?? if exists (select * from inserted where 性别='男')
?? begin
???? update 人数表 set 人数=(select count(*) from 学生表 group by 性别 having 性别='男')
???? where 性别='男'
?? end
?? else
?? begin
???? if exists (select * from inserted where 性别='女')
????? begin
???????? update 人数表 set 人数=(select count(*) from 学生表 group by 性别 having 性别='女')
????????? where 性别='女'
?????? end
?? end
end
select? * from 学生表
select? * from 人数表insert into 学生表 values ('小王','男',20)
insert into 学生表 values ('小胡','男',19)alter trigger upd001
on 学生表? for? insert
as
begin
?
?update 人数表? set? 人数=人数+1? where 性别 in (select 性别 from inserted )
enddrop trigger test2
select? * from 学生表
select? * from 人数表
insert into 学生表 values ('小孙','女',20)
insert into 学生表 values ('小纪','女',19)
delete from? 学生表? where 性别='女'实现删除学生记录create trigger dele01
on 学生表 for delete
as
if exists(select? * from deleted)
?begin
??? update 人数表 set 人数=(select count(*) from 学生表 where 性别='男')? where 性别='男'
??????? update 人数表 set 人数=(select count(*) from 学生表 where 性别='女')? where 性别='女'
?end
测试:? 分开删除

?

热点排行