`

mysql触发器

 
阅读更多

有个产品,产品有专辑,每个产品只能有一个专辑。

专辑表有个专辑中产品数量。

于是写了个触发器维护。发现写这玩意很容易出错,因为涉及到产品设置专辑和取专辑,其次产品还会进行删除操作,目前只考虑假删,通过isdel标志位来判断。

 

drop trigger trig_Product_update;
CREATE  TRIGGER trig_Product_update
AFTER UPDATE
ON product FOR EACH ROW
BEGIN
if new.albumid<>old.albumid then
if (new.albumid=0) then
update album set pronum=pronum-1 where id=old.albumid;
else
update album set pronum=pronum+1 where id=new.albumid;
END IF;
end if;

if new.isdel<>old.isdel and old.albumid>0 then
if new.isdel=1  then
update album set pronum=pronum-1 where id=old.albumid;
else
update album set pronum=pronum+1 where id=new.albumid;
END IF;
end if;

END;

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics