触发器实例

前提有总表分表
--
------------------------------------------------------------------------------------------------------------------------------------ insert 触发器 create trigger insert_trigger after insert on blog for each row begin if new.userid % 3 = 0 then insert into blog_0(id,title,content,userid,pubtime) value(new.id,new.title,new.content,new.userid,new.pubtime); elseif new.userid % 3 = 1 then insert into blog_1(id,title,content,userid,pubtime) value(new.id,new.title,new.content,new.userid,new.pubtime); elseif new.userid % 3 = 2 then insert into blog_2(id,title,content,userid,pubtime) value(new.id,new.title,new.content,new.userid,new.pubtime); end if; end// insert into blog(id,title,content,userid,pubtime) value('111','134','789','456','123'); -------------------------------------------------------------------------------------------------------------------------------------- delete 触发器 create trigger delete_trigger after delete on blog for each row begin if old.userid % 3 = 0 then delete from blog_0 where id=old.id; elseif old.userid % 3 = 1 then delete from blog_1 where id=old.id; elseif old.userid % 3 = 2 then delete from blog_2 where id=old.id; end if; end// delete from blog where id = 111; -------------------------------------------------------------------------------------------------------------------------------------- update 触发器 create trigger update_trigger after update on blog for each row begin if old.userid % 3 = 0 then update blog_0 set title = new.title, content=new.content where id=old.id; elseif old.userid % 3 = 1 then update blog_1 set title = new.title, content=new.content where id=old.id; elseif old.userid % 3 = 2 then update blog_2 set title = new.title, content=new.content where id=old.id; end if; end// update blog set title="abc",content="ccc" where id= 112//

 

posted @ 2018-07-18 10:50  super久违  阅读(204)  评论(0编辑  收藏  举报