SQL query to compare two tables in different databases in Oracle

Oracle is a versatile database and it is highly secured and hence to a large extent used in banking and insurance applications. Though NoSQL database dominates in many industries, still oracle has its own significance because most the legacy applications still like to go with oracle like RDBMS only.  Distributed systems will have the usage of 2 or more oracle databases and hence there occurs the scenario of comparing data in all tables in different databases of oracle. Similarly, SQL Server is a versatile most wanted RDBMS and because of its security features, it is highly used.

Let us see how to compare data in all tables in two different databases in SQL Server:

Step 1: Create databases for  employee i.e employeeData 1 and employeeData 2.

Query:

-- employeeData1 database is created
CREATE  DATABASE employeeData1; 
-- Making employeeData1 as active database
USE employeeData1; 

-- Create a table named employees under employeeData1 
CREATE TABLE employees 
( EMPLOYEEID int NOT NULL,   
  EMPLOYEENAME varchar(50) NOT NULL,   
  EMPLOYEECITY varchar(50)   
)
-- employeeData2 database is created
CREATE  DATABASE employeeData2; 
-- Making employeeData2 as active database

USE employeeData2; 
GO
CREATE TABLE employees 
( EMPLOYEEID int NOT NULL,   
  EMPLOYEENAME varchar(50) NOT NULL,   
  EMPLOYEECITY varchar(50)   
)

Step 2: Insert a values in database.

Query:

GO
INSERT INTO employees (employeeId,employeeName,employeeCity) 
VALUES (1,'XXX','CHENNAI')
SELECT * FROM employeeData2.dbo.employees;

Output:

SQL query to compare two tables in different databases in Oracle


SQL query to compare two tables in different databases in Oracle

employeeData2

Step 3: Now let us see the comparison of data between this 2 different database of the employees table.

  • Using INTERSECT:

Query:

--INTERSECT - IT WILL DISPLAY  
-- ONLY COMMONLY OCCURING ROWS IN BOTH TABLES

SELECT * FROM employeeData1.dbo.employees INTERSECT
SELECT * FROM employeeData2.dbo.employees;

Output:

SQL query to compare two tables in different databases in Oracle

  • Using UNION:

Query:

--UNION - WILL COMBINE ALL THE ROWS 
--IN BOTH TABLES BUT IGNORES DUPLICATES
SELECT * FROM employeeData1.dbo.employees UNION
SELECT * FROM employeeData2.dbo.employees;

Output:

SQL query to compare two tables in different databases in Oracle

  • Using UNION ALL:

Query:

-- UNION ALL - WILL COMBINE ALL THE ROWS IN 
-- BOTH TABLES BUT WILL HAVE  DUPLICATES AS WELL.
SELECT * FROM employeeData1.dbo.employees UNION ALL
SELECT * FROM employeeData2.dbo.employees;

Output:

SQL query to compare two tables in different databases in Oracle

  • Using EXCEPT:

Query:

-- EXCEPT - IT WILL DISPLAY  ONLY 
-- UNCOMMON ROWS OF BOTH TABLES
SELECT * FROM employeeData1.dbo.employees EXCEPT
SELECT * FROM employeeData2.dbo.employees;

Output:

SQL query to compare two tables in different databases in Oracle

Only Uncommon rows are present

Conclusion:

By using INTERSECT, UNION, UNION ALL, and EXCEPT, we can compare the data for a single database or even with the different database as well.

Structured Query Language supported by RDBMS(Relation Database Management Systems) such as Oracle, Mysql, PostgreSQL, and SQL Server. All relational database system support objects such as databases, tables, schema, indexes, views, functions, procedures, and many more.

RDBMS system performs best when the schema is on write. To make these systems perform well on the schema on reading we have indexes that help in improving the data read or search performance.

Comparing indexes of tables from two different databases requires their data and schema structure to be compared first. In a hierarchy of database management systems, we have a schema, databases, tables, columns then indexes.

Consider two different databases “Department_HR”  and “Department_CR” 

Step 1: Creating users for the databases.

create user source PASSWORD 's';
ALTER USER source WITH SUPERUSER;

SQL query to compare two tables in different databases in Oracle

create user target PASSWORD 't';

SQL query to compare two tables in different databases in Oracle

Step 2: Creating Database “Department_HR” and “Departement_CR”

create database Department_HR OWNER source;

SQL query to compare two tables in different databases in Oracle

create database Department_CR OWNER target;

SQL query to compare two tables in different databases in Oracle

Step 3: Create table “Employees”  in Database “Department_HR” and “Department_CR”

Create table Department_HR.Employees
(  EMPLOYEE_ID int,
  FIRST_NAME varchar(120),
  LAST_NAME varchar(120),
  EMAIL varchar(120),
  HIRE_DATE date,
  JOB_ID  varchar(120),
  SALARY float,
  COMMISSION_PCT float,
  MANAGER_ID int,
  DEPARTMENT_ID int
);

SQL query to compare two tables in different databases in Oracle

