首先,准备两台服务器,一台 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),添加如下代码:
(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;
创建触发器:
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;
/
测试两台服务器的数据同步:
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.