oracle diff: how to compare two tables?

后端 未结 12 1948
忘了有多久
忘了有多久 2021-01-31 03:22

Suppose I have two tables, t1 and t2 which are identical in layout but which may contain different data.

What\'s the best way to diff these two tables?

12条回答
  •  不思量自难忘°
    2021-01-31 04:07

    In addition to some of the other answers provided, if you wanted to look at the differences in table structure with a table that might have the similar but differing structure, you could do this in multiple ways:

    First - If using Oracle SQL Developer, you could run a describe on both tables to compare them:

    descr TABLE_NAME1
    descr TABLE_NAME2
    

    Second - The first solution may not be ideal for larger tables with a lot of columns. If you only want to see the differences in the data between the two tables, then as mentioned by several others, using the SQL Minus operator should do the job.

    Third - If you are using Oracle SQL Developer, and you want to compare the table structure of two tables using different schemas you can do the following:

    1. Select "Tools"
    2. Select "Database Diff"
    3. Select "Source Connection"
    4. Select "Destination Connection"
    5. Select the "Standard Object Types" you want to compare
    6. Enter the "Table Name"
    7. Click "Next" until you reach "Finish"
    8. Click "Finish"
    9. NOTE: In steps 3 & 4 is where you would select the differing schemas in which the objects exist that you want to compare.

    Fourth - If the tables two tables you wish to compare have more columns, are in the same schema, have no need to compare more than two tables and are unappealing to compare visually using the DESCR command you can use the following to compare the differences in the table structure:

    select 
         a.column_name    || ' | ' || b.column_name, 
         a.data_type      || ' | ' || b.data_type, 
         a.data_length    || ' | ' || b.data_length, 
         a.data_scale     || ' | ' || b.data_scale, 
         a.data_precision || ' | ' || b.data_precision
    from 
         user_tab_columns a,
         user_tab_columns b
    where 
         a.table_name = 'TABLE_NAME1' 
    and  b.table_name = 'TABLE_NAME2'
    and  ( 
           a.data_type      <> b.data_type     or 
           a.data_length    <> b.data_length   or 
           a.data_scale     <> b.data_scale    or 
           a.data_precision <> b.data_precision
         )
    and a.column_name = b.column_name;
    

提交回复
热议问题