本文主要向大家介绍了MySQL数据库入门的mysql触发器的一个小例子,通过具体的实例让大家了解,希望对大家学习MySQL数据库入门有所帮助。
触发器是一个事务.
当某时间点;某个表执行了某个操作,将引起的一系列事件.这一整套事务.便是一个触发器.
2.语法:
CREATE TRIGGER trigger_name
trigger_time
trigger_event ON tbl_name
FOR EACH ROW
trigger_stmt
trigger_name:标识触发器名称,用户自行指定; trigger_time:标识触发时机,取值为 BEFORE 或 AFTER; trigger_event:标识触发事件,取值为 INSERT、UPDATE 或 DELETE; tbl_name:标识建立触发器的表名,即在哪张表上建立触发器; trigger_stmt:触发器程序体,可以是一句SQL语句,或者用 BEGIN 和 END 包含的多条语句。
模拟事件(当订单发生变化时;商品也要随之发生变化)
首先创建订单表
create table goods_order(order_id int ,goods_id int,purchase_number int);
创建商品表
create table goods(goods_id int,goods_name varchar(20),goods_number int);
给商品表放一些数据
insert into goods values (1,'cat',23);
insert into goods values (2,'dog',23);
insert into goods values (3,'pig',23);
insert into goods values (4,'horse',23);
insert into goods values (5,'cow',23);
insert into goods values (6,'bird',23);
insert into goods values (7,'fish',23);
有人订了6头猪
insert into goods_order values (1,3,6);
引起的事务:商品猪的数量减少6头.
update goods t set t.goods_number=t.goods_number-6 where t.goods_id=3;
将以上事务替换为trigger
需要优化的事项:将3和6改换成变量,通过插入订单的记录中的数据来赋值 定义trigger时,出现的两个语句结束符; 如何解决这个冲突问题? 自定义一个结束符$,解决语句结束符冲突问题
delimiter $;
create trigger make_an_order
after
insert
on goods_order
for each row
begin
update goods set goods_number=goods_number-6 where goods_id=3;
end$
--查看trigger
show triggers$
show triggers \G
如何使用触发器?
当insert + goods_order就会自动触发.
好,试水试水~
insert into goods_order values (1,3,6);
结果:mysql> insert into goods_order values (1,3,6)$
Query OK, 1 row affected (0.00 sec)
mysql> select * from goods;
-> \c
mysql> select * from goods$
+----------+------------+--------------+
| goods_id | goods_name | goods_number |
+----------+------------+--------------+
| 1 | cat | 23 |
| 2 | dog | 23 |
| 3 | pig | 11 |
| 4 | horse | 23 |
| 5 | cow | 23 |
| 6 | bird | 23 |
| 7 | fish | 23 |
+----------+------------+--------------+
nice
接下来就要处理调变量的问题
mysql把insert的记录定义为new mysql把delete的记录定义为old 通过new,old记录来获取目标字段则可以优化触发器make_an_ordercreate trigger make_an_nice_order
after
insert
on goods_order
for each row
begin
update goods set goods_number=goods_number-new.purchase_number where goods_id=new.goods_id;
end$
执行上面的语句会发生错误,因为两个触发器在同一时间(after)对同一个人(table goods_order)执行同一件事(insert),那么当执行insert事件时,会触发哪一个触发器呢,第一个?第二个?还是两个都触发?这里两个触发器之间明显存在冲突.因此,需要先把第一个触发器删掉.
drop trigger make_an_order$
接着,继续试水~
insert into goods_order values (1,2,4)$
结果:mysql> insert into goods_order values (1,2,4)$
Query OK, 1 row affected (0.01 sec)
mysql> select * from goods$
+----------+------------+--------------+
| goods_id | goods_name | goods_number |
+----------+------------+--------------+
| 1 | cat | 23 |
| 2 | dog | 19 |
| 3 | pig | 11 |
| 4 | horse | 23 |
| 5 | cow | 23 |
| 6 | bird | 23 |
| 7 | fish | 23 |
+----------+------------+--------------+
7 rows in set (0.00 sec)
mysql> select * from goods_order$
+----------+----------+-----------------+
| order_id | goods_id | purchase_number |
+----------+----------+-----------------+
| 1 | 3 | 6 |
| 1 | 3 | 6 |
| 1 | 2 | 4 |
+----------+----------+-----------------+
3 rows in set (0.00 sec)
恩~,完美
关于update
多讲无益,来一发试试水验验货
假设有那么一个功能需求:你的购物车中订单可以调节商品的数量
需要满足的条件:必须是同一种商品
那么原来的订单记录就是old
后来的订单记录就是new
他们之间的差值就是(new.purchase_number-old.purchase_number)
以实际的数字举个栗子
原先下的订单数量为4
后来更改数量为6
差值=6-4
create trigger update_orders
after
update
on goods_order
for each row
begin
update goods set goods_number=goods_number-(new.purchase_number-old.purchase_number) where goods_id=new.goods_id;
end$
update goods_order t set t.purchase_number=3 where t.goods_id=2$
结果:mysql> select * from goods$
+----------+------------+--------------+
| goods_id | goods_name | goods_number |
+----------+------------+--------------+
| 1 | cat | 23 |
| 2 | dog | 19 |
| 3 | pig | 11 |
| 4 | horse | 23 |
| 5 | cow | 23 |
| 6 | bird | 23 |
| 7 | fish | 23 |
+----------+------------+--------------+
7 rows in set (0.00 sec)
mysql> update goods_order t set t.purchase_number=3 where t.goods_id=2$
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from goods$
+----------+------------+--------------+
| goods_id | goods_name | goods_number |
+----------+------------+--------------+
| 1 | cat | 23 |
| 2 | dog | 20 |
| 3 | pig | 11 |
| 4 | horse | 23 |
| 5 | cow | 23 |
| 6 | bird | 23 |
| 7 | fish | 23 |
+----------+------------+--------------+
7 rows in set (0.00 sec)
恩,完美~
本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!