CREATE [链接类型] DATABASE LINK [DBLink链接名称] CONNECT TO [远程数据库用户] IDENTIFIED BY "[远程数据库密码]" USING '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = [远程数据库IP地址])(PORT = [远程数据库端口])) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = [远程数据库SERVICE_NAME]) ) )';
创建public类型DBLink
创建DBLink时候使用双引号将密码括起来,Oracle11g会将小写变成大写。
如果后期修改了远程数据库的密码,需要在DBLink中修改密码
1 2 3 4 5 6 7 8 9 10
CREATE PUBLIC DATABASE LINK "DB_REMOTE" CONNECT TO test IDENTIFIED BY "AAbb111" USING '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.111)(PORT = 1521)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ORCL) ) )';
查看DBLink
1 2 3
SELECT OWNER, OBJECT_NAME FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'DATABASE LINK'; --OR SELECT * FROM DBA_DB_LINKS;
使用DBLink
查询
在表名后加上“[@DBLink链接名称]”即可,如下
1
SELECT SYSDATE FROM DUAL@DB_REMOTE;
创建同义词
1 2 3
CREATE SYNONYM TEST_REMOTE FOR TEST@DB_REMOTE; -- 那么上面的查询、插入、修改、删除中可直接用TEST_REMOTE代替TEST@DB_REMOTE即可,例如查询语句可改成如下方式(插入,修改,删除类似) SELECT * FROM TEST_REMOTE ORDER BY ID;
修改DBLink
如修改密码,也可以删除重建
1 2
alter database link [DBLink链接名称] connect to [远程数据库用户] identified by "[远程数据库密码]" ; alter database link link_102 connect to test identified by "abcABC";
删除DBLink
1 2 3 4 5
--删除private模式DBLink DROP DATABASE LINK [DBLink链接名称]; --删除public模式DBLink DROP PUBLIC DATABASE LINK [DBLink链接名称] DROP PUBLIC DATABASE LINK DB_REMOTE;