Wednesday, January 16, 2013

ORACLE Database Link

Database links allow users to access a remote database.

How to create private db link ?
At remote database: 
1.create a user for db link and grant schema privileges to him:
create user dblink_user identified by password;
grant create session to dblink_user;
grant select on s_role to dblink_user;

At source database:
1. Add tnsname.ora file:
dbname =
  (description =
    (address = (protocol = tcp)(host = dbname.com)(port = 1521))
    (address = (protocol = tcp)(host = dbname_v.com)(port = 1521))
    (load_balance = yes)
    (connect_data =
      (server = dedicated)
      (service_name = dbname.com)
    )
  )

2.connect as a schema owner, create private db links and verify:
grant create database link to schema_owner; (if not exists)
conn schema_owner/password
create database link dblink1 connect to dblink_user identified by password using 'dbname';
select * from dba_db_links; [verification]

How to access remote database using dblink ?

1) Access remote database objects at local database: Example -
select count(*) from remote_schema_name.table_name@dblink1
;
2) Access the data from remote database and insert into local database : Example -
INSERT INTO schema_name.table_name
select * from remote_schema_name.table_name@dblink1
;
Note: Try to avoid PUBLIC dblink for security and audit purpose.






No comments:

Post a Comment