SQL Developer
database comparison
SQL queries
data analysis
database management

Compare tables from two different databases in SQL Developer

Codemia Knowledge Hub ·

In Oracle SQL Developer, connect to both databases and run Tools → Database Diff to compare table structures. For the data itself, write MINUS and JOIN queries that show rows missing from one side or values that differ. Both are common during data migrations, schema changes, and consistency checks across distributed systems.

What makes cross-database comparison hard

  1. Differences in schema: tables can differ in columns, data types, and constraints.
  2. Data discrepancies: rows may be missing or altered between databases.
  3. Performance: large datasets need careful resource use.
  4. Connectivity: the two database systems may use different protocols.

Steps in SQL Developer

  1. Set up connections:
    • Open SQL Developer and create connections to both the source and target databases.
    • Make sure you have the required access permissions on both.
  2. Run Database Diff:
    • Go to Tools → Database Diff.
    • Select the source and target connections.
    • Choose the schemas and objects to compare.
  3. Review the comparison:
    • The diff shows differences in table structure, including columns, data types, constraints, and indexes.
    • From the results you can generate scripts to synchronize them.
  4. Compare data with SQL queries:
    • To find rows that exist on only one side, combine MINUS in both directions:
sql
1SELECT column_name FROM (
2    SELECT column_name FROM source_table
3    MINUS
4    SELECT column_name FROM target_table
5) UNION ALL
6SELECT column_name FROM (
7    SELECT column_name FROM target_table
8    MINUS
9    SELECT column_name FROM source_table
10);
  1. Synchronize:
    • SQL Developer can generate scripts for schema differences.
    • For data differences, write your own scripts to update, insert, or delete rows as needed.

Example: comparing an EMPLOYEES table

Take two databases, DB_SOURCE and DB_TARGET, each with an EMPLOYEES table keyed on EMP_ID. Use Database Diff for column differences, then queries for the data. A query in one Oracle database reaches a table in another through a database link, so run these in DB_SOURCE after creating a link to DB_TARGET:

sql
1CREATE DATABASE LINK DB_TARGET
2  CONNECT TO hr IDENTIFIED BY password
3  USING 'db_target';
4
5-- Query to find missing records in DB_TARGET
6SELECT EMP_ID
7FROM EMPLOYEES
8MINUS
9SELECT EMP_ID
10FROM EMPLOYEES@DB_TARGET;
11
12-- Query to find differing records between the databases
13SELECT src.EMP_ID, src.NAME, tgt.NAME
14FROM EMPLOYEES src
15JOIN EMPLOYEES@DB_TARGET tgt
16ON src.EMP_ID = tgt.EMP_ID
17WHERE src.NAME <> tgt.NAME;

Fixing the mismatches

Once you know what differs, insert the missing rows and update the mismatched ones. For example, to copy names from source to target where they differ:

sql
1UPDATE EMPLOYEES@DB_TARGET tgt
2SET NAME = (SELECT src.NAME FROM EMPLOYEES src WHERE src.EMP_ID = tgt.EMP_ID)
3WHERE EXISTS (
4    SELECT 1 
5    FROM EMPLOYEES src
6    WHERE src.EMP_ID = tgt.EMP_ID AND src.NAME <> tgt.NAME
7);

Steps and tools at a glance

Feature/StepDescriptionExample/Tool Use
Setting Up ConnectionsEstablish connections to source and target databases in SQL Developer.SQL Developer GUI
Schema ComparisonUse Database Diff to compare tables' structures.Tools → Database Diff
SQL Data ComparisonExecute SQL queries to identify data discrepancies.MINUS and UNION ALL SQL operations
Data SynchronizationAuto-generate scripts or manually create SQL scripts for syncing data/schema changes.Synchronization scripts

Other checks before you compare

  • Data type mappings: keep data types consistent across databases to avoid truncation or conversion problems.
  • Network configuration: confirm firewall and security settings allow cross-database operations.
  • Audit and logging: keep logs of comparisons and changes for auditing and rollback.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design