【www.gdgbn.com--php与数据库】
sql 创建触发器
创建触发器语法
create trigger triggername on tablename
for insert |delete|update
as
begin
end
来面来看一下创建触发器的实例
create table ta1
(
taid int identity(1001,1) primary key ,
taname varchar(20) not null,
tasl int
)
create table ta2
(
ta2id int identity(1001,1) primary key ,
ta2name varchar(20) not null,
ta2ysh int,
ta2sl int
)
create trigger tru_ta1
on ta1
for update
as
begin
if exists(select 1 from ta2,inserted where ta2id=taid and ta2ysh < tasl)
rollback
else
update ta2 set ta2sl=tasl
from inserted where ta2id=taid
end
上面的功能是实例
要求是当我对ta1的tasl 修改时 触发,首先先比较ta1表中 tasl 是否 大于ta2表中 ta2ysh 如果大于ta2 表的 ta2ysh 则不允许进行修改操作,否则 先修改ta2表中的ta2sl 然后修改 ta1中的tasl