Home > Oracle Tips > Oracle Database Administrator > How to create a database link in Oracle
Oracle Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ORACLE DATABASE ADMINISTRATOR

How to create a database link in Oracle


Elisa Gabbert, Associate Editor
01.04.2008
Rating: -4.35- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Part of our "How do I do that in Oracle?" series, this tip, compiled largely of expert advice from our Oracle database design and architecture expert Brian Peasland, explains in detail how to create a database link in Oracle, as well as how to troubleshoot errors with your db link.


A database link creates a connection between a local database and a remote database. You might want to create a database link, for instance, if you want the data in a remote database updated when the local database is updated. Here's how to accomplish this:

The first thing you need to do is to create a database link pointing to the other location. The database link can be created with a command similar to the following:

CREATE DATABASE LINK other_db CONNECT TO scott IDENTIFIED BY tiger

USING 'tns_alias';

You'll want to give the database link a better name, use the appropriate userid/password to connect to the remote database, and configure your TNSNAMES.ORA file with a TNS alias to point to that database. You can test the database link with a simple query like the following:

SELECT sysdate FROM dual@other_db;

The "@other_db" clause uses the database link you created. If the query returns the date, then the link works properly. Once the link is set up, you can either issue a command to modify the data in the remote database when you modify the local database, or you can use a database trigger. For instance, if the application modifies the local database by performing an INSERT similar to the following,

INSERT INTO my_table VALUES (1,'Two');
then you can also code another SQL statement to insert the same values across a database link.
INSERT INTO my_table@other_db VALUES (1,'Two');

Additionally, you can create a trigger:

CREATE TRIGGER modify_remote_my_table
BEFORE INSERT ON my_table
BEGIN
  INSERT INTO my_table@other_db VALUES(:new.colA, :new.colB);
END;
/

You can write similar triggers for updating and deleting rows as well. (From "Updating data in remote database when local database is updated")

You can also use a database link to pull data from a remote database to a local one. To pull data through a database link, you need to create a database link to the remote database. You should be able to construct SQL commands to insert data into the local database, selecting from the remote database. A SQL statement might look like the following:

INSERT INTO my_local_table (ColA, ColB, ColC)
SELECT ColA, ColB, ColC FROM remote_table@remotedb
WHERE conditions are true;

The SQL language in these situations is a very powerful and flexible tool that can't be beat. But you'll have to write your own routines to move the data. (From "Getting data from remote 8i database to 9i)

Database links can also be used for partial database replication. Oracle has a product called Replication Services which performs this type of replication, but this product costs additional funds which may not be justifiable for replicating a small subset of your database. If that is the case, then you can write your own replication routines using database triggers and database links.

  1. Create the database link to the remote database:
    CREATE DATABASE LINK link_name CONNECT TO 
    username IDENTIFIED BY password USING 'tns_alias';
    
  2. Create a trigger on the table which populates newly inserted rows into the remote database table.
    CREATE TRIGGER my_table_insert_trig
    BEFORE INSERT ON my_table
    BEGIN
       INSERT INTO my_table@link_name VALUES 
       (:new.colA, :new.colB, ..., :new.colX);
    END;
    /
    

You'll have to create your own triggers similar to the above which perform your replication for you. (From "Partial database replication setup")

You can use a database link to migrate a database or copy data from one database to another with the same structure. For instance, to migrate from Oracle 9i to Oracle 10g: Create an Oracle 10g database on your target server and create a database link from the Oracle 10g database to the Oracle 9i database. Run 'create table as select …' (CTAS) statements to create your tables with the data in the Oracle 10g database. Remember to also create any required indexes, constraints and referential integrity. (From "Migrating 9i database on Windows to 10g on SLES)

In addition, you can create database links between an Oracle database and a non-Oracle database such as SQL Server or DB2. For this use Oracle Heterogeneous Services (formerly known as Oracle Transparent Gateways).

If you run into errors after creating your database link, read the following advice.

You must correctly configure your TNSNAMES.ORA file or you will get the ORA-12154 error (TNS: could not resolve service name). One thing that people don't understand about database links is that it will only look in $ORACLE_HOME/network/admin for a TNSNAMES.ORA file. And this file must reside on the server that the database is running! So correctly configure this file in this location for your database links. Always test database links by issuing the following query:

SELECT sysdate FROM dual@remotedb;

If you get the results back, then the link is working fine. Otherwise, fix the corresponding error. (From "Copying tables from one DB to another via database link")

The ORA-12505 error, on the other hand, simply means that the ORACLE_SID in your TNS alias does not match any ORACLE_SID defined for the database listener. You'll have to make sure your TNS alias SID matches the one defined in the listener's LISTENER.ORA configuration file. (From "DB link only works in one direction")

To troubleshoot the ORA-02068 error: The database link requires a TNS alias, which you specified in the USING clause of the CREATE DATABASE LINK command. Outside of Oracle, check to ensure that you can use this TNS alias to connect to the database. Using SQL*Plus, see if you can connect to this remote database:

sqlplus system/password@tns_alias

My guess is that the above will give you the same error. This means that either the TNS alias is defined incorrectly, or the instance is not really running. If you can connect with SQL*Plus, the database link should work just fine. (From "ORA-02068 with database link")

Reader feedback

Jim Amos writes: "Prove that a database link goes to the exact database you want by one of the two queries below. These queries are a more specific test than selecting sysdate from dual@dblink_name.


SELECT * from global_name@dblink_name;

Or this one:

SELECT instance_name FROM V$INSTANCE@dblink_name;

Return to "How do I do that in Oracle?"

Rate this Tip
To rate tips, you must be a member of SearchOracle.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Oracle Database Administrator
Import on one table from dump file
Error during RMAN backup
Can I drop a column in SYS schema?
STATSPACK tool: transaction vs. execution measurement
Should I port from Microsoft Access?
Installing multiple Oracle homes
How can I find statistics on total memory usage and database connections?
Modifying SYS password in a RAC environment
How to create Datafiles in a Data Guard (10g) environment
Importing tables with integrity constraints

Oracle database design
Weighing remote database administration pros and cons takes care
Oracle Database 11g makes waves at Burlington Coat Factory
Data modeling tools no substitute for hard work
How do I do that in Oracle?
The Oracle Database user's guide to Oracle OpenWorld 2007
Oracle OpenWorld 2007 Special Report
How many redo log files?
How to move tables from system tablespace to user tablespace
LAST_OPER_TYPE column in v$sga_dynamic_components
ORA-12560 error with Oracle 10g Instant Client

Oracle tutorials, tips and FAQs
Most clicked stories of 2007
How do I do that in Oracle?
Counting a row's NULL columns
How many redo log files?
How to start multiple instances in Oracle 10g
Is it possible to assign a role to a profile?
Difference between RECYCLEBIN and 'DROPPED' column
What is the System Global Area (SGA)?
How to find transactions being committed to Oracle database
How to create a database schema in Oracle?
Oracle tutorials, tips and FAQs Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
extent  (SearchOracle.com)
field  (SearchOracle.com)
flexfield  (SearchOracle.com)
foreign key  (SearchOracle.com)
quad tree  (SearchOracle.com)
record  (SearchOracle.com)
row  (SearchOracle.com)
splay tree  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsTipsAsk the ExpertsWebcastsWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts