Oracle——在oracle下创建DBLink来链接其它数据库(windows环境)
一、Oracle与Oracle的链接
应用场景:现有两个数据库,一个是SRM,一个是BK_SRM,现要通过dblink在SRM连接BK_SRM。
1.在元Oracle中创建DBLink
a.检查oracle登录账户是否有创建dblink的权限,验证元oracle是否有CREATE PUBLIC DATABASE LINK,DROP PUBLIC DATABASE权限,执行select * from user_sys_privs查询语句,如下图,该图表示权限已经有,如果没有则需要执行b步骤。
b.没有权限则需要给与SRM库账号授权,切换sys高级账号登录,执行下述SQL
grant CREATE PUBLIC DATABASE LINK,DROP PUBLIC DATABASE LINK to SRM
c.创建DBLink,执行下述SQL
语法:create public database link [dblink名字]
connect to "[需要连接数据库的用户名]" identified by "[需要连接数据库的密码]"
using '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = [数据库ip])(PORT = [数据库端口])))(CONNECT_DATA =(SERVICE_NAME = [服务名称])))';
注意:用户名和密码左右两边的双引号不要漏掉,这里SERVICE_NAME = [服务名称] 可以替换成SID=[实例名称]
用户名需要区分的大小写(orecle区分大小写),我已经与坑了~~
举例如下:
create public database link PJDBLink
connect to "C##BK_SRM" identified by "123456" using '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)))';
2.查看dblink是否创建成功,执行下述SQL
方式一:select * from dba_db_links;
方式二:select owner,object_name from dba_objects where object_type='DATABASE LINK';--查询时间久
3.操作查询DBLink数据库的数据
语法:select * from 表名@[dblink名字]
举例:select * from crm_bd_building@PJDBLink
4.删除dblink
语法:drop public database link [dblink名字]
举例:drop public database link PJDBLink
二、Oracle与SQLServer的链接
后补~~
三、Oracle与Mysql的链接
后补~~