In Oracle, a database link (dblink) is a schema object in one database that enables you to access objects in another database. This is useful for distributed database systems where you need to perform operations or retrieve data from a remote database.
Creating a Database Link
Basic Syntax
CREATE DATABASE LINK dblink_nameCONNECT TO remote_user IDENTIFIED BY remote_password
USING 'tns_service_name';
dblink_name
: The name of the database link.remote_user
: The username on the remote database.remote_password
: The password for the remote user.tns_service_name
: The TNS service name that resolves to the remote database.
Examples
1. Creating a Basic Database Link
Assume you have a remote database with the TNS service name remote_db
and a user remote_user
with password remote_password
.
CREATE DATABASE LINK remote_dblinkCONNECT TO remote_user IDENTIFIED BY remote_password
USING 'remote_db';
2. Using a Database Link
After creating the dblink, you can use it to query data from the remote database.
Querying a Table
SELECT * FROM employees@remote_dblink;
Joining Local and Remote Tables
SELECT l.local_column, r.remote_columnFROM local_table l
JOIN remote_table@remote_dblink r ON l.id = r.id;
Performing DML Operations
You can also perform DML operations like INSERT
, UPDATE
, and DELETE
using the database link.
Inserting Data
INSERT INTO employees@remote_dblink (employee_id, first_name, last_name)VALUES (1001, 'John', 'Doe');
Updating Data
UPDATE employees@remote_dblinkSET salary = salary * 1.1
WHERE department_id = 10;
Deleting Data
DELETE FROM employees@remote_dblinkWHERE employee_id = 1001;
Managing Database Links
1. Viewing Existing Database Links
You can query the DBA_DB_LINKS
or ALL_DB_LINKS
view to see existing database links.
SELECT db_link, username, hostFROM dba_db_links;
Or for the current user:
SELECT db_link, username, hostFROM all_db_links;
2. Dropping a Database Link
To drop a database link, use the DROP DATABASE LINK
statement.
DROP DATABASE LINK remote_dblink;
3. Public Database Links
A public database link can be accessed by all users in the database.
Creating a Public Database Link
CREATE PUBLIC DATABASE LINK public_dblinkCONNECT TO remote_user IDENTIFIED BY remote_password
USING 'remote_db';
Dropping a Public Database Link
DROP PUBLIC DATABASE LINK public_dblink;
Considerations
Security: Be cautious with the credentials used in database links. Ensure that the remote user has the appropriate privileges and that sensitive information is protected.
Performance: Accessing remote databases can introduce network latency. Optimize queries and consider using materialized views for frequently accessed remote data.
Dependency Management: Changes in the remote database, such as schema changes, can impact the functionality of the database link.
Privileges: Creating a database link requires the
CREATE DATABASE LINK
privilege. For public database links, you need theCREATE PUBLIC DATABASE LINK
privilege.
Use Cases
- Distributed Databases: Ideal for accessing and integrating data from distributed databases.
- Data Migration: Useful for migrating data between databases.
- Reporting and Analytics: Enable consolidated reporting from multiple data sources.
- Application Integration: Allow different applications to share and access data seamlessly.
By leveraging database links, you can facilitate seamless communication and data exchange between different Oracle databases, enabling more robust and integrated database solutions.