Home > Oracle Database / Applications 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.39- (out of 5)


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


[IMAGE]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:

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:

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, then you can also code another SQL statement to insert the same values across a database link.

Additionally, you can create a trigger:

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...


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



RELATED CONTENT
Oracle database administrator
Understanding SQL string functions
What is the difference between a database engineer, architect and 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?
How can I find statistics on total memory usage and database connections?
Installing multiple Oracle homes
Modifying SYS password in a RAC environment

Oracle database design and architecture
How to use V$SEGMENT_STATISTICS to find the most accessed Oracle table
Can I install an Oracle client on Windows 7?
How to use the Oracle Database Upgrade Assistant (DBUA)
Can I specify Oracle column order in my database table?
Can I have a single Oracle 11g RAC instance across multiple databases?
How to use the Oracle export utility to duplicate database structure
How to choose the primary key columns in an Oracle table
Understanding the data archiving definition
Review: Oracle's 11g R2 database has some good and bad
Scaling an Oracle database: What is the best strategy for you?

Oracle tutorials, tips and FAQs
Part 2: Maintaining your place in the race -- ERP project management
Oracle virtualization introduction guide
Revving your engines: Tuning up your ERP project plan
Oracle Openworld 2009: Here's what to expect
Oracle OpenWorld 2009 Special Report
SearchOracle.com podcasts
Do I need to buy an Oracle license for a development database?
Special Report: Collaborate '09
IOUG on 2009 Collaborate conference: what's hot, what's not
Most clicked stories of 2007
Oracle tutorials, tips and FAQs Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
E. F. Codd  (SearchOracle.com)
extent  (SearchOracle.com)
flexfield  (SearchOracle.com)
foreign key  (SearchOracle.com)
multidimensional database  (SearchOracle.com)
object-oriented database management system  (SearchOracle.com)
quad tree  (SearchOracle.com)
relational online analytical processing  (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


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:

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:
  2. Create a trigger on the table which populates newly inserted rows into the remote database table.

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:

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:

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. Or this one:

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




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.



Oracle Development Solutions - SQL, J2EE, XML, SOA
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




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