简体   繁体   中英

Oracle 11g: table checksum or hash

I would like do a checksum against different views implementing different approach supposed to produce the same result (assuming each view contains no duplicates and have the exact same columns).

Questions

  1. What is the easiest way to compute a table checksum or hash ?
  2. Is a checksum the only way to compare in Oracle if 2 tables are identical ?

Note: Googling around I found some old answers such as this one .

Is a checksum the only way to compare in Oracle if 2 tables are identical ?

No, since you have no duplicates, you can use the MINUS operator:

SELECT * FROM Table1
MINUS
SELECT * FROM Table2

Will return all the rows from Table1 that do not exist in Table2 . If zero rows are returned then all the rows in Table1 also exist in Table2 .

If you want to check the tables in both directions then:

SELECT *
FROM   (
  SELECT t1.*, 'T1' AS "WHERE" FROM Table1 t1
  MINUS
  SELECT t2.*, 'T1' FROM Table2 t2
)
UNION ALL
(
  SELECT t2.*, 'T2' FROM Table2 t2
  MINUS
  SELECT t1.*, 'T2' FROM Table1 t1
)

Again, if no rows are returned then the tables are identical.

If you are comparing tables with duplicates and want the same number of duplicate rows in each table then you can use something like:

SELECT Col1, Col2, Col3, /*...*/ ColN,
       ROW_NUMBER() OVER ( PARTITION BY Col1, Col2, Col3, /*...*/ ColN ORDER BY ROWNUM )
         AS rn
FROM   table1
MINUS
SELECT Col1, Col2, Col3, /*...*/ ColN,
       ROW_NUMBER() OVER ( PARTITION BY Col1, Col2, Col3, /*...*/ ColN ORDER BY ROWNUM )
         AS rn
FROM   table2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM