createtrigger 触发器名 on zg -- 在zg表建立触发器 forinsert,update-- 为什么事件触发 as-- 事件触发后所要做的事情 select*from inserted select*from deleted if exists (select*from ck where 仓库号 in (select 仓库号 from inserted)) commit else rollback
1 2 3 4 5
CREATETRIGGER trigger_name [BEFORE|AFTER] event_name ON table_name BEGIN -- Trigger logic goes here.... END
Deleted 与 Inserted
Deleted表存放由于执行Delete或Update语句而要从表中删除的所有行。
Inserted表存放由于执行Insert或Update语句而要向表中插入的所有行。
UPDATE 操作上在表的一个或多个指定列上创建触发器(Trigger)
语法
1 2 3 4 5
CREATETRIGGER trigger_name [BEFORE|AFTER] UPDATEOF column_name ON table_name BEGIN -- Trigger logic goes here.... END
触发器操作
删除
1
droptrigger trigger_name
修改
1 2 3 4 5 6 7 8 9
altertrigger tri_ck_zg on zg forinsert,update as select*from inserted select*from deleted if exists (select*from ck where 仓库号 in (select 仓库号 from inserted)) commit else rollback
列出触发器
所有
1 2
SELECT name FROM sqlite_master WHERE type ='trigger'
指定表
1 2
SELECT name FROM sqlite_master WHERE type ='trigger'AND tbl_name ='表名'
习题
成绩小于60的不可更改
1 2 3 4 5 6 7 8 9
createtrigger tri_grade on xkgx forupdate as select*from inserted select*from deleted
if update(成绩) if exists(select*from deleted where 成绩<60) rollback
- 验证
1
update xkgx set 成绩=80where课程号='303'and 学号='S101101'
成绩小于60的不可更改
1 2 3 4 5 6 7 8 9 10
createtrigger tri_grade on xkgx forupdate as select*from inserted select*from deleted
if update(成绩) if exists(select*from inserted join deleted on inserted.学号=deleted.学号 where inserted.成绩>=60and deleted.成绩<60) rollback