触发器可以监视某种数据操作(insert/update/delete)
并触发(insert/update/delete)(
创建触发器4要素
监视地点 -----监视事件----触发时间----触发事件
触发器:trigger:
需求:
我们有商品表goods,订单表 ord,当下一个订单时,对应的商品要减少
分析:
监视谁:ord表
监视动作:insert
触发事件:after
触发事件:goods表中库存减少事件,update
create trigger t1
after
insert
on ord
for each row
begin
update goods xxxxx
end;
create table goods(
gid int,
name varchar(20),
num smallint
);
create table ord(
oid int,
gid int,
much smallint
);
insert into goods values
(1,'cat',34),
(2,'dog',65),
(3,'pig',21);
delimiter $
create trigger t1
after
insert
on ord
for each row
begin
update goods set num=num-2 where gid=1;
end$
查看触发器:
show triggers \G
删除已有的triggers
drop trigger t1
insert(被监测的语句)产生的数据,能否在触发器中被引用
create trigger t2
after
insert
on ord
for each row
begin
update goods set num=num-new.much where gid=new.gid;
end$
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
#在mysql编程中的for each row 是干什么的?
在oracle 触发器中,触发器分为语句级的触发器和行级触发器
比如
create trigger tn
after update
on xxtable
for each row #每一行受影响,触发器都指行,叫做行级触发器
begin
sqlN
end$
指行:update xxxtable se xxx=xxx where id>100; 影响100条
那么sqlN会被触发几次?100次
在oracle 中
for each 如果不写,
无论update语句一次影响了多少行,都只执行1次
mysql不支持语句级触发器。必须写上。
查看已有的triggers:show triggers
删除已有的triggers: drop trigger triggerName
撤回一个订单时:
create trigger t3
after
delete
on ord
for each row
begin
update goods set num=num+old.much where gid=old.gid;
end$
#改订单数量
create trigger t4
before
update
on ord
for each row
begin
update goods set num=num+old.much-new.much where gid=old.gid;
end$
思考:before 和after的区别
如果发生爆仓情况,如何处理呢?
#在t2的基础上,完成much 与num的判断
create trigger t5
before
insert
on ord
for each row
begin
declare rnum int;
select num into rnum from goods where gid=new.gid;
if new.much>rnum then
set new.much=rnum;
end if;
update goods set num=num-new.much where gid=new.gid;
end$
并触发(insert/update/delete)(
创建触发器4要素
监视地点 -----监视事件----触发时间----触发事件
触发器:trigger:
需求:
我们有商品表goods,订单表 ord,当下一个订单时,对应的商品要减少
分析:
监视谁:ord表
监视动作:insert
触发事件:after
触发事件:goods表中库存减少事件,update
create trigger t1
after
insert
on ord
for each row
begin
update goods xxxxx
end;
create table goods(
gid int,
name varchar(20),
num smallint
);
create table ord(
oid int,
gid int,
much smallint
);
insert into goods values
(1,'cat',34),
(2,'dog',65),
(3,'pig',21);
delimiter $
create trigger t1
after
insert
on ord
for each row
begin
update goods set num=num-2 where gid=1;
end$
查看触发器:
show triggers \G
删除已有的triggers
drop trigger t1
insert(被监测的语句)产生的数据,能否在触发器中被引用
create trigger t2
after
insert
on ord
for each row
begin
update goods set num=num-new.much where gid=new.gid;
end$
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
#在mysql编程中的for each row 是干什么的?
在oracle 触发器中,触发器分为语句级的触发器和行级触发器
比如
create trigger tn
after update
on xxtable
for each row #每一行受影响,触发器都指行,叫做行级触发器
begin
sqlN
end$
指行:update xxxtable se xxx=xxx where id>100; 影响100条
那么sqlN会被触发几次?100次
在oracle 中
for each 如果不写,
无论update语句一次影响了多少行,都只执行1次
mysql不支持语句级触发器。必须写上。
查看已有的triggers:show triggers
删除已有的triggers: drop trigger triggerName
撤回一个订单时:
create trigger t3
after
delete
on ord
for each row
begin
update goods set num=num+old.much where gid=old.gid;
end$
#改订单数量
create trigger t4
before
update
on ord
for each row
begin
update goods set num=num+old.much-new.much where gid=old.gid;
end$
思考:before 和after的区别
如果发生爆仓情况,如何处理呢?
#在t2的基础上,完成much 与num的判断
create trigger t5
before
insert
on ord
for each row
begin
declare rnum int;
select num into rnum from goods where gid=new.gid;
if new.much>rnum then
set new.much=rnum;
end if;
update goods set num=num-new.much where gid=new.gid;
end$