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 ;
网友评论