Auto commit
This commit is contained in:
parent
02788e2bb2
commit
6aa71ed621
@ -264,6 +264,28 @@ VALUES(DEFAULT, '雷昊', 60000.00, NOW(), 1, NULL);
|
||||
|
||||
创建一个触发器 `before_employee_update`,在更新 `employee` 表数据时,防止修改 `hire_date` 字段。如果尝试修改,抛出错误。
|
||||
|
||||
```sql
|
||||
DELIMITER $$
|
||||
DROP TRIGGER IF EXISTS before_employee_update;
|
||||
CREATE TRIGGER before_employee_update
|
||||
BEFORE UPDATE ON employee
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF NEW.hire_date <> OLD.hire_date THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT='不允许修改用户的入职时间';
|
||||
END IF;
|
||||
END $$
|
||||
DELIMITER ;
|
||||
UPDATE employee e
|
||||
SET e.hire_date = '2025-3-20'
|
||||
WHERE e.emp_id = 1;
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#### 练习题 7: 自动计算工龄
|
||||
|
||||
创建一个触发器 `before_employee_insert`,在插入 `employee` 表数据时,自动计算员工的工龄(以年为单位),并将其存储到一个新字段 `years_of_service` 中。
|
||||
|
Loading…
x
Reference in New Issue
Block a user