oracle使用快照和触发器同步数据

假设有数据库db1和db2 , 表db1.t_task_msg, db2.t_task_msg
现在同步db1.t_task_msg数据到db2.t_task_msg

1, 在db2建立到db1的连接source_link

  create database link source_link
   connect to db1_user identified by db1_pwd   
   using
        '(DESCRIPTION =
           (ADDRESS_LIST =
             (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
           )
          (CONNECT_DATA =
             (SERVICE_NAME = orcl)
          )
       )';

2,在db1上创建要同步的表的快照日志

   create snapshot log on t_task_msg;

3,在db2上创建快照并设置刷新时间

   create snapshot sn_t_task_msg
      refresh fast Start with sysdate next sysdate with primary key
      as select * from t_task_msg@source_link;
  或
  create snapshot sn_t_task_msg as select * from t_task_msg@source_link;
    Alter snapshot sn_t_task_msg refresh fast Start with sysdate next sysdate with primary key;

4,创建一个触发器, 当快照有更新时, 更新db1中的数据

create or replace trigger tr_t_task_msg
  after  insert or update or delete on sn_t_task_msg
  for each row
begin
  if deleting then
      delete from t_task_msg where fid=:old.fid;
  end if;
  if inserting then
      insert into t_task_msg(fid,fserviceid,fcontent,fuserid,fstate,finserttime)
      values(:new.fid,:new.fserviceid,:new.fcontent,:new.fuserid,:new.fstate,:new.finserttime);
  end if;
  if updating then
     update t_task_msg set fserviceid=:new.fserviceid, fcontent=:new.fcontent, fuserid=:new.fuserid, fstate=:new.fstate, finserttime=:new.finserttime where fid=:old.fid;
  end if;
end tr_t_task_msg;


----本文来Net探索者,http://www.codefinds.net 转载请说明出处!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值