很多时候会遇到要从A 库访问到B库中的数据的情况,oracle 提供了 database link 技术。创建dblink 有两种方法:
1):不能在 tnsnames.ora文件里配置的情况。
yang@rac1>create database link linkyang
2 connect to yang identified by yang
3 using
4 '( DESCRIPTION =
5 (ADDRESS_LIST =
6 (ADDRESS=(PROTOCOL = TCP )(HOST = 10.1.165.4)(PORT=1521))
7 )
8 (CONNECT_DATA=
9 (SERVICE_NAME =oracl)
10 )
11 )';
Database link created.
yang@rac1>select count(1) from yang_old@linkyang;
COUNT(1)
----------
100000
2) ,如果可以在tnsnames.ora 文件里面配置,则如下:
在文件里面添加:
RAC =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.111)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = rac)
)
)
yang@rac1>create database link linkyang2
2 connect to yang identified by yang
3 using ‘rac’;
Database link created.
yang@rac1>select count(1) from yang_old@linkyang2;
COUNT(1)
----------
100000
3)删除dblink
yang@rac1>drop database link linkyang;
Database link dropped.
4)查询已有的dblink
yang@rac1>select owner,object_name from dba_objects where object_type='DATABASE LINK';
OWNER OBJECT_NAME
--------------- -------------------------
PUBLIC ZZZZ
WANGYJ MY_RAC
WANGYJ UDB_TEST
YANG LINKORACL
YANG LINKYANG
5)同义词的使用:yang_old@linkyang 可以使用同义词来代替的。
yang@rac1>create synonym oracl_yang_yangold for yang_old@linkyang;
Synonym created.
yang@rac1>select count(1) from oracl_yang_yangold;
COUNT(1)
----------
100000
yang@rac1>