当前位置:首页 > Oracle
使用触发器实现两台服务器上 Oracle 数据库的数据同步
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2014-01-09 17:41:07  点击量:941  评论:0

    首先,准备两台服务器,一台 Windows XP 系统,数据库:Oracle 10g 企业版;另一台 Linux 系统,数据库:Oracle 11g 企业版。


    为了保证 Windows XP 系统上的 Oracle 数据库能够连接到 Linux 系统上的 Oracle 数据库,需要建立一个 dblink。
    修改 tnsnames.ora 文件(Windows XP 系统上默认在 C:\oracle\product\10.2.0\db_1\NETWORK\ADMIN\tnsnames.ora),添加如下代码:

MORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.15.188)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )


    创建 dblink 之前,必须有创建 dblink 的权限。以 sys 身份登录:sqlplus sys/change_on_install as sysdba;
    查看 dblink 的权限:select * from user_sys_privs t where t.privilege like upper('%link%');

    CREATE DATABASE LINK:所创建的 dblink 只有创建者能使用,其它用户不能使用
    CREATE PUBLIC DATABASE LINK:所创建的 dblink 所有用户都可以使用
    DROP PUBLIC DATABASE LINK:删除 dblink
    以 sys 身份把 CREATE PUBLIC DATABASE LINK 和 DROP PUBLIC DATABASE LINK 权限授予 scott 用户:grant CREATE PUBLIC DATABASE LINK, DROP PUBLIC DATABASE LINK to scott;

    以 scott 身份登录,创建 dblink:create public database link to_morcl connect to scott identified by "tiger" using 'MORCL';

    其中,to_morcl 是所创建的 dblink 的名字,MORCL 是远程数据库的实例名,scott/tiger 是登录到远程数据库的帐号/密码。
    通过 dblink 访问远程数据库MORCL中DEPT表:select * from DEPT@to_morcl;

    创建触发器:

create or replace trigger rtest
after insert or update or delete on DEPT
for each row
begin
if inserting then
    insert into DEPT@to_morcl(DEPTNO,DNAME,LOC) values (:new.DEPTNO,:new.DNAME,:new.LOC);
  elsif updating then
    update DEPT@to_morcl set DEPTNO=:new.DEPTNO,DNAME=:new.DNAME,LOC=:new.LOC where DEPTNO=:old.DEPTNO;
  elsif deleting then
    delete from DEPT@to_morcl where DEPTNO=:old.DEPTNO;
  end if;
end;
/


    测试两台服务器的数据同步:


版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting