Home  >  Article  >  Database  >  dblink概述及创建示例

dblink概述及创建示例

WBOY
WBOYOriginal
2016-06-07 15:28:542432browse

dblink概述 dblink是定义一个数据库到另一个数据库的路径的对象,dblink允许你查询远程表及执行远程程序。在任何分布式环境里,database都是必要的。另外要注意的是db link是单向的连接。 在创建dblink的时候,Oracle再数据字典中保存相关的dblink的信息,在

dblink概述
dblink是定义一个数据库到另一个数据库的路径的对象,dblink允许你查询远程表及执行远程程序。在任何分布式环境里,database都是必要的。另外要注意的是db link是单向的连接。
在创建dblink的时候,Oracle再数据字典中保存相关的dblink的信息,在使用dblink的时候,Oracle通过Oracle Net用用户预先定义好的连接信息访问相应的远程数据库以完成相应的工作。
建立dblink之前需要确认的事项:
确认从local database到remote database的能够通过监听访问--sqlplus aa/bb@cc 连接正常,并确认所使用的用户在remote database上面有相应的访问权限

源库上查询:
SYS@ bys3>select * from user_sys_privs where privilege like upper('%link%');
USERNAME PRIVILEGE ADM
------------------------------ ---------------------------------------- ---
SYS DROP PUBLIC DATABASE LINK NO
SYS CREATE DATABASE LINK NO
SYS CREATE PUBLIC DATABASE LINK NO
[oracle@bys3 admin]$ cat tnsnames.ora
ocm =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.215)(PORT =1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ocm1)
)
)
目标库表中数据:
BYS@ ocm1>select * from test;
DEPTNO DNAME LOC
---------- ---------- ----------
10 chedan zheng
20 chifan bj
#######################################实验

1.创建公有DBLINK的方法: ---需要使用public参数,OWNER 是public

SYS@ bys3>create public database link ocm1 connect to bys identified by bys using 'ocm';
Database link created.
Elapsed: 00:00:00.03 ---语法正常且using 的网络服务名正常的话,语句完成很快,如果等待很久才能完成,很可能是网络服务名的配置有问题。
SYS@ bys3>select * from dept@ocm1; ---DEPT表不存在
select * from dept@ocm1
*
ERROR at line 1:
ORA-00942: table or view does not exist
ORA-02063: preceding line from OCM1
SYS@ bys3>select * from test@ocm1;
DEPTNO DNAME LOC
---------- ---------- ----------
10 chedan zheng
20 chifan bj
SYS@ bys3>drop public database link ocm1; --删除PUBLIC DBLINK
Database link dropped.
另一条创建语句:数据链的名称可以自己决定--不过最好弄成能标示连接的目标库信息
SYS@ bys3>create public database link aa connect to bys identified by bys using 'ocm';
Database link created.
Elapsed: 00:00:00.01
SYS@ bys3>select * from test@aa;
DEPTNO DNAME LOC
---------- ---------- ----------
10 chedan zheng
20 chifan bj
############

2.创建私有DBLINK---创建dblink的user拥有该dblink

TEST1@ bys3>create database link ocm2 connect to bys identified by bys using 'ocm';
Database link created.
TEST1@ bys3>select * from test@ocm2;
DEPTNO DNAME LOC
---------- ---------- ----------
10 chedan zheng
20 chifan bj
TEST1@ bys3>select db_link from dba_db_links;
DB_LINK
----------------------------------------------------------------------------------------------------
AA
OCM2
TEST1@ bys3>drop database link ocm2;
Database link dropped.

3.使用网络服务的连接串来创建DBLINK--这样做好处是不受tnsnames.ora文件中信息的影响

TEST1@ bys3>create database link ocm4 connect to bys identified by bys using '(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.215)(PORT =1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = ocm1)
)
)';

Database link created.
Elapsed: 00:00:00.01 ---这个长命令应该在文本中写好命令,直接复制执行。
TEST1@ bys3>select * from test@ocm4;
DEPTNO DNAME LOC
---------- ---------- ----------
10 chedan zheng
20 chifan bj
TEST1@ bys3>select db_link from dba_db_links;
DB_LINK
---------------------------
AA
OCM2
OCM4

4.ORA-02011:错误--因为数据链名称已经存在


TEST1@ bys3>create database link ocm2 connect to bys identified by bys using 'ocm';
create database link ocm2 connect to bys identified by bys using 'ocm'
*
ERROR at line 1:
ORA-02011: duplicate database link name

TEST1@ bys3>select db_link from dba_db_links;
DB_LINK
----------------------------------------------------------------------------------------------------
AA
OCM2


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn