We want to access the NASREEN schema objects (resides on BATCH46 database) from source db (TESTDB Database)Â
SQL> select name, open_mode from v$database;
NAME OPEN_MODE
--------- ----------
TESTDB READ WRITE
SQL> show user
USER is "SYS"
SQL>
CREATE PUBLIC DATABASE LINK link_name
CONNECT TO remote_user_name
IDENTIFIED BY remote_user_password
USING 'remote_service_name';
SQL>
— Create Public db link without modify TNS entry
CREATE PUBLIC DATABASE LINK link_three
CONNECT TO NASREEN IDENTIFIED BY abc123
using
' (DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = xx.xxx.xxx.xx)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = BATCH46)
)
)'
/
SQL> show user
USER is "SYS"
— OR —
Â
— Create PUBLIC DB Link Using EASY CONNECT
CREATE PUBLIC DATABASE LINK link_five
CONNECT TO NASREEN IDENTIFIED BY abc123
USING 'xx.xxx.xxx.xx:1521/BATCH46';
6.Create PRIVATE database Link
Private database link belongs to a specific schema of a database. Only the owner of a private database link can use it.
[php]
CREATE DATABASE LINK link_name
CONNECT TO remote_user_name
IDENTIFIED BY remote_user_password
USING ‘remote_service_name’;
SQL> create user C##ABC identified by abc123;
User created.
SQL> GRANT CONNECT TO C##ABC;
Grant succeeded.
SQL> grant create database link to C##ABC;;
Grant succeeded.
[php]
SQL> conn C##ABC; <-- If you don't know password then use proxy user . PROXY USER
Connected.
SQL>
SQL> CONN C##ABC/abc123
Connected.
.
SQL> CREATE DATABASE LINK LINK_PRIVATE1
CONNECT TO NASREEN IDENTIFIED BY abc123
USING 'BATCH46';
2 3
Database link created.
Â
— OR —
— Create PRIVATE db link without modify TNS entry
CREATE DATABASE LINK LINK_PRIVATE2
CONNECT TO NASREEN IDENTIFIED BY abc123
using
'(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = xx.xxx.xxx.xx)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = BATCH46)
)
)'
/
— OR —
-- Create DB Link Using EASY CONNECT
CREATE DATABASE LINK LINK_PRIVATE3
CONNECT TO NASREEN IDENTIFIED BY abc123
USING 'xx.xxx.xxx.xx:1521/BATCH46';
SQL>
SQL> conn / as sysdba
Connected.
SQL> revoke create database link from C##ABC; <----
[/php]
7. List database links again
set lines 180 pages 999
col owner for a15
col DB_LINK for a15
col USERNAME for a15
col HOST for a39
col CREATION_DATE for a20
select owner, db_link, username,host ,to_char(created,'MM/DD/YYYY HH24:MI:SS') creation_date from dba_db_links;
8. Verify the db link results
 — Since it is public db link any user in source database can access the TEST schema objects of targert db
 — Even new user can access. eg…. create new user <—-
— verfiy public db links —
SQL> create user C##TWO identified by one; <----
User created.
[/php]
SQL> grant connect to C##TWO;
Grant succeeded.
SQL> conn C##TWO/two; <----
Connected.
SQL> select count(*) from employee@link_two; <-- with TNS entry
COUNT(*)
----------
2
[/php]
SQL> select count(*) from employee@link_three; <-- with TNS Connect String
COUNT(*)
----------
2
[/php]
drop public database link LINK_TWO;
drop public database link LINK_THREE;
drop public database link LINK_FOUR;
drop public database link LINK_FIVE;
10.Drop Private Database link
— Please login as owner of db link
SQL> conn C##ABC/abc123; <----
Connected.
drop database link LINK_PRIVATE1;
drop database link LINK_PRIVATE2;
drop database link LINK_PRIVATE3;
[/php]
SQL>
SQL> conn / as sysdba
Connected.
set lines 180 pages 999
col owner for a15
col DB_LINK for a15
col USERNAME for a15
col HOST for a39
col CREATION_DATE for a20
select owner, db_link, username,host ,to_char(created,'MM/DD/YYYY HH24:MI:SS') creation_date from dba_db_links;