首页 > 代码库 > d_2_MYSQL晋级之触发器

d_2_MYSQL晋级之触发器

mysql表设计网址:王李-MySQL表设计

0.什么时候会用到触发器

百度解释:百度百科-触发器

个人理解:

触发器就是我们预先定义好的触发任务,当我们变动表的时候,正好符合触发器的规则,那就执行触发器。

1.触发器的格式

针对于DML触发器:

1 CREATE TRIGGER TRIGGER_NAME
2 BEFORE/ALTER INSERT/UPDATE/DELETE TB_NAME
3 FOR EACH NOW
4 BEGIN
5 ...
6 END

这里稍微解释一下:

1 1.触发器分为before和alter之分,before是在数据改变前,alter在数据改变后,最大的不同就是before可以改变new的值,而alter则不会
2 2.监视的动作也分为三种,INSERT UPDATEDELETE ,意思是当表做这些动作的时候就执行下面的语句
3 3.for each now 就是每改变一行都会去检查一下触发器的条件
4 4.begin 这就意味着下面就是执行的语句了
5 5.end 这就意味着触发器结束了

贴几个关于mysql 触发器的教程:触发器教程

2.触发器的实际应用

修改MySQL默认执行结束符

1 delimiter $

 

关联文章:王李-MySQL表设计

 2.1 实现功能:在增加b_book_information数据的时候,每增加一条,就会向表b_book_amount添加一行数据,数据的内容是书本编号和书本册书

1 mysql> create trigger t_insert_book_amount
2     -> after insert on b_book_information
3     -> for each row
4     -> begin
5     -> insert into b_book_amount (book_id,book_count) values (new.book_id,10);
6     -> end $
7 Query OK, 0 rows affected (0.08 sec)

2.2 实现功能:我删除b_book_information数据的时候,会自动删除b_book_amount对应书本编号的数据

1 mysql> create trigger t_delete_book_amount
2     -> after delete on b_book_information
3     -> for each row
4     -> begin
5     -> delete from b_book_amount where book_id = old.book_id;
6     -> end$
7 Query OK, 0 rows affected (0.06 sec)

2.3 每删除b_borrow_info表一条数据的时候,就会向b_book_amount添加该书本编号对应的册数,使其加1

1 mysql> create trigger t_delete_borrow
2     -> after delete on b_borrow_info
3     -> for each row
4     -> begin
5     -> update b_book_amount set book_count = book_count + 1 where book_id = old.book_id;
6     -> end$
7 Query OK, 0 rows affected (0.06 sec)

 

3.检查触发器

2.1 检查

插入之前检查

1 mysql> select * from b_book_information where book_id = d1112$
2 Empty set (0.01 sec)
3 
4 mysql> select * from b_book_amount where book_id = d1112$
5 Empty set (0.02 sec)
6 
7 mysql>

 

插入数据

1 mysql> insert into b_book_information values (d1112,O1102,情感暴力,加藤谛三,19.00,哈佛大学心理导师情感自修课,优质畅销心理作品;别让你的爱变刀子,也被让他人的爱伤害你。停止心理"杀亲",才能爱和被爱。)$
2 Query OK, 1 row affected (0.11 sec)

 

插入之后检查

 1 mysql> select * from b_book_information where book_id = d1112$
 2 +---------+-------------+-----------+-------------+------------+----------------------------------------------------------------------------------------------------------------------+
 3 | book_id | category_id | book_name | book_author | book_price | book_remask                                                                                                          |
 4 +---------+-------------+-----------+-------------+------------+----------------------------------------------------------------------------------------------------------------------+
 5 | d1112   | O1102       | 情感暴力  | 加藤谛三    |      19.00 | 哈佛大学心理导师情感自修课,优质畅销心理作品;别让你的爱变刀子,也被让他人的爱伤害你。停止心理"杀亲",才能爱和被爱。 |
 6 +---------+-------------+-----------+-------------+------------+----------------------------------------------------------------------------------------------------------------------+
 7 1 row in set (0.02 sec)
 8 
 9 mysql> select * from b_book_amount where book_id = d1112$
10 +---------+------------+
11 | book_id | book_count |
12 +---------+------------+
13 | d1112   |         10 |
14 +---------+------------+
15 1 row in set (0.00 sec)
16 
17 mysql>

 

2.2 检查

现在将删除2.1中添加的book_id为‘d1112‘的数据

1 mysql> delete from b_book_information where book_id = d1112$
2 Query OK, 1 row affected (0.04 sec)

 

删除之后查看数据

 1 mysql> delete from b_book_information where book_id = d1112$
 2 Query OK, 1 row affected (0.04 sec)
 3 
 4 mysql> select * from b_book_information where book_id = d1112$
 5 Empty set (0.00 sec)
 6 
 7 mysql> select * from b_book_amount where book_id = d1112$
 8 Empty set (0.00 sec)
 9 
10 mysql>

 

2.3 检查

查看未删除b_borrow_info之前的数据

 1 mysql> select * from b_borrow_info where user_id = D170117 and book_id = d1101$
 2 +---------+---------+-------------+-------------+
 3 | user_id | book_id | create_date | update_date |
 4 +---------+---------+-------------+-------------+
 5 | D170117 | d1101   | 2017-07-17  | NULL        |
 6 +---------+---------+-------------+-------------+
 7 1 row in set (0.00 sec)
 8 
 9 mysql> select * from b_book_amount where book_id in (select book_id from b_borrow_info where user_id = D170117 and book_id = d1101) $
10 +---------+------------+
11 | book_id | book_count |
12 +---------+------------+
13 | d1101   |         10 |
14 +---------+------------+
15 1 row in set (0.01 sec)
16 
17 mysql>

 

 删除b_borrow_info的数据

1 mysql> delete from b_borrow_info where user_id = D170117 and book_id = d1101$
2 Query OK, 1 row affected (0.06 sec)

 

删除之后查看

 1 mysql> select * from b_borrow_info where user_id = D170117 and book_id = d1101$
 2 Empty set (0.00 sec)
 3 
 4 mysql> select * from b_book_amount where book_id = d1101$
 5 +---------+------------+
 6 | book_id | book_count |
 7 +---------+------------+
 8 | d1101   |         11 |
 9 +---------+------------+
10 1 row in set (0.00 sec)
11 
12 mysql>

 

 

4.注意事项

语法注意:

1 create trigger t_name
2 before/after insert/update/delete on tn_name
3 for each now
4 begin
5 ...
6 end

 

其中ON 和 begin 是初学者最容易忘记的

END

 

d_2_MYSQL晋级之触发器