0% found this document useful (0 votes)
152 views2 pages

Oracle 12c - PL/SQL Tables Replication Between Databases

This document discusses replicating tables between Oracle databases using PL/SQL. It describes creating an initial full copy of tables from the source to destination using CTAS statements. For incremental copies, a PL/SQL package is created to select new or changed rows from the source tables based on a primary key and insert them into the destination tables. The document also explores using the DBMS_COMPARISON package to synchronize tables and check for discrepancies between databases. Scheduling jobs using DBMS_JOB or DBMS_SCHEDULER is recommended to automate the replication process.

Uploaded by

admir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views2 pages

Oracle 12c - PL/SQL Tables Replication Between Databases

This document discusses replicating tables between Oracle databases using PL/SQL. It describes creating an initial full copy of tables from the source to destination using CTAS statements. For incremental copies, a PL/SQL package is created to select new or changed rows from the source tables based on a primary key and insert them into the destination tables. The document also explores using the DBMS_COMPARISON package to synchronize tables and check for discrepancies between databases. Scheduling jobs using DBMS_JOB or DBMS_SCHEDULER is recommended to automate the replication process.

Uploaded by

admir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Oracle 12c – PL/SQL tables replication between databases

1 Introduction
There are many ways to implement table/schema/database replication in Oracle databases. We won’t here explain native
Oracle RDBMS server replication funcionalities, like Oracle DataGuard, physical standby database, logical standby
database or Oracle Streams Replication. This brief report will consider pl/sql replication implementation based on limited
numbers of source database tables and using db links between source database and target database.

2 Chosen scenarios
We will refer just two scenarios:
 incremental copy of data from source tables to destination tables
 full copy of data from source tables to destination tables

3 Incremental data copy


Incremental data copy consider first creating full copy of source tables to destination database schema, and then creating
pl/sql procedure on destination database and schedule job to do incremental copy on chosen interval ( for example every
week ). For initial full copy of source database tables , we should use create table as select - CTAS statements. For
example:
create table t_copy as select * from t_original@mydb_link.com;
Good practice is create pl/sql package ( for example INITIAL_COPY_PCK) for initial copy which can be used if
replication/sinc fails. In this package should be procedure for droping db objects that initial copy create again. Because we
have to use DDL statements this pl/sql packege will use dynamic sql.
So structure of INITIAL_COPY_PCK would be:
 procedure for droping objects.
For example: execute immediate ‘drop table t_copy’;
 procedure for crating objects as select.
For example: execute immediate ‘create table t_copy as select * from t_original@mydb_link.com’;
 procedure for recreating indexes an or foreign key constraints
For example: execute immediate 'ALTER TABLE t_copy ADD CONSTRAINT pk_t_copy_id PRIMARY KEY(ID)';
After creating initial copy of tables on destination database, we need to create PL/SQL package/procedure that would do
sync on scheduled job. For example that package is REPLICATION_PCK.
If source tables have primary keys – that in most case ID sequence columns, we can just do insert select statements from
last copied sequence number ( or some other unique column identifier ).
So in this case we can use next generic code:
select max(id) into l_copied_id from t_copy;
insert into t_copy select * from t_original@mydb_link.com where id > l_copied_id;
Another way is using Table Synchronization using DBMS_COMPARISON package. The dbms_comparison package can
be used to compare objects; if there is any discrepancy, they can be re-synchronized. Some prerequisites exist in order to
use this package:
 The source database must be an Oracle 11g Release 1 or later.
 The destination database must be an Oracle database.

1
Oracle 12c – PL/SQL tables replication between databases

 The character set must be the same.


Assume that we want to compare two tables between ora1 and ora2 databases named before as t_original and tab_copy. If
any divergence is found, synchronize them and then check the data that must be equalized at the end of the process. In
sum, the most useful views pertaining to comparisons include:
 dba_comparison: Displays information about all comparisons created in a database
 dba_comparison_columns: Displays information about table columns that belong to a comparison
 dba_comparison_scan: Each time the compare function is executed, a new scan id is created. This view
displays information about all executed scans.
 dba_comparison_scan_values: Displays value details for all comparisons
 dba_comparison_row_dif: This view displays information about different data values for all comparisons
As we mentioned before, this package can be used with different objects and schemas. For the record sync is two way.
Very good examples of this process can be found on :
http://www.dba-oracle.com/t_packages_dbms_comparison.htm
http://www.nazmulhuda.info/table-synchronization-using-dbms_comparison

4 Full copy
Full copy of source database tables on destination database is nothing else then executing procedures / packages
procedures INTIAL_COPY_PCK on scheduled timing. For example every week.

5 Options
 Bulk insert is not supported trough database links
 For that you need to use global temporary tables and we wouldn’t recommend that
 Source: https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:1001450700346544769

6 Conclusion
Regarding above,
we recommend next scenario for data replication trough database link:
1. Using Incremental copy option with (if possible unique sequence in tables) because DBMS_COMPARISON
package do synchronization in both databases by default.
2. Creating schedule DBMS_JOB or DBMS_SCHEDULER to automate process of synchronization between source
and destination tables.
3. Using PARALLEL hints is not option trough database links but it can be used
insert/*+ append */ into t_copy select * from t_original@mydb_link.com where id > l_copied_id;

You might also like