Auto commit
This commit is contained in:
parent
95aa201cf4
commit
02788e2bb2
@ -241,6 +241,25 @@ WHERE emp_id = 20;
|
||||
|
||||
创建一个触发器 `before_employee_insert`,在插入 `employee` 表数据时,如果未指定 `job_id`,则自动将其设置为 `job` 表中的默认职位(假设默认职位的 `job_id` 为 1)。
|
||||
|
||||
```sql
|
||||
DELIMITER $$
|
||||
DROP TRIGGER IF EXISTS before_employee_insert;
|
||||
CREATE TRIGGER before_employee_insert
|
||||
BEFORE INSERT ON employee
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.job_id IS NULL THEN
|
||||
SET NEW.job_id = 1;
|
||||
END IF;
|
||||
END $$
|
||||
DELIMITER ;
|
||||
|
||||
INSERT INTO employee
|
||||
VALUES(DEFAULT, '雷昊', 60000.00, NOW(), 1, NULL);
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 练习题 6: 防止修改入职日期
|
||||
|
||||
创建一个触发器 `before_employee_update`,在更新 `employee` 表数据时,防止修改 `hire_date` 字段。如果尝试修改,抛出错误。
|
||||
|
Loading…
x
Reference in New Issue
Block a user