DBLink 实现方式
-- 在12C 数据库 dba 用户下面做:
rlwrap sqlplus / as sysdba
create public database link db11g connect to C##ZZH identified by 000000 using '在12c配置的连接字符串(db11g)';
-- 例:
create database link db11g.orcl
connect to Ahern identified by oracle
using '(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.246)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.com)
(INSTANCE_NAME = orcl)
)
)';
-- 建立连接后就可以连接到 C##ZZH
conn C##ZZH/000000
-- 就可以在 db12c 查看 db11g 下的表:
desc tablename@db11g
-- 访问:
select * from tablename@db11g where deptno=10; --该是远程11g数据库的表
-- 要让用户感觉就像调用自己的表:
-- 在dba用户下授予他建立同义词权限:
grant create synonym to C##ZZH;
-- 现在就可以连到C##ZZH用户下创建同义词:
create synonym tablename for tablename@db11g;
-- 现在就可以这样访问:给你的感觉就像这表就是本地表一样
select * from tablename where '...';
-- 通过Db link 就可以实现一个数据库去访问很多数据库,给用户的感觉就是该表就像是存储在本地数据库一样
-- 可以通过语句查询数据库上面有多少个db link 有权限访问的都可以查询出来
desc all_db_links;
select * from all_db_links;
-- 在dba用户下删除link:
drop public database link db11g.orcl;
-- 同义词也失效了也应该删除:
drop synonym tablename;
-- 关于db link 的东西就删除干净了
示例:
revoke create database link from dataexch;
create public database link exchlink connect to dataexch identified by zzhQ123hz using 'YNSBSBY';
create user dataexch identified by zzhQ123hz default tablespace DATA;
grant connect,resource to dataexch;
select 'grant select on yn01700_p.'||table_name||' to dataexch;' from dba_tables where OWNER='YN01700_P';
select 'grant select on yn01700_i.'||table_name||' to dataexch;' from dba_tables where OWNER='YN01700_I';
select 'grant select on yn01700_param.'||table_name||' to dataexch;' from dba_tables where OWNER='YN01700_PARAM';
select 'grant select on yn01700_frame.'||table_name||' to dataexch;' from dba_tables where OWNER='YN01700_FRAME';