# 增 Create CREATE TRIGGER 触发器名 触发时机 触发事件 ON 表名 FOR EACH ROW 触发器程序体 # 查 Retrieve SHOW TRIGGERS # 改 Update ALTER TRIGGER 触发器名 触发时机 触发事件 ON 表名 FOR EACH ROW 触发器程序体 # 删 Delete DROP TRIGGER trigger_name
【NEW 和 OLD】
NEW 对象:SQL语句执行之前的记录对象
OLD 对象:SQL语句执行之后的记录对象
【基本使用】
当需要在操作 user 表的时候做独立的数据库操作记录,并备份 user 表的数据 —— Demo1
# 创建 user 表:id 主键,自增;name 不允许空 DROP TABLE IF EXISTS `user`; CREATE TABLE `user`( `id` INT PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(20) NOT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8;
# 插入测试数据 INSERT INTO `user`(NAME) VALUES('jack'); INSERT INTO `user`(NAME) VALUES('rose'); INSERT INTO `user`(NAME) VALUES('tom'); INSERT INTO `user`(NAME) VALUES('jerry');
SELECT `id`,`name` FROM `user`;
# 创建 user 表对应的备份表 user_log,记录 user 表中所有 DML 操作被触发器语句触发而记录的内容 DROP TABLE IF EXISTS `user_log`; CREATE TABLE `user_log` ( `id` INT(10) NOT NULL AUTO_INCREMENT, `create_time` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, # 该字段数据有变化的时候自动更新该时间 `current_user` VARCHAR(20) DEFAULT NULL, `operation` VARCHAR(64) DEFAULT NULL, `name` VARCHAR(64) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8;
SELECT * FROM `user_log`;
# -- 【创建 INSERT 触发器】 -- # DROP TRIGGER IF EXISTS `user_log_insert`; DELIMITER $ # 设置 sql 语句的结束符为 $ CREATE TRIGGER user_log_insert AFTER INSERT ON `user` FOR EACH ROW BEGIN DECLARE s1 VARCHAR(64) CHARACTER SET utf8; # 语句①:定义变量 s1,后面发现中文字符编码出现乱码时设置字符集 SET s1 = "新增"; # 语句②:为 s1 赋值 INSERT INTO user_log(`current_user`, `operation`, `name`) VALUES(USER(), s1, NEW.name); # 语句③:NEW 关键字,代表即将插入 user 表的数据 END $ # 此时 CREATE 语句到此为一整句的结束 DELIMITER ; # 恢复 sql 语句的结束符为默认的分号 ;
# 显示所有触发器 SHOW TRIGGERS;
# 插入一条数据,并查询备份表,测试触发器是否产生记录 INSERT INTO `user` (NAME) VALUES ('周杰伦'); SELECT * FROM `user`; SELECT * FROM `user_log`;
# -- 【创建 DELETE 触发器】 -- # DROP TRIGGER IF EXISTS `user_log_delete`; DELIMITER $ CREATE TRIGGER user_log_delete AFTER DELETE ON `user` FOR EACH ROW BEGIN DECLARE s1 VARCHAR(64) CHARACTER SET utf8; # 语句①:定义变量 s1,后面发现中文字符编码出现乱码时设置字符集 SET s1 = "删除"; # 语句②:为 s1 赋值 INSERT INTO user_log(`current_user`, `operation`, `name`) VALUES(USER(), s1, OLD.name); # 语句③:OLD 关键字,代表未删除之前 user 表的数据 END $ DELIMITER ;
# 显示所有触发器 SHOW TRIGGERS;
# 删除一条数据,并查询备份表,测试触发器是否产生记录 DELETE FROM `user` WHERE `name`='周杰伦'; SELECT * FROM `user`; SELECT * FROM `user_log`;
# -- 【创建 UPDATE 触发器】 -- # DROP TRIGGER IF EXISTS `user_log_update`; DELIMITER $ CREATE TRIGGER user_log_update AFTER UPDATE ON `user` FOR EACH ROW BEGIN DECLARE s1 VARCHAR(64) CHARACTER SET utf8; SET s1 = "更新"; INSERT INTO user_log(`current_user`, `operation`, `name`) VALUES(USER(), s1, CONCAT(OLD.name, ' -> ', NEW.name)); END $ DELIMITER ;
# 显示所有触发器 SHOW TRIGGERS;
# 更新一条数据,并查询备份表,测试触发器是否产生记录 UPDATE `user` SET NAME='jay' WHERE id=1; SELECT * FROM `user`; SELECT * FROM `user_log`;
# 创建班级表 DROP TABLE IF EXISTS `class`; CREATE TABLE `class`( `cid` INT PRIMARY KEY AUTO_INCREMENT, `cname` VARCHAR(30) DEFAULT NULL, `num` INT DEFAULT 0 ) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO class VALUES(1,"小班",0); INSERT INTO class VALUES(2,"中班",0); INSERT INTO class VALUES(3,"大班",0);
SELECT * FROM class;
# 创建学生表 DROP TABLE IF EXISTS `student`; CREATE TABLE `student`( `sid` INT PRIMARY KEY AUTO_INCREMENT, `sname` VARCHAR(30) DEFAULT NULL, `age` INT DEFAULT 0, `cno` INT DEFAULT 0 ) ENGINE=INNODB DEFAULT CHARSET=utf8;
SELECT * FROM student;
# 学生表添加一条学生记录, 班级表字段 num 加 1 DROP TRIGGER IF EXISTS `trigger_add`; DELIMITER $ CREATE TRIGGER trigger_add AFTER INSERT ON student FOR EACH ROW BEGIN UPDATE class SET num = num + 1 WHERE cid = NEW.cno; END $ DELIMITER ;
SHOW TRIGGERS;
INSERT INTO student VALUES(1, "小三", 3, 1); SELECT * FROM class; SELECT * FROM student; INSERT INTO student VALUES(2, "小四", 4, 1); INSERT INTO student VALUES(3, "小五", 5, 1);
# 学生表删除一条学生记录, 班级表字段 num 减1 DROP TRIGGER IF EXISTS `trigger_sub`; DELIMITER $ CREATE TRIGGER trigger_sub AFTER DELETE ON student FOR EACH ROW BEGIN UPDATE class SET num = num - 1 WHERE cid = OLD.cno; END $ DELIMITER ;
SHOW TRIGGERS;
DELETE FROM student WHERE sname = "小三"; SELECT * FROM class; SELECT * FROM student;