Auto commit
This commit is contained in:
parent
2cafec9ae9
commit
5072cd79b1
@ -386,6 +386,23 @@ DELIMITER ;
|
||||
|
||||
创建一个触发器 `before_job_insert`,在插入 `job` 表数据之前,检查职位名称是否已存在。如果存在,阻止插入并抛出错误。
|
||||
|
||||
```sql
|
||||
DELIMITER $$
|
||||
DROP TRIGGER IF EXISTS before_job_insert;
|
||||
CREATE TRIGGER before_job_insert
|
||||
BEFORE INSERT ON job
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
IF (SELECT COUNT(*) FROM job WHERE job_id = NEW.job_id) = 1 THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = '职位名称已存在';
|
||||
END IF;
|
||||
END $$
|
||||
DELIMITER ;
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 练习题 12: 自动更新员工总数
|
||||
|
||||
创建一个触发器 `after_employee_insert`,在插入 `employee` 表数据之后,自动更新一个统计表 `employee_statistics` 中的员工总数。
|
||||
|
Loading…
x
Reference in New Issue
Block a user