Useful shortcuts for vi editor

Tuesday 13 September 2016

How to define new db link?

Add a new connection node into tnsnames.ora file.
vi $ORACLE_HOME/network/admin/tnsnames.ora
e.g.
ALPER_DB =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.10)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = XE )
    ))

Check remote host via tnsping
$ tnsping ALPER_DB

Define a new db link
For specific user:
SQL> CREATE DATABASE LINK "ALPER_DATABASE"
 CONNECT TO hr
 IDENTIFIED BY hr
 USING 'ALPER_DB';

If you desire all users use this link you should add "public" into create statement
For All users (public):
SQL> CREATE PUBLIC DATABASE LINK "ALPER_DATABASE"
 CONNECT TO hr
 IDENTIFIED BY hr
 USING 'ALPER_DB';

Alternative option: define a new db link
For specific user:
SQL> CREATE DATABASE LINK "ALPER_DATABASE"
 CONNECT TO hr
 IDENTIFIED BY hr
 USING '(DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.10)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = XE )
    ))';

Observe the new link and check it
For specific users:
SQL> select * from user_db_links where db_link = 'ALPER_DATABASE';
SQL> select * from employees@ALPER_DATABASE
For All users (public):
SQL> select * from all_db_links where db_link = 'ALPER_DATABASE';
SQL> select * from employees@ALPER_DATABASE

Extra
Connect without db link:
$ sqlplus "hr@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=10.10.10.10)(Port=1521))(CONNECT_DATA=(SID=XE)))"
or
CMD> sqlplus hr@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=10.10.10.10)(Port=1521))(CONNECT_DATA=(SID=XE)))