MySQL 触发器 前言 一、什么是触发器 二、触发器 实战案例 1、学生多表 2、多表信息同步更新(增加、跟新、删除) 前言 本环境是基于 C
MySQL 触发器
- 前言
- 一、什么是触发器
- 二、触发器 实战案例
- 1、学生多表
- 2、多表信息同步更新(增加、跟新、删除)
前言
本环境是基于 Centos 7.8 系统构建MySQL-5.7.14
具体构建,请参考 MySQL-5.7.14 环境构建
经常要对emp和dept表进行连接查询,每次都要做表的连接,写同样的一串语句,同时由于工资列队数据比较敏感,对外要求不可见。
这样,我们可以通过创建视图来完成
一、什么是触发器
触发器(trigger)是一个特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触发。
当对一个表进行操作( insert,delete, update)时就会激活它执行。触发器经常用于加强数据的完整性约束和业务规则等。
例如,当学生表中增加了一个学生的信息时,学生的总数就应该同时改变。因此可以针对学生表创建一个触发器,每次增加或删除一个学生记录时,就执行一次学生总数的计算操作,从而保证学生总数与记录数的一致性。
触发器(trigger)是个特殊的存储过程,不同的是执行存储过程要使用CALL语句来调用,
而触发器的执行不需要使用CALL语句来调用,也不需要手工启动,只要当一个预定义的事件发生的时候,就会被MySQL自动调用。
触发程序的优点如下:
- 触发程序的执行是自动的。当对触发程序相关表的数据做出相应的修改后立即执行。
- 触发程序可以通过数据库中相关的表进行层叠修改另外的表。
- 触发程序可以实施比FOREIGN KEY约束、CHECK约束更为复杂的检查和操作。
二、触发器 实战案例
1、学生多表
信息同步跟新(增加、删除)
创建触发器
准备两张表:student、student_total
当student表中增加或者删除学生信息时,student_total统计的数字也随之更新
# 创建student
mysql> create table student
-> (id int unsigned auto_increment primary key,
-> name varchar(50));
# 创建student_total
mysql> create table student_total(total int);
# 两张表中分别插入数据
mysql> insert into student(name) values('z3');
mysql> insert into student_total values(1);
mysql> select * from student;
+----+------+
| id | name |
+----+------+
| 1 | z3 |
+----+------+
1 row in set (0.00 sec)
mysql> select * from student_total;
+-------+
| total |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
# 创建学生信息增加 触发器
mysql> \d $$
mysql> create trigger student_insert after insert
-> on student for each row
-> begin
-> update student_total set total=total+1;
-> end$$
mysql> \d ;
# 创建学生信息删除 触发器
mysql> \d $$
mysql> create trigger student_delete after delete
-> on student for each row
-> begin
-> update student_total set total=total-1;
-> end$$
mysql> \d ;
# 增加学生时
mysql> insert into student(name)
-> values('l4');
mysql> select * from student;
+----+------+
| id | name |
+----+------+
| 1 | z3 |
| 2 | l4 |
+----+------+
2 rows in set (0.00 sec)
mysql> select * from student_total;
+-------+
| total |
+-------+
| 2 |
+-------+
1 row in set (0.00 sec)
# 删除学生时
mysql> delete from student
-> where id=1;
mysql> select * from student;
+----+------+
| id | name |
+----+------+
| 2 | l4 |
+----+------+
1 row in set (0.00 sec)
mysql> select * from student_total;
+-------+
| total |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
查看创建的触发器
# 查看创建的触发器
mysql> show triggers\G
*************************** 1. row ***************************
Trigger: student_insert
Event: INSERT
Table: student
Statement: begin
update student_total set total=total+1;
end
Timing: AFTER
Created: 2021-02-03 15:05:38.49
sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
*************************** 2. row ***************************
Trigger: student_delete
Event: DELETE
Table: student
Statement: begin
update student_total set total=total-1;
end
Timing: AFTER
Created: 2021-02-03 15:21:11.06
sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: utf8_general_ci
2 rows in set (0.00 sec)
mysql> select * from information_schema.triggers where TRIGGER_NAME='student_insert' or TRIGGER_NAME='student_delete'\G
*************************** 1. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: db
TRIGGER_NAME: student_insert
EVENT_MANIPULATION: INSERT
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: db
EVENT_OBJECT_TABLE: student
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: begin
update student_total set total=total+1;
end
ACTION_ORIENTATION: ROW
ACTION_TIMING: AFTER
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2021-02-03 15:05:38.49
SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
DEFINER: root@localhost
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
DATABASE_COLLATION: utf8_general_ci
*************************** 2. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: db
TRIGGER_NAME: student_delete
EVENT_MANIPULATION: DELETE
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: db
EVENT_OBJECT_TABLE: student
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: begin
update student_total set total=total-1;
end
ACTION_ORIENTATION: ROW
ACTION_TIMING: AFTER
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2021-02-03 15:21:11.06
SQL_MODE: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
DEFINER: root@localhost
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
DATABASE_COLLATION: utf8_general_ci
2 rows in set (0.00 sec)
删除触发器
mysql> drop trigger student_insert;
Query OK, 0 rows affected (0.00 sec)
mysql> drop trigger student_delete;
Query OK, 0 rows affected (0.00 sec)
mysql> show triggers\G
Empty set (0.00 sec)
2、多表信息同步更新(增加、跟新、删除)
# 创建tb1、tb2表
mysql> create table tb1
-> (id int primary key auto_increment,
-> name varchar(50),
-> gender enum('男','女') default '男',
-> age int);
mysql> create table tb2
-> (id int primary key auto_increment,
-> name varchar(50),
-> salary double(10,2)
-> );
# 分别创建插入、更新、删除的触发器
mysql> create trigger tb1_after_insert
-> after insert
-> on tb1 for each row
-> begin
-> insert into tb2(name,salary) values(new.name,8000);
-> end$$
mysql> create trigger tb1_after_update
-> after update
-> on tb1 for each row
-> begin
-> update tb2 set name=new.name where name=old.name;
-> end$$
mysql> create trigger tb1_after_delete
-> after delete
-> on tb1 for each row
-> begin
-> delete from tb2 where name=old.name;
-> end$$
#
# 测试
---tb1插入三条记录
mysql> insert into tb1(name,age) values('张三','16');
mysql> insert into tb1(name,age) values('李四','22');
mysql> insert into tb1(name,age) values('王五','32');
---tb1、tb2均插入信息
mysql> select * from tb1;
+----+--------+--------+------+
| id | name | gender | age |
+----+--------+--------+------+
| 1 | 张三 | 男 | 16 |
| 2 | 李四 | 男 | 22 |
| 3 | 王五 | 男 | 32 |
+----+--------+--------+------+
3 rows in set (0.00 sec)
mysql> select * from tb2;
+----+--------+---------+
| id | name | salary |
+----+--------+---------+
| 1 | 张三 | 8000.00 |
| 2 | 李四 | 8000.00 |
| 3 | 王五 | 8000.00 |
+----+--------+---------+
3 rows in set (0.00 sec)
# 修改id=2的学生name
mysql> update tb1
-> set name='老六'
-> where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tb1;
+----+--------+--------+------+
| id | name | gender | age |
+----+--------+--------+------+
| 1 | 张三 | 男 | 16 |
| 2 | 老六 | 男 | 22 |
| 3 | 王五 | 男 | 32 |
+----+--------+--------+------+
3 rows in set (0.00 sec)
mysql> select * from tb2;
+----+--------+---------+
| id | name | salary |
+----+--------+---------+
| 1 | 张三 | 8000.00 |
| 2 | 老六 | 8000.00 |
| 3 | 王五 | 8000.00 |
+----+--------+---------+
3 rows in set (0.00 sec)
mysql> delete from tb1
-> where id=3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb1;
+----+--------+--------+------+
| id | name | gender | age |
+----+--------+--------+------+
| 1 | 张三 | 男 | 16 |
| 2 | 老六 | 男 | 22 |
+----+--------+--------+------+
2 rows in set (0.01 sec)
mysql> select * from tb2;
+----+--------+---------+
| id | name | salary |
+----+--------+---------+
| 1 | 张三 | 8000.00 |
| 2 | 老六 | 8000.00 |
+----+--------+---------+
2 rows in set (0.00 sec)