Auto commit
This commit is contained in:
parent
5072cd79b1
commit
37fccb1898
@ -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` 字段。如果尝试修改,抛出错误。
|
||||
|
Loading…
x
Reference in New Issue
Block a user