编写触发器
模拟 某品牌全国连锁店 生产a,b,c三款产品
1.总公司实现统计月销售金额,月利润
2.分销点 下单订单
3.分销点 退还单
建表
create table chanpin(name varchar(10),danjia int,kucun int);
create table xiadan(name varchar(10),shuliang int);
create table tuidan(name varchar(10),shuliang int);
create table tongji(name varchar(10),shuliang int,zongji int);
添加产品信息
insert into chanpin values ('a',100,10000);
insert into chanpin values ('b',150,10000);
insert into chanpin values ('c',120,10000);
下单触发
create or replace trigger dingdan
after insert on xiadan
for each row
begin
if inserting then
update chanpin set kucun=kucun-:new.shuliang where chanpin.name=:new.name;
end if;
end;
/
测试
insert into xiadan values('a',100);
select * from xiadan;
select * from chanpin;
退单触发
create or replace trigger tuihuo
after insert on tuidan
for each row
begin
if inserting then
update chanpin set kucun=kucun+:new.shuliang where chanpin.name=:new.name;
end if;
end;
/
测试
insert into tuidan values('a',10);
select * from tuidan;
select * from chanpin;