DBLINK简述
A database link is a schema object in one database that enables you to access objects on another database. The other database need not be an Oracle Database System.
语法/写法
CREATE [ SHARED ] [ PUBLIC ] DATABASE LINK dblink
[ CONNECT TO
{ CURRENT_USER
| user IDENTIFIED BY password [ dblink_authentication ]
}
| dblink_authentication
]...
[ USING connect_string ] ;关键字
- PUBLIC,如果省略它默认创建的DB_LINK是private的不能被其它用户共享仅能自己使用。
- SHARED,此关键字代表将创建的DB_LINK全局共享(与session相关),使用此关键字要求必须要有db_link_authentication(见示例2)
- CONNECT TO,指定连接到远程数据库的连接标记
- CURRENT_USER,使用当前用户信息建立与远程数据库的信息,这块要保证当前用户在远端数据库中存在并且合法。
- USING,指定远端数据库地址信息(示例)
示例1
CREATE DATABASE LINK DB_LINK_NAMECONNECT TO REMOTE_USER_CODE IDENTIFIED BY USER_PASSWORD
USING '192.168.0.8:1521/SID';
示例2
CREATE SHARED DATABASE_LINK DB_LINK_NAME
CONNECT TO REMOTE_USER_CODE_1 IDENTIFIED BY USER_PASSWORD_1
AUTHENTICATED BY REMOTE_USER_CODE_2 IDENTIFIED BY USER_PASSWORD_2
USING '192.168.0.8:1521/SID'
示例2_1
CREATE SHARED DATABASE_LINK DB_LINK_NAME
AUTHENTICATED BY REMOTE_USER_CODE_2 IDENTIFIED BY USER_PASSWORD_2
USING '192.168.0.8:1521/SID'
注意,示例2中出现两个REMOTE_USER_CODE分别是REMOTE_USER_CODE_1\REMOTE_USER_CODE_2,因为指定了SHARED关键字必须要求有AUTHENTICATION而这时CONNECT TO可以省略(如示例2_1)。
用法
- SELECT COUNT(*) FROM USER_TABLES@DB_LINK_NAME;
- UPDATE SYS_USER@DB_LINK_NAME SET USER_NAME = '001' WHERE AGE>50
- INSERT INTO SYS_USER@DB_LINK_NAME(USER_ID,USER_NAME,AGE) VALUES(001,'001', 51)
- DELETE FROM USER_TABLES@ WHERE USER_ID = '001'
参考资料
- Oracle Documentation Library
- http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_5005.htm#SQLRF01205