在oracle中数据库与数据库 之间相互访问可以采用database link (以下简称dblink)方式连接,
在每个数据库服务器上设置链接字符串,即服务名,就是我们用plsql登陆需要选择的那个东西
例如:
ORCL101 = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.4.101)(PORT = 1521)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ORCL) ) )
ORCL101 数据库服务器连接字符串 服务名
1、创建db link
方式一、直接新建服务连接字符串
SQL>create public database link DONG connect to dong717 identified by "Dong717" using '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.4.220)(PORT = 1521)) ) (CONNECT_DATA = (service_name=ORCL)))';
方式二、使用已经建好的服务名
SQL>create public database link DONG connect to dong717 identified by "Dong717" using 'ORCL101';
2、查看数据库连接
sql> select owner, db_link from dba_db_links; OWNER DB_LINK PUBLIC DB_156
3、使用dblink执行查询
SQL>select * from tb_sys_department t,department@DONG d where d.code=t.search_key
4、为了方便查询可以建立同义词,为了使有关分布式操作更透明,ORACLE数据库里有同义词的对象synonym
SQL>create synonym dept_link for department@DONG; SQL>select * from dept_link
5、删除dblink
SQL>drop public database link DONG;
6、常见错误
1、ORA-12154: TNS : 无法处理服务名