//用户有一张购物卡
create table card(
id int notnull primary key auto_increment,
cash int unsigned notnull);
//金额有1000
insert into card values(1,1000);
//一位大佬,每次买东西,金额都能增加//但这显然是不可以的
update card set cash = cash - (-20) where id =1;
//写个触发器来约束这种行为
mysql> create table card_err(
-> id int notnull,
-> old_cash int unsigned notnull,
-> new_cash int unsigned notnull,
-> user varchar(30),
-> time DATETIME);
mysql> create trigger tgr_card_update
-> before update on card
-> for each row
-> begin
-> ifnew.cash-old.cash >0 then
-> insert into card_err
-> select old.id,old.cash,new.cash,USER(),NOW();
-> setnew.cash = old.cash;
-> end if;
-> end;
-> ??//重复UPDATE,钱不会改变了,日志如下+----+----------+----------+----------------+---------------------+| id | old_cash | new_cash | user | time |+----+----------+----------+----------------+---------------------+|1|1020|1040| root@localhost |2017-09-3014:49:16||1|1020|1040| root@localhost |2017-09-3014:49:31|+----+----------+----------+----------------+---------------------+