[转载]Oracle DBLink 创建及其使用和使用中问题解决
转载于
http://dacoolbaby.iteye.com/blog/1632076
1.创建dblink的语法:
1.创建dblink的语法:
- CREATE [PUBLIC] DATABASE LINK link
- CONNECTTO username IDENTIFIED BYpassword
- USING ‘connectstring’
CREATE [PUBLIC] DATABASE LINK link CONNECT TO username IDENTIFIED BY password USING ‘connectstring’
2.说明:
1)权限:创建数据库链接的帐号必须有CREATE DATABASE LINK或CREATE PUBLIC DATABASE LINK的系统权限,用来登录到远程数据库的帐号必须有CREATE SESSION权限。这两种权限都包含在CONNECT角色中(CREATE PUBLIC DATABASE LINK权限在DBA中)。
如果省略public,则创建的是private的数据库连接,private的连接只能是当前创建DB_LINK的用户可以使用这个db_link,public表示数据库中所有的用户都可以使用这个db_link。
2)link : 当GLOBAL_NAME=TRUE时,link名必须与远程数据库的全局数据库名global_name相同(global_name可以在远程数据库上执行select * from global_name;得到); 当GLOBAL_NAME=TRUE时,可以任意命名。
3)connectstring:连接字符串,tnsnames.ora中定义远程数据库的连接串。
4)username、password:远程数据库的用户名,口令。如果不指定,则使用当前的用户名和口令登录到远程数据库。
3.删除数据库链接的语句:
- DROP [PUBLIC] DATABASE LINK zrhs_link
DROP [PUBLIC] DATABASE LINK zrhs_link
4.查看已创建的dblink
- select owner,object_name from dba_objects where object_type='DATABASE LINK';
- select * from dba_db_links;
select owner,object_name from dba_objects where object_type='DATABASE LINK'; select * from dba_db_links;
5.修改GLOBAL_NAME的方法:
方法一、在数据库的init.ora文件中将global_names设为true/false。
方法二、语句:ALTER SYSTEM SET GLOBAL_NAME=TRUE/FALSE;
修改后重新启动数据库设置才能生效。
注:数据库全局名称可以用以下命令查出:SELECT * FROM GLOBAL_NAME;
6.dblink的创建和使用:
- --GLOBAL_NAME为false
- alter system set global_names=false;
--GLOBAL_NAME为false alter system set global_names=false;
数据库链接名(db_link名)任意取名:
创建:
方法一:
- createpublicdatabase link USER
- connectto jade identified by jade
- using '(DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
- )
- (CONNECT_DATA =
- (SERVICE_NAME = orcl)
- )
- )';
create public database link USER connect to jade identified by jade using '(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = orcl) ) )';
方法二:
- createpublicdatabase link userconnectto epm2 identified by epm2 using 'orcl';
- --使用:
- select * from pm_t_kpi_data@user;
- --GLOBAL_NAME为true
- alter system set global_names=true;
create public database link user connect to epm2 identified by epm2 using 'orcl'; --使用: select * from pm_t_kpi_data@user; --GLOBAL_NAME为true alter system set global_names=true;
要求数据库链接名(db_link名)跟远端数据库的global_name名一样
错误创建和使用:
- createpublicdatabase link userconnectto epm2 identified by epm2 using 'orcl';
- select * from pm_t_kpi_data@user;
create public database link user connect to epm2 identified by epm2 using 'orcl'; select * from pm_t_kpi_data@user;
ORA-02085:数据库链接:USER.REGRESS.RDBMS.DEV.US.ORCLE.COM 连接 ORCL
正确创建和使用:
创建一个和使用:
- createpublicdatabase link orcl connectto epm2 identified by epm2 using 'ORCL';
- select * from pm_t_kpi_data@orcl@al;
create public database link orcl connect to epm2 identified by epm2 using 'ORCL'; select * from pm_t_kpi_data@orcl@al;
创建多个和使用:注:al可以任意取名
- createpublicdatabase link orcl@al connectto epm2 identified by epm2 using 'ORCL';
- select * from pm_t_kpi_data@orcl@al;
create public database link orcl@al connect to epm2 identified by epm2 using 'ORCL'; select * from pm_t_kpi_data@orcl@al;
7、取消REGRESS.RDBMS.DEV.US.ORACLE.COM后缀:
global_name(oradb)带上默认的域名REGRESS.RDBMS.DEV.US.ORACLE.COM后缀,则可以执行下面语句进行解决
- update global_name set global_name='orcl';
- -- (本地服务器上执行)
- commit;
update global_name set global_name='orcl'; -- (本地服务器上执行) commit;
或者
- update props$ set value$ = 'orcl'wherename = 'GLOBAL_DB_NAME'; commit;
update props$ set value$ = 'orcl' where name = 'GLOBAL_DB_NAME'; commit;
之后重启。重新建立的就没有后缀名了。
8、为dblink创建同义词
- createorreplace synonym student for student_a@USER;
create or replace synonym student for student_a@USER;
注:student为同义词名,student_a为dblink链接的表
查询:
- select *from student_a@USER
select *from student_a@USER
等价于
- select * from student