Sei sulla pagina 1di 2

=============================================================

Subject: Database Links for Oracle Database 11g


Purpose: To demonstrate Oracle Database Links
=============================================================
Database Version :
Oracle Database 11g --- Oracle Database 11g
Instance Name
:
local_db
--- remote_db
============================================================
Example
------The user SCOTT wants to manipulate data of the database "remote_db"
on a remote host "remote_db.xxx.com" through the port 1521 with the default list
ener
on the remote host.
-------------------------------------------------------------------On the local host local_db.xxx.com
Login: oracle
$ ORACLE_SID=local_db ; export ORACLE_SID
$ echo $ORACLE_SID
$ cp -p $ORACLE_HOME/network/admin/tnsnames.ora $ORACLE_HOME/network/admin/tnsna
mes_orig.ora
$ vi $ORACLE_HOME/network/admin/tnsnames.ora
...
remote_db =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.20)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = remote_db)
)
)
------------------------------------------------------------------Grant some privileges to database users who will create database links.
The local database users must have the privileges "CREATE DATABASE LINK,
CREATE PUBLIC DATABASE LINK". The remote user must have the privilege "CREATE SE
SSION".
------------------------------------------------------------------On the remote host remote_db.xxx.com
Login: oracle
$ ORACLE_SID=remote_db ; export ORACLE_SID
$ echo $ORACLE_SID
$ sqlplus system/<password>
SQL> GRANT CREATE SESSION TO scott;
SQL> ALTER USER scott ACCOUNT UNLOCK;
------------------------------------------------------------------Now, create and use database links.
------------------------------------------------------------------On the local host local_db.xxx.com
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> ALTER USER scott IDENTIFIED BY tiger;
SQL> connect scott/tiger
SQL> CREATE DATABASE LINK rad_orcl_dbl
CONNECT TO "scott" IDENTIFIED BY "tiger"
USING 'remote_db';

SQL> SELECT *
FROM dept@remote_db;
SQL> INSERT INTO dept@remote_db
VALUES(...);
SQL> DELETE FROM dept@remote_db
WHERE deptno = ...;
SQL> UPDATE dept@remote_db
SET dname = '...'
WHERE deptno = ...;
SQL> COMMIT;
To join a local table "emp" to a remote table "dept":
SQL> SELECT emp.ename, emp.deptno, dept.dname
FROM emp, dept@remote_db
WHERE emp.deptno = dept.deptno;
===============================================================

Potrebbero piacerti anche