MYSQL 触发器
Posted 一只小白呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL 触发器相关的知识,希望对你有一定的参考价值。
#触发器
#创建基本语法
#插入前
CREATE trigger tri_before_insert_tb1 BEFORE INSERT ON tb1 FOR EACH ROW
BEGIN
...
END
#插入后
CREATE TRIGGER tri_after_insert_tb1 AFTER INSERT ON tb1 FOR EACH ROW
BEGIN
...
END
#删除前
CREATE TRIGGER tri_before_delete_tb1 BEFORE DELETE tb1 FOR EACH ROW
BEGIN
...
END
#删除后
CREATE TRIGGER tri_after_delete_tb1 AFTER DELETE tb1 FOR EACH ROW
BEGIN
...
END
#更新前
CREATE TRIGGER tri_beofre_update_tb1 BEFORE UPDATE ON tb1 FOR EACH ROW
BEGIN
...
END
#更新后
CREATE TRIGGER tri_after_update_tb1 AFTER UPDATE ON tb1 FOR EACH ROW
BEGIN
...
END
#插入前触发器
delimiter //
CREATE TRIGGER tri_before_insert_tb1 BEFORE INSERT ON tb1 FOR EACH ROW
BEGIN
IF NEW.NAME == ‘alex‘ THEN
VAlUES
(‘aa‘)
END
END //
delimiter;
#插入后触发器
delimiter //
CREATE TRIGGER tri_before_insert_tb1 AFTER INSERT ON tb1 FOR EACH ROW
BEGIN
IF NEW.num = 666 THEN
INSERT INTO tb2 (NAME)
VAlUES
(‘666‘),
(‘666‘);
ELSE IF NEW.num = 555 THEN
INSERT INTO tb2 (NAME)
VAlUES
(‘555‘),
(‘555‘);
END IF;
END//
delimiter;
#删除触发器
DROP TRIGGER tri_after_insert_tb1;
#使用触发器
insert into tb1(num) values(666);
以上是关于MYSQL 触发器的主要内容,如果未能解决你的问题,请参考以下文章