Create table Department_CR.Employees
(  EMPLOYEE_ID int,
  FIRST_NAME varchar(120),
  LAST_NAME varchar(120),
  EMAIL varchar(120),
  HIRE_DATE date,
  JOB_ID  varchar(120),
  SALARY float,
  COMMISSION_PCT float,
  MANAGER_ID int,
  DEPARTMENT_ID int
);

SQL query to compare two tables in different databases in Oracle

Step 4: Insert data in the department_HR.employees and department_CR.employees tables

insert into employees values(100,'Steven',
'King','SKING','17-Jun-03','AD_PRES',24000, null ,90);
insert into employees values(101,'Neena','
Kochhar','NKOCHHAR','21-Sept-05','AD_VP',17000,100,90);
insert into employees values(102,'Lex','DeHaan',
'LDEHAAN','13-Jan-01','AD_VP',17000,100,90);
insert into employees values(103,'Alexander',
'Hunold','AHUNOLD','03-Jan-06','IT_PROG',9000,102,60);
insert into employees values(104,'Bruce','
Ernst','BERNST','21-May-07','IT_PROG',6000,103,60);
insert into employees values(105,'David',
'Austin','DAUSTIN','25-Jun-05','IT_PROG',4800,103,60);
insert into employees values(106,'Valli',
'Pataballa','VPATABAL','05-Feb-06','IT_PROG',4800,103,60);
insert into employees values(107,'Diana',
'Lorentz','DLORENTZ','07-Feb-07','IT_PROG',4200,103,60);
insert into employees values(108,'Nancy',
'Greenberg','NGREENBE','17-Aug-02','FI_MGR',12008,101,100);

Step 5: Select the inserted data in the department_HR.employees and department_CR.employees table

select * from department_HR.employees;

SQL query to compare two tables in different databases in Oracle

select * from department_CR.employees;

SQL query to compare two tables in different databases in Oracle

Step 6: Compare the indexes from both databases we must have indexes on the table’s column

create index indx_dept_emp_id on
department_HR.employees(employee_id);
create index indx_dept_manager_id 
on department_HR.employees(manager_id);

SQL query to compare two tables in different databases in Oracle

create index indx_dept_emp_id on 
department_CR.employees(employee_id);
create index indx_dept_manager_id on 
department_CR.employees(manager_id);

SQL query to compare two tables in different databases in Oracle

Step 7: Create a link between two different databases to compare the indexes

Login to database  Source database Department_HR and create a DB LINK

  • Create an extension to establish a connection between two databases
CREATE EXTENSION dblink;

SQL query to compare two tables in different databases in Oracle

  • Create a server to make a connection to the target database.
CREATE SERVER server_Department_CR_remote 
FOREIGN DATA WRAPPER 
 dblink_fdw OPTIONS (host 'localhost', 
 dbname 'Department_CR', port '1521');

SQL query to compare two tables in different databases in Oracle

  •  Create user mapping from the source database for the target database.
CREATE USER MAPPING FOR Department_HR SERVER
 server_Department_CR_remote OPTIONS (user 'target',password 't');

SQL query to compare two tables in different databases in Oracle

  •  Provide select access to the source database on the remote server.
GRANT USAGE ON FOREIGN SERVER server_Department_CR_remote TO source;

SQL query to compare two tables in different databases in Oracle

  • dblink_connect function to establish the connection from the source to the target DB.
SELECT dblink_connect('myconn', 
'dbname=department_cr port=1521 
host=localhost user=target password=t');
 

Step 8: Query to compare the indexes.

# Login to source database
select distinct i1.index_name from user_ind_columns i1
where table_name='Department_HR' and not exists 
(select * from user_ind_columns i2 where 
i2.table_name = 'Department_CR' and i1.column_name = i2.column_name
and i1.column_position = i2.column_position);

Output:

Index_name
----------
indx_dept_emp_id 
indx_dept_manager_id 
 

How to compare two tables from different database in Oracle?

select count(*) from ( ( select * from t minus select * from t@db2 ) union all ( select * from t@db2 minus select * from t) ) <code> So thats very easy to turn into a PLSQL block <code> SQL> set serverout on SQL> declare 2 template varchar2(1000) := 3 'select count(*) 4 from 5 ( 6 ( select * from ##TABLE## 7 minus 8 ...

How to compare two tables in SQL in different databases?

Comparing Database Data.
On the SQL menu, point to Data Compare, and then click New Data Comparison. ... .
Identify the source and target databases. ... .
Select the check boxes for the tables and views that you want to compare..

How to compare two tables in different schemas in Oracle?

Firstly, For this i have created function which will fetch common columns in both tables. create or replace procedure comparetable( schema1 in varchar2,table1 in varchar2,schema2 in varchar2,table2 in varchar2, p_array_size in number default 100 ) as l_cursor sys_refcursor; l_owner dbms_sql.

How to compare two columns in two different tables in Oracle?

Compare columns in two tables and list out column names which are different. for ex:- create table t1(c1 number(2), c2 varchar2(10)); create table t2(c1 number(2), c2 varchar2(10)); insert into t1 values(1,'a'); insert into t2 values(1,'b'); result should be column c2 is different.