[英]Oracle Compare data between two different table
I have two table one is having all field VARCHAR2 but other having different type for different data. 我有两个表,一个是所有字段VARCHAR2,另一个是不同数据的类型。
For Example : 例如 :
Table One
==========================
Col 1 VARCHAR2 UNIQUE KEY
Col 2 VARCHAR2
Col 3 VARCHAR2
===========================
Table Two
==========================
Col One VARCHAR2 UNIQUE KEY
Col Two TIMESTAMP
Col Three NUMBER
==========================
we are having one mapping table. 我们有一个映射表。 it denotes which column of Table One has to compare with which column of Table Two.
它表示表一的哪一列必须与表二的哪一列进行比较。 For Example
例如
Mapping Table
==============================
Table One Table Two
==============================
Col 1 Col One
Col 2 Col Three
Col 3 Col Two
==============================
Now with the help of UNIQUE KEY of TABLE ONE we have to find same row in TABLE TWO and compare rows column by column and get changes in data. 现在,借助于表ONE的UNIQUE KEY,我们必须在表2中找到同一行,并逐列比较行并获取数据更改。 Currently we are using java program for comparing data row by row and column by column and getting changes between data in rows with same UNIQUE KEY.
当前,我们正在使用Java程序来逐行比较数据和逐列比较数据,并使用相同的UNIQUE KEY获取行中数据之间的更改。 it is working fine but taking too much time as we are having 100000 records in DB.
它工作正常,但是由于我们在数据库中有100000条记录,所以花费了太多时间。
Now my question is : is there any way i can compare data at SQL level and get changes in data? 现在我的问题是:有什么方法可以在SQL级别比较数据并获取数据更改?
You can do it 'manually' with a query like this: It's a lot of work, but there are only three different types of checks you need to do, so it's not very complex: 您可以使用以下查询来“手动”执行此操作:这项工作很繁琐,但是只需要执行三种不同类型的检查,因此它并不十分复杂:
select
*
from
Table1 t1
full outer join Table2 t2 on t2.ID = t1.ID
where
-- Check ID, either record does not exist in either table.
t1.ID is null or
t2.ID = null or
-- Not nullable field can be easily compared.
t1.NotNullableField1 <> t2.NotNUllableField1 or
-- Nullable field is slightly more work.
t1.NullableField1 <> t2.NullableField1 or
(t1.NullableField1 is null and t2.NullableField1 is not null) or
(t1.NullableField1 is not null and t2.NullableField1 is null)
Another solution is to use MINUS, which is a bit like UNION, only it returns a dataset minus the records in a second dataset: 另一个解决方案是使用MINUS,这有点像UNION,只是它返回一个数据集减去第二个数据集中的记录:
select * from Table1 t1
MINUS
select * from Table2 t2
This works only one way (which might be fine for your purpose), but you can also combine it with UNION to make it bidirectional. 这仅是一种方法(可能对您有用),但是您也可以将其与UNION组合以使其双向。
select
*
from
( select * from Table1
MINUS
select * from Table2)
UNION ALL
( select * from Table2
MINUS
select * from Table1)
The output of both solutions is a bit different. 两种解决方案的输出都有些不同。
In the FULL OUTER JOIN query, the IDs will be joined and the values of the matching rows will be displayed next to each other as a single row. 在FULL OUTER JOIN查询中,将合并ID,并且匹配行的值将作为一行单独显示。
In the MINUS query, the result will be presented as a single dataset. 在MINUS查询中,结果将显示为单个数据集。 If a record does not exist in either one table, it will be displayed.
如果任一表中都不存在记录,则将显示该记录。 If a record (ID) exists in both tables, but other fields are different, you will get both rows.
如果两个表中都存在记录(ID),但其他字段不同,则将获得两行。 So it's a bit harder to compare them.
所以比较它们有点困难。
See: http://www.techonthenet.com/oracle/minus.php 请参阅: http : //www.techonthenet.com/oracle/minus.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.