美文网首页
MySql触发器

MySql触发器

作者: Jin110 | 来源:发表于2019-03-23 00:21 被阅读0次

1.添加事件

DELIMITER $
DROP TRIGGER IF EXISTS m_add_up_customer_num;
create trigger m_add_up_customer_num after insert
on me_table for each row
begin

UPDATE customer SET num = num + 1 WHERE customer_id = new.customer_id;

end$
DELIMITER ; 

2.修改事件

DELIMITER $
DROP TRIGGER IF EXISTS m_up_update_customer_num;
create trigger m_up_update_customer_num after update
on me_table for each row
begin

IF new.is = -1
THEN

UPDATE customer SET num = num - 1 WHERE customer_id = new.customer_id;

END IF;
end$
DELIMITER ;

3.删除事件

DELIMITER $
DROP TRIGGER IF EXISTS m_del_update_customer_num;
create trigger m_del_update_customer_num after DELETE
on me_table for each row
begin

IF  old.is = 1
THEN

UPDATE customer SET num = num - 1 WHERE customer_id = old.customer_id;

END IF;

end$
DELIMITER ; 

相关文章

网友评论

      本文标题:MySql触发器

      本文链接:https://www.haomeiwen.com/subject/kyfjvqtx.html