diff --git a/2207/触发器练习题.md b/2207/触发器练习题.md index 7f08032..4a8d963 100644 --- a/2207/触发器练习题.md +++ b/2207/触发器练习题.md @@ -589,5 +589,22 @@ VALUES(DEFAULT, '雷昊', 0, NOW(), 1, 1, NULL, NULL); 创建一个触发器 `after_employee_delete`,在删除 `employee` 表数据之后,将被删除的员工信息插入到一个离职记录表 `employee_exit_log` 中,记录员工 ID、姓名、离职时间和原因(假设原因由用户输入)。 ```sql +CREATE TABLE employee_exit_log( + emp_id INT, + emp_name VARCHAR(20), + quit_date DATE, + quit_reason VARCHAR(50) +); + +DELIMITER $$ +DROP TRIGGER IF EXISTS after_employee_delete; +CREATE TRIGGER after_employee_delete +AFTER DELETE ON employee +FOR EACH ROW +BEGIN + INSERT INTO employee_exit_log() + VALUES(OLD.emp_id, OLD.emp_name, NOW(), NULL); +END $$ +DELIMITER ; ```