Mysql中的触发器简单介绍及使用案例
程序员文章站
2024-02-25 21:27:27
什么是触发器? 触发器是数据库的一个程序,他是用来监听着数据表的某个行为,一旦数据表的这个行为发生了,马上执行相应的sql语句 触发器的语法结构: create trigg...
什么是触发器?
触发器是数据库的一个程序,他是用来监听着数据表的某个行为,一旦数据表的这个行为发生了,马上执行相应的sql语句
触发器的语法结构:
create trigger 触发器的名称触发器事件 on 监听的表名 for each row 行为发生后执行的sql语句
触发器事件组成:;两部分组成:
触发器事件发生的时间-----是在监听的表的行为 after before 常用的是after
触发器执行的内容:增删改
创建order 表的时候,需要注意,因为order在mysql中是一个关键字排序,为了避免错误的发生,我们可以添加反引号,表明这不是一个关键字
案例研究:
一旦生成订单,对应的库存表要减去相应的数据
(1)建两张表 :一个商品goods表 一个订单order表
mysql> create table goods(goods_id int primary key auto_increment,goods_name var
char(64),shop_price decimal(10,2),goods_number int)engine=mysiam default charset
=utf8;
mysql> create table `order`(goods_id int primary key auto_increment,goods_name v
archar(64),buy_number int)engine=mysiam default charset=utf8;
mysql> insert into goods values(null,'nokian85',2000,35),(null,'iphone4s',4500,3
0),(null,'lnmia',5000,40),(null,'samsung',4200,20);
mysql> select*from goods;
+----------+------------+------------+--------------+
| goods_id | goods_name | shop_price | goods_number |
+----------+------------+------------+--------------+
| 1 | nokian85 | 2000.00 | 35 |
| 2 | iphone4s | 4500.00 | 30 |
| 3 | lnmia | 5000.00 | 40 |
| 4 | samsung | 4200.00 | 20 |
+----------+------------+------------+--------------+
(2)创建触发器
mysql> create trigger alter_goods_number after insert on `order` for each row up
date goods set goods_number=goods_number-5 where goods_id=1;
mysql> insert into `order` values(1,'nokian85',5);
mysql> select*from goods;
+----------+------------+------------+--------------+
| goods_id | goods_name | shop_price | goods_number |
+----------+------------+------------+--------------+
| 1 | nokian85 | 2000.00 | 30 |
| 2 | iphone4s | 4500.00 | 30 |
| 3 | lnmia | 5000.00 | 40 |
| 4 | samsung | 4200.00 | 20 |
+----------+------------+------------+--------------+
new 的使用 mysql> create trigger alter_goods_number after insert on `order` for each row up
date goods set goods_number=goods_number-new.buy_number where goods_id=new.goods
_id;
mysql> insert into `order` values(4,'samsung',5);
old 的使用
mysql> create trigger back_goods_number after delete on `order` for each row upd
ate goods set goods_number=goods_number+old.buy_number where goods_id=old.goods_
id;
mysql> delete from `order` where goods_id=1;
更新 (update将之前下的订单撤销,再重新下订单)
mysql> create trigger update_goods_number after update on `order` for each row u
pdate goods set goods_number=goods_number+old.buy_number-new.buy_number where go
ods_id=new.goods_id;
mysql> update `order` set buy_number = 10 ;
触发器是数据库的一个程序,他是用来监听着数据表的某个行为,一旦数据表的这个行为发生了,马上执行相应的sql语句
触发器的语法结构:
create trigger 触发器的名称触发器事件 on 监听的表名 for each row 行为发生后执行的sql语句
触发器事件组成:;两部分组成:
触发器事件发生的时间-----是在监听的表的行为 after before 常用的是after
触发器执行的内容:增删改
创建order 表的时候,需要注意,因为order在mysql中是一个关键字排序,为了避免错误的发生,我们可以添加反引号,表明这不是一个关键字
案例研究:
一旦生成订单,对应的库存表要减去相应的数据
(1)建两张表 :一个商品goods表 一个订单order表
mysql> create table goods(goods_id int primary key auto_increment,goods_name var
char(64),shop_price decimal(10,2),goods_number int)engine=mysiam default charset
=utf8;
mysql> create table `order`(goods_id int primary key auto_increment,goods_name v
archar(64),buy_number int)engine=mysiam default charset=utf8;
mysql> insert into goods values(null,'nokian85',2000,35),(null,'iphone4s',4500,3
0),(null,'lnmia',5000,40),(null,'samsung',4200,20);
mysql> select*from goods;
+----------+------------+------------+--------------+
| goods_id | goods_name | shop_price | goods_number |
+----------+------------+------------+--------------+
| 1 | nokian85 | 2000.00 | 35 |
| 2 | iphone4s | 4500.00 | 30 |
| 3 | lnmia | 5000.00 | 40 |
| 4 | samsung | 4200.00 | 20 |
+----------+------------+------------+--------------+
(2)创建触发器
mysql> create trigger alter_goods_number after insert on `order` for each row up
date goods set goods_number=goods_number-5 where goods_id=1;
mysql> insert into `order` values(1,'nokian85',5);
mysql> select*from goods;
+----------+------------+------------+--------------+
| goods_id | goods_name | shop_price | goods_number |
+----------+------------+------------+--------------+
| 1 | nokian85 | 2000.00 | 30 |
| 2 | iphone4s | 4500.00 | 30 |
| 3 | lnmia | 5000.00 | 40 |
| 4 | samsung | 4200.00 | 20 |
+----------+------------+------------+--------------+
new 的使用 mysql> create trigger alter_goods_number after insert on `order` for each row up
date goods set goods_number=goods_number-new.buy_number where goods_id=new.goods
_id;
mysql> insert into `order` values(4,'samsung',5);
old 的使用
mysql> create trigger back_goods_number after delete on `order` for each row upd
ate goods set goods_number=goods_number+old.buy_number where goods_id=old.goods_
id;
mysql> delete from `order` where goods_id=1;
更新 (update将之前下的订单撤销,再重新下订单)
mysql> create trigger update_goods_number after update on `order` for each row u
pdate goods set goods_number=goods_number+old.buy_number-new.buy_number where go
ods_id=new.goods_id;
mysql> update `order` set buy_number = 10 ;