背景
需求:两张结构相同的表,进行数据同步
工具:MySQL表触发器
1、插入
begin
if(not exists(select 1 from 副表 where id=new.id)) then
INSERT INTO 副表 (字段) VALUES(new.字段);
end if;
END
2、更新
begin
if(not exists(select 1 from 副表 where id<=>old.id and 字段<=>old.字段)) then
update 副表 set 字段=new.字段 where id=old.id;
end if;
END
3、删除
begin
if(exists(select 1 from 副表 where id=old.id)) then
delete from 副表 where id=old.id;
end if;
END