触发器
-- 需求: 当向员工表插入一条记录时,希望mysql自动同时往日志表插入数据 -- 创建触发器(添加) CREATE TRIGGER tri_empAdd AFTER INSERT ON employee FOR EACH ROW -- 当往员工表插入一条记录时 INSERT INTO test_log(content) VALUES('员工表插入了一条记录');
-- 插入数据 INSERT INTO employee(id,empName,deptId) VALUES(7,'扎古斯',1); INSERT INTO employee(id,empName,deptId) VALUES(8,'扎古斯2',1);
-- 创建触发器(修改) CREATE TRIGGER tri_empUpd AFTER UPDATE ON employee FOR EACH ROW -- 当往员工表修改一条记录时 INSERT INTO test_log(content) VALUES('员工表修改了一条记录');
-- 修改 UPDATE employee SET empName='eric' WHERE id=7;
-- 创建触发器(删除) CREATE TRIGGER tri_empDel AFTER DELETE ON employee FOR EACH ROW -- 当往员工表删除一条记录时 INSERT INTO test_log(content) VALUES('员工表删除了一条记录');
-- 删除 DELETE FROM employee WHERE id=7; |