1.建立全局link(使用本地一個用戶訪問其它用戶的表)sql
語法:
數據庫
create public database link 連接名 CONNECT TO 本地用戶名 IDENTIFIED BY 密碼 USING '本地數據庫實例名';oracle
SQL> create user upch identified by System13579;
User created.
SQL> grant dba,resource,connect to upch;
Grant succeeded.ide
SQL> conn upch/System13579
Connected.spa
SQL> create table t (a number);
Table created.it
SQL> insert into t values(1);
1 row created.
SQL> insert into t values(2);
1 row created.
SQL> select * from t;
A
----------
1
2
SQL> conn / as sysdba
Connected.io
SQL> create public database link tong connect to upch identified by System13579 using 'orcl';table
SQL> select * from t@tong; --訪問upch用戶下的t表
A
----------
1
2
SQL>
class
2.遠程建立linkselect
語法:
create /* public */ database link tong connect to 遠程用戶名 identified by 密碼using '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = 遠程IP地址)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = 遠程SID值)))';
[oracle@localhost dbs]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Wed Jun 28 14:47:53 2017
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> create database link tong1 connect to upch identified by System13579 using '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = 172.16.8.161)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl)))';
Database link created.
SQL> select * from t@tong1;
A
----------
1
2
SQL> col owner for a10
SQL> col db_link for a10
SQL> col host for a150
SQL> select * from dba_db_links; --查看數據庫有多少dblinkOWNER DB_LINK USERNAME HOST CREATEDSYS TONG1 UPCH (DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL = TCP)(HOST = 172.16.8.161)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = orcl))) 28-JUN-17SQL> drop database link tong1; --刪除dblinkDatabase link dropped.SQL>