学习过SQL相关知识小伙伴肯定都知道存储过程、触发器、视图等相关知识,不知道的听也肯定听过,有的小伙伴可能会说,现在开发过程中都不会去使用这些相关东西了,但是渣渣东在这说一句“有些东西你可以不用,但是不能不会”;本文主要就是讲解触发器的如何创建以及触发条件,有需要的可以学习学习哦!
触发器(trigger)是SQL server提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发,比如当对一个表进行操作(insert,delete, update)时就会激活它执行。触发器经常用于加强数据的完整性约束和业务规则等。 触发器可以从 DBA_TRIGGERS ,USER_TRIGGERS 数据字典中查到。
要素:
触发器经常用于加强数据的完整性约束和业务规则等。 触发器创建语法四要素:
- 监视地点(table)
- 监视事件(insert/update/delete)
- 触发时间(after/before)
- 触发事件(insert/update/delete)
操作:
查看所有的触发器:
SHOW TRIGGERS;
删除指定的触发器:
DROP TRIGGER INSERT_FULL_PROLE;
创建简单(单语句)触发器:
AFTER: 触发语句之后执行,比如插入一条语句到监控表之后再去执行对应的sql语句
BEFORE: 触发语句之前执行,在向监控表插入数据之前先执行sql语句
CREATE TRIGGER 触发器名称 AFTER|BEFORE INSERT|UPDATE|DELETE ON 监控表
FOR EACH ROW
BEGIN
xxxx sql语句 (insert/update/delete/select)
END ;
创建复杂(多语句)触发器:
CREATE TRIGGER 触发器名称 AFTER|BEFORE INSERT|UPDATE|DELETE ON 监控表
FOR EACH ROW
BEGIN
if 条件 then
sql语句 (insert/update/delete/select)sql语句
else if xxxx then
sql语句 (insert/update/delete/select)sql语句
else
sql语句 (insert/update/delete/select)sql语句
END if;
END ;
禁用和启用触发器:
--禁用触发器
alter trigger 触发器名称 disable;
--启用触发器
alter trigger 触发器名称 enable;
示例:
-- 新增:
DELIMITER ||
CREATE TRIGGER INSERT_FULL_prole AFTER INSERT ON dwd_mat_fullinventoryinfo
FOR EACH ROW
BEGIN
DECLARE number int;
SET number = (select count(1) from dwd_mat_lock_storeinfo
where matnr=new.matnr
and werks=new.werks
and storage_location = new.storage_location
);
IF number > 0 then
update dwd_mat_lock_storeinfo
set MENGE = MENGE + new.MENGE,
SURPLUS_MENGE = MENGE - ifnull(POINT_MENGE,0)
where matnr=new.matnr
and werks=new.werks
and storage_location = new.storage_location;
else
insert into dwd_mat_lock_storeinfo(MATNR,MAKTX,WERKS,STORAGE_LOCATION,STORAGE_LOCATION_MARK,WBS,WBS_DESC, MENGE,UNIT,POINT_MENGE,SURPLUS_MENGE,GMT_CREATE,GMT_MODIFIED,CREATE_BY,LAST_MODIFIED_BY)
select MATNR,MAKTX,WERKS,STORAGE_LOCATION,STORAGE_LOCATION_MARK,WBS,WBS_DESC,
IFNULL(MENGE,0) MENGE,UNIT,IFNULL(POINT_MENGE,0) POINT_MENGE,(MENGE-POINT_MENGE) SURPLUS_MENGE,
GMT_CREATE,GMT_MODIFIED,CREATE_BY,LAST_MODIFIED_BY
from dwd_mat_fullinventoryinfo
where matnr=new.matnr
and werks=new.werks
and storage_location = new.storage_location ;
END IF;
END ;
or
-- 修改
DELIMITER ||
CREATE TRIGGER update_FULL_STOREINFO AFTER UPDATE ON dwd_mat_fullinventoryinfo
FOR EACH ROW
BEGIN
delete from dwd_mat_fullinventoryinfo where OLD.FULL_INVENTORY_INFO_ID = full_inventory_info_id;
insert into dwd_mat_lock_storeinfo(FULL_INVENTORY_INFO_ID,MATNR,MAKTX,WERKS,STORAGE_LOCATION,STORAGE_LOCATION_MARK,WBS,WBS_DESC,MENGE,UNIT,
POINT_MENGE,SURPLUS_MENGE,GMT_CREATE,GMT_MODIFIED,CREATE_BY,LAST_MODIFIED_BY)
select FULL_INVENTORY_INFO_ID,right(MATNR,9) MATNR,MAKTX,WERKS,STORAGE_LOCATION,STORAGE_LOCATION_MARK,WBS,WBS_DESC,IFNULL(MENGE,0) MENGE,UNIT,
ifnull(POINT_MENGE,0) POINT_MENGE,(MENGE-POINT_MENGE) SURPLUS_MENGE,GMT_CREATE,GMT_MODIFIED,CREATE_BY,LAST_MODIFIED_BY from
dwd_mat_fullinventoryinfo where OLD. FULL_INVENTORY_INFO_ID= full_inventory_info_id;
END ;
END:
文章到这就结束了,本人最近创建了一个群,欢迎各位进群讨论技术,互相学习!同时感兴趣的也可以关注我的博客东哥的博客,领取学习资料。