CREATE [PUBLIC] DATABASE LINK
CREATE [PUBLIC] DATABASE LINK創(chuàng)建一個數(shù)據(jù)庫連接。
語法
------連接到PolarDB PostgreSQL版(兼容Oracle)
CREATE [ PUBLIC ] DATABASE LINK <name>
CONNECT TO <username> IDENTIFIED BY "<password>"
USING ( channel_name '<channel_name>', dbname '<db_name>' );
------連接到Oracle
CREATE [ PUBLIC ] DATABASE LINK <name>
CONNECT TO <username> IDENTIFIED BY "<password>"
USING '//<channel_name>/<dbname>';
參數(shù)說明
參數(shù) | 說明 |
PUBLIC | 創(chuàng)建任何用戶都可以使用的公共數(shù)據(jù)庫連接。如果省略此參數(shù),則創(chuàng)建私有數(shù)據(jù)庫連接,只有數(shù)據(jù)庫連接的所有者可以使用。 |
name | 數(shù)據(jù)庫連接的名稱。 |
username | 用于連接到遠程數(shù)據(jù)庫的用戶名。 |
password | 用于連接到遠程數(shù)據(jù)庫的密碼。 |
注意事項
CREATE DATABASE LINK創(chuàng)建的數(shù)據(jù)庫連接是一個對象,允許在DELETE、INSERT、SELECT或UPDATE命令中引用遠程數(shù)據(jù)庫中的表或視圖。
數(shù)據(jù)庫連接可以是公共的或私有的。公共數(shù)據(jù)庫連接是任何用戶都可以使用的連接。私有數(shù)據(jù)庫連接只有數(shù)據(jù)庫連接的所有者才能使用。指定PUBLIC選項來創(chuàng)建公共數(shù)據(jù)庫連接,否則,將創(chuàng)建私有數(shù)據(jù)庫連接。
創(chuàng)建數(shù)據(jù)庫連接之后,可以通過 DBA_DB_LINKS/ALL_DB_LINKS/USER_DB_LINKS 視圖查看所有已經(jīng)創(chuàng)建的數(shù)據(jù)庫連接。
示例
如果創(chuàng)建過程中提示 oracle_fdw/postgres_fdw 插件不存在,請通過 CREATE EXTENSION
命令創(chuàng)建。
創(chuàng)建oracle_fdw數(shù)據(jù)庫連接。
CREATE DATABASE LINK ora_dblink
CONNECT TO admin IDENTIFIED BY 'mypassword'
USING '//orachannel/acctg';
創(chuàng)建postgres_fdw數(shù)據(jù)庫連接。
CREATE DATABASE LINK pg_dblink
CONNECT TO admin IDENTIFIED BY "mypassword"
USING (channel_name 'localhost', dbname 'marketing');
使用數(shù)據(jù)庫連接。
SELECT * FROM emp@ora_link;
結(jié)果顯示如下:
empno | ename | job | mgr | hiredate | sal | comm | deptno
-------+--------+-----------+------+--------------------+------+------+--------
7369 | SMITH | CLERK | 7902 | 17-DEC-80 00:00:00 | 800 | | 20
7499 | ALLEN | SALESMAN | 7698 | 20-FEB-81 00:00:00 | 1600 | 300 | 30
7521 | WARD | SALESMAN | 7698 | 22-FEB-81 00:00:00 | 1250 | 500 | 30
7566 | JONES | MANAGER | 7839 | 02-APR-81 00:00:00 | 2975 | | 20
(4 rows)
使用數(shù)據(jù)庫連接。
SELECT d.deptno, d.dname, e.empno, e.ename, e.job, e.sal, e.comm FROM
emp@ora_link e, dept@fdwlink d WHERE e.deptno = d.deptno ORDER BY 1, 3;
結(jié)果顯示如下:
deptno | dname | empno | ename | job | sal | comm
--------+------------+-------+--------+-----------+------+------
10 | ACCOUNTING | 7782 | CLARK | MANAGER | 2450 |
10 | ACCOUNTING | 7839 | KING | PRESIDENT | 5000 |
10 | ACCOUNTING | 7934 | MILLER | CLERK | 1300 |
20 | RESEARCH | 7369 | SMITH | CLERK | 800 |
20 | RESEARCH | 7566 | JONES | MANAGER | 2975 |
20 | RESEARCH | 7788 | SCOTT | ANALYST | 3000 |
20 | RESEARCH | 7876 | ADAMS | CLERK | 1100 |
20 | RESEARCH | 7902 | FORD | ANALYST | 3000 |
30 | SALES | 7499 | ALLEN | SALESMAN | 1600 | 300
30 | SALES | 7521 | WARD | SALESMAN | 1250 | 500
30 | SALES | 7654 | MARTIN | SALESMAN | 1250 | 1400
30 | SALES | 7698 | BLAKE | MANAGER | 2850 |
30 | SALES | 7844 | TURNER | SALESMAN | 1500 | 0
30 | SALES | 7900 | JAMES | CLERK | 950 |
(14 rows)