mysql 触发器

CREATE TABLE `hx_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gid` varchar(10) NOT NULL DEFAULT '',
  `num` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

CREATE TABLE `hx_goods` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_name` varchar(10) NOT NULL DEFAULT '',
  `num` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

delimiter $;
create trigger t2
after 
delete
on hx_order
for each row
begin
update hx_goods set num=num+old.num where id=old.gid;
end$;
delimiter $;
create trigger t1
after 
insert
on hx_order
for each row
begin
update hx_goods set num=num-new.num where id=new.gid;
end$;



你可能感兴趣的:(mysql)