首页 > 代码库 > MySQL 触发器学习

MySQL 触发器学习

DROP TRIGGER IF EXISTS t_a_b;    /*删除触发器*/

CREATE TRIGGER t_a_b             /*创建触发器 t_a_b*/
AFTER INSERT ON a                /*标识触发时机,取值为 BEFORE 或 AFTER;*/
                                 /*由此可见,可以建立6种触发器,即:BEFORE INSERT、BEFORE UPDATE、BEFORE DELETE、AFTER INSERT、AFTER UPDATE、
                   AFTER DELETE。
*/ FOR EACH ROW BEGIN /*触发器程序体,可以是一句SQL语句,或者用 BEGIN 和 END 包含的多条语句。*/ INSERT INTO b (NAME) /*表b新增name,值是a的那么的值*/ VALUES (new.name); END; INSERT INTO a(NAME) values("001"); DROP TRIGGER IF EXISTS d_a_b; CREATE TRIGGER d_a_b AFTER DELETE ON a FOR EACH ROW BEGIN DELETE FROM b WHERE name=old.name; END; DELETE FROM a WHERE name="001";

 

MySQL 触发器学习