SQL Server 2005系列教学_ 触发器
触发器:
本质是一个存储过程
select * from users
--drop trigger reminder
禁止插入行政部新员工
create trigger inse03
on yuangong for insert
as
begin
if (select count(*) from inserted where 部门='' )>0
begin
rollback
print '不能插入新记录'
end
end
删除触发器:
create trigger 名称
on 表名 for delete
as
begin
代码
end
更新触发器:
用到临时表: 两个:
inserted
deleted
测试:
create table 学生表
(
姓名 nvarchar(8) not null primary key,
性别 nvarchar(2) not null ,
年龄 tinyint
)
create table 人数表
(
性别 nvarchar(2),
人数 int
)
insert 人数表 values ('女',0)
insert 人数表 values ('男',0)
select * from 学生表
select * from 人数表
select * from 学生表
select * from 人数表
select * from 学生表
select * from 人数表
insert into 学生表 values ('小孙','女',20)
insert into 学生表 values ('小纪','女',19)
delete from 学生表 where 性别='女'
本质是一个存储过程
常见的触发器类型:
插入触发器 insert
删除触发器 delete
更新触发器 update
插入触发器 insert
删除触发器 delete
更新触发器 update
插入触发器:
语法:
create trigger 名称
on 表名 for insert
as
begin
代码
end
on 表名 for insert
as
begin
代码
end
select * from users
insert into users values ('张三','男',20,40)
例:禁止插入新记录
alter trigger inse01
on users for insert
as
begin
if exists( select * from inserted) 或者写成 count(*)
rollback
print '禁止插入新员工信息'
end
on users for insert
as
begin
if exists( select * from inserted) 或者写成 count(*)
rollback
print '禁止插入新员工信息'
end
insert into users values ('张三风','男',20,40)
select * from users
select * from users
--simpletrigger.sql
--简单的触发器
use northwind
--简单的触发器
use northwind
例:
alter trigger 简单触发器 on dufei for insert as
declare @ID int,@username nvarchar(50)
set @ID = @@identity
set @username = (select username from dufei where ID=@ID)
print '最后写入的记录的编号是:' + convert(nvarchar,@ID) + ' 用户名是:' + @username
go
insert dufei (username) values('吴小华')
alter trigger 简单触发器 on dufei for insert as
declare @ID int,@username nvarchar(50)
set @ID = @@identity
set @username = (select username from dufei where ID=@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))
create table dufei
(id int identity(1,1),
username varchar(10))
--drop trigger reminder
insert into employees(LastName,FirstName)
values('hahaha1','xixixi1')
values('hahaha1','xixixi1')
select * from employees
--禁止触发器
alter table employees disable trigger reminder
--启用触发器
alter table employees enable trigger reminder
select * from employees
alter table employees disable trigger reminder
--启用触发器
alter table employees enable trigger reminder
select * from employees
解除触发器:(删除触发器)
drop trigger 名称
drop trigger inse01
drop trigger 名称
drop trigger inse01
禁止插入行政部新员工
alter trigger inse02
on yuangong for insert
as
begin
if exists(select * from inserted where 部门='行政部' )
begin
rollback
print '你的权限不够,不能插入行政部新员工!'
end
end
on yuangong for insert
as
begin
if exists(select * from inserted where 部门='行政部' )
begin
rollback
print '你的权限不够,不能插入行政部新员工!'
end
end
select * from yuangong
delete from yuangong where 姓名='于洋'
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
end
drop trigger inse03
insert 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
end
on yuangong for delete
as
begin
if exists(select * from deleted where 部门='人事部')
begin
rollback
print '不能删除人事部员工'
end
end
select * from yuangong
delete from yuangong where 编号='0006'
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 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
on yuangong for update
as
begin
if update(基本工资) or update(奖金)
begin
rollback
print '不允许更改数据!'
end
end
select * from yuangong
update yuangong set 年龄=30 where 姓名='于洋'
Create unique ix_name on hr (姓,名)
create [unique][clustered][nonstered] index on 数据表名称(列名称)
只允许更改姓名,性别,年龄
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
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 姓名='于小洋',性别 ='女',年龄=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
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)
insert into 学生表 values ('小胡','男',19)
alter trigger upd001
on 学生表 for insert
as
begin
update 人数表 set 人数=人数+1 where 性别 in (select 性别 from inserted )
end
on 学生表 for insert
as
begin
update 人数表 set 人数=人数+1 where 性别 in (select 性别 from inserted )
end
drop 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
测试: 分开删除
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
测试: 分开删除