通过触发器分发同步数据

通过触发器分发同步数据


create table tab(id int not null primary key,name varchar(20),age int,address varchar(200));
create table tab0(id int not null primary key,name varchar(20),age int,address varchar(200));
create table tab1(id int not null primary key,name varchar(20),age int,address varchar(200));
create table tab2(id int not null primary key,name varchar(20),age int,address varchar(200));




insert into tab values(1,'chen',30,'shanghai');
insert into tab values(2,'jiang',40,'shanghai');
insert into tab values(3,'wang',31,'shanghai');






delimiter //
create trigger tri_sync_insert
  after insert on tab
for each row
begin
declare
v_result int;
set v_result=mod(new.id,3);
if v_result =0 then
insert into tab0(id,name,age,address) values(new.id,new.name,new.age,new.address);
elseif v_result = 1 then
insert into tab1(id,name,age,address) values(new.id,new.name,new.age,new.address);
else
insert into tab2(id,name,age,address) values(new.id,new.name,new.age,new.address);
end if;
end;
//
delimiter ;




delimiter //
create trigger tri_sync_update
  after update on tab
for each row
begin
declare
v_result int;
set v_result=mod(new.id,3);
if v_result =0 then
replace into tab0(id,name,age,address) values(new.id,new.name,new.age,new.address);
elseif v_result = 1 then
replace into tab1(id,name,age,address) values(new.id,new.name,new.age,new.address);
else
replace into tab2(id,name,age,address) values(new.id,new.name,new.age,new.address);
end if;
end;
//
delimiter ;




delimiter //
create trigger tri_sync_delete
  after delete on tab
for each row
begin
declare
v_result int;
set v_result=mod(old.id,3);
if v_result =0 then
delete from tab0 where id=old.id;
elseif v_result = 1 then
delete from tab1 where id=old.id;
else
delete from tab2 where id=old.id;
end if;
end;
//
delimiter ;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/91975/viewspace-2124034/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/91975/viewspace-2124034/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值