10触发器

触发器:监视某种情况,并触发某种操作

触发器创建语法四要素:1、监视地点(TABLE)

           2、监视事件(INSERT/UPDATE/DELETE)

           3、触发时间(AFTER/BEFORE)

           4、触发事件(INSERT/UPDATE/DELETE)

触发器--INSERT事件

INSERT INTO order_table(gid,much) VALUES(1,3);


CREATE TRIGGER tg1 AFTER INSERT ON order_table
FOR EACH ROW
BEGIN
    UPDATE goods SET num=num-new.much WHERE id=new.gid;
END

 触发器--UPDATE事件

UPDATE order_table SET much=much-2 WHERE oid=11;

CREATE TRIGGER tg2 AFTER UPDATE ON order_table
FOR EACH ROW
BEGIN
    UPDATE goods SET num=num+old.much-new.much WHERE id=old.gid;
END

 

触发器--DELETE事件

DELETE FROM order_table WHERE oid=11;

UPDATE goods SET num=num+1 WHERE id=1;

CREATE TRIGGER tg3 AFTER DELETE ON order_table
FOR EACH ROW
BEGIN
    UPDATE goods SET num=num+old.much WHERE id=old.gid;
END

 

posted @ 2019-02-05 20:58  余火灬  阅读(129)  评论(0编辑  收藏  举报