mysql触发器

来源:互联网 发布:俄罗斯经济崩溃 知乎 编辑:程序博客网 时间:2024/06/04 18:54

一、先创建两张表 商品和订单product和order来模拟一下,下单和减少库存操作

字段 类型 注释 id int 主键 name varchar(20) 商品名字 amount int 库存数量 price decimal(10,2) 商品价格


创建product表

create table product(id int primary key not null auto_increment,name varchar(20) not null default '',amount int not null default 0,price decimal(10,2) not null default 0.00)charset utf8;
字段 类型 注释 id int 主键 product_id int 关联商品 amount int 购买数量


创建purchase_order表

create table purchase_order(id int primary key auto_increment,product_id int not null default 0,amount int not null default 0)charset uft8;

在商品表中添加几条数据
insert into product
(name,amount,price)
values
(‘笔记本’,100,9999),
(‘iphone7’,100,6388),
(‘ipad’,100,388)

现在模拟一下操作,在purchase_order表中添加一条记录,则相应地在purchase表中减少应的数量。现在我们要用到触发器概念了。

0 0