diff --git a/2207/触发器练习题.md b/2207/触发器练习题.md index 490a2ec..9fbb58b 100644 --- a/2207/触发器练习题.md +++ b/2207/触发器练习题.md @@ -407,6 +407,29 @@ DELIMITER ; 创建一个触发器 `after_employee_insert`,在插入 `employee` 表数据之后,自动更新一个统计表 `employee_statistics` 中的员工总数。 +```sql +CREATE TABLE employee_statistics( + marking VARCHAR(20), + num INT +); +INSERT INTO employee_statistics() +VALUES('count', 0); + +DELIMITER $$ +DROP TRIGGER IF EXISTS after_employee_insert; +CREATE TRIGGER after_employee_insert +AFTER INSERT ON employee +FOR EACH ROW +BEGIN + UPDATE employee_statistics + SET num = (SELECT COUNT(*) FROM employee) + WHERE marking = 'count'; +END $$ +DELIMITER ; +``` + + + #### 练习题 13: 防止修改部门所在地 创建一个触发器 `before_department_update`,在更新 `department` 表数据时,防止修改 `location` 字段。如果尝试修改,抛出错误。