O11g Dblink Setting
O11g Dblink Setting
------The user SCOTT wants to manipulate data of the database "orcl" on a remote host "DBHOST" through the port 1521 with the default listener on the remote host. Steps: Create a database link using the service name "host2_db2" in the file tnsnames.o ra. Part of the file tnsnames.ora on the local host: host2_db2 = (DESCRIPTION = (ADDRESS = (DBHOSTTOCOL = TCP)(HOST = DBHOST)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) ) Grant some privileges to user who will create database links. The local user must have the privileges "CREATE DATABASE LINK, CREATE PUBLIC DATABASE LINK". The remote user must have the privilege "CREATE SESSION". On the remote host: SQL> connect system/<pass_word> SQL> GRANT CREATE SESSION TO scott; SQL> ALTER USER scott ACCOUNT UNLOCK; On the local host: SQL> connect system/<pass_word> SQL> GRANT CREATE DATABASE LINK, CREATE PUBLIC DATABASE LINK, CREATE SESSION TO scott; SQL> ALTER USER scott ACCOUNT UNLOCK; SQL> connect scott/tiger SQL> CREATE DATABASE LINK dblink2 CONNECT TO scott IDENTIFIED BY tiger USING 'host2_db2'; SQL> SELECT * FROM dept@dblink2; SQL> INSERT INTO dept@dblink2 VALUES(...); SQL> DELETE FROM dept@dblink2 WHERE deptno = ...; SQL> UPDATE dept@dblink2 SET dname = '...' WHERE deptno = ...;
SQL> COMMIT; SQL> SELECT emp.ename, emp.deptno, dept.dname FROM emp, dept@dblink2 WHERE emp.deptno = dept.deptno; END OF BASIC DATABASE LINK ------------------------------------------------Advanced database link. The following statement works on Oracle 10.2 onwards. Create a database link using Easy Connect method without modifying tnsnames.ora. SQL> CREATE DATABASE LINK dblink3 CONNECT TO peter IDENTIFIED BY password USING 'DBHOST:1521/OLTP'; SQL> SELECT * FROM dept@dblink3; ------------------------------------------------Connect to a remote database using Easy Connect without any database link: $ sqlplus scott/tiger@DBHOST:1521/OLTP or $ sqlplus scott/tiger@DBHOST/OLTP -- The default port is 1521 or $ sqlplus scott/tigerd@DBHOST -- The default service name is the valud of -- DEFAULT_SERVICE_listener_name in listener.ora