Auto commit
This commit is contained in:
parent
983e28df10
commit
2cafec9ae9
@ -358,6 +358,30 @@ DELIMITER ;
|
||||
|
||||
创建一个触发器 `after_employee_update`,在更新 `employee` 表的 `dept_id` 字段后,将变更记录插入到一个新的日志表 `department_change_log` 中,记录员工 ID、旧部门 ID、新部门 ID 和变更时间。
|
||||
|
||||
```sql
|
||||
CREATE TABLE department_change_log(
|
||||
emp_id INT,
|
||||
old_dept_id INT,
|
||||
new_dept_id INT,
|
||||
update_date DATE
|
||||
);
|
||||
|
||||
DELIMITER $$
|
||||
DROP TRIGGER IF EXISTS after_employee_update;
|
||||
CREATE TRIGGER after_employee_update
|
||||
AFTER UPDATE ON employee
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.dept_id <> OLD.dept_id THEN
|
||||
INSERT INTO department_change_log()
|
||||
VALUES(NEW.emp_id, OLD.dept_id, NEW.dept_id, NOW());
|
||||
END IF;
|
||||
END $$
|
||||
DELIMITER ;
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 练习题 11: 防止插入重复职位
|
||||
|
||||
创建一个触发器 `before_job_insert`,在插入 `job` 表数据之前,检查职位名称是否已存在。如果存在,阻止插入并抛出错误。
|
||||
|
Loading…
x
Reference in New Issue
Block a user