mysql触发器的使用

语法:

create trigger 触发器名 before(或者after) update(或者delete或者insert)on 数据库表名
for each row(固定结构)
begin
  sql语句
end 

实例:


  create trigger calculate_fileSize after update on dps_task

 for each row

BEGIN
IF new.is_finish = 1 and new.doc_type = 379 THEN
           UPDATE dps_resource SET filesize = (SELECT SUM(filesize) 
           from dps_resource_associate dra WHERE dra.resource_id = 
           new.resource_id and dra.uri like "%/pic/%") WHERE 
           dps_resource.id = new.resource_id;
ELSEIF new.is_finish = 1 and new.doc_type <> 51 THEN
           UPDATE dps_resource SET filesize = (SELECT SUM(filesize) 
           from dps_resource_associate dra WHERE dra.resource_id = 
           new.resource_id and dra.uri like "%/origpic/%") WHERE 
           dps_resource.id = new.resource_id;
END IF;
END;        

new: 指的是sql中更新后的那条数据

old:指的是sql中更新前的那条数据

如果事件是delete,则只有old没有new。如果事件是insert, 则只有new没有old,否则会报错。

    

posted @ 2021-03-15 10:51  曹伟666  阅读(75)  评论(0编辑  收藏  举报