Auto commit
This commit is contained in:
parent
6733da3ec5
commit
26b0159c32
@ -181,6 +181,35 @@ VALUES(DEFAULT, '雷昊', 60000.00, NOW(), 1, 1);
|
||||
|
||||
创建一个触发器 `after_employee_update`,在更新 `employee` 表的 `salary` 字段后,将变更记录插入到一个新的日志表 `salary_log` 中,记录员工 ID、旧工资、新工资和变更时间。
|
||||
|
||||
```sql
|
||||
DROP TABLE IF EXISTS salary_log;
|
||||
CREATE TABLE salary_log(
|
||||
emp_id INT,
|
||||
old_salary DECIMAL(10, 2),
|
||||
new_slaray DECIMAL(10, 2),
|
||||
update_date DATE
|
||||
);
|
||||
|
||||
DELIMITER $$
|
||||
DROP TRIGGER IF EXISTS after_employee_update;
|
||||
CREATE TRIGGER after_employee_update
|
||||
BEFORE UPDATE ON employee
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.salary <> OLD.salary THEN
|
||||
INSERT INTO salary_log()
|
||||
VALUES(NEW.emp_id, OLD.salary, NEW.salary, NOW());
|
||||
END IF;
|
||||
END $$
|
||||
DELIMITER ;
|
||||
|
||||
UPDATE employee e
|
||||
SET e.salary = 10
|
||||
WHERE e.emp_id = 1;
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 练习题 4: 删除员工时备份数据
|
||||
|
||||
创建一个触发器 `before_employee_delete`,在删除 `employee` 表数据之前,将被删除的员工数据插入到一个备份表 `employee_backup` 中。
|
||||
|
Loading…
x
Reference in New Issue
Block a user