溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

通過(guò)觸發(fā)器分發(fā)同步數(shù)據(jù)

發(fā)布時(shí)間:2020-08-12 16:26:07 來(lái)源:ITPUB博客 閱讀:116 作者:wzgchen 欄目:MySQL數(shù)據(jù)庫(kù)
通過(guò)觸發(fā)器分發(fā)同步數(shù)據(jù)


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 ;

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI