oracle中比較兩表表結構差異和數據差異的方法


  在工作中需要完成這么一個需求:比較兩個表的表結構是否形相同,並找出差異.比較兩個表中的數據是否相同,並找出差異數據?
    分析:由於表結構中字段比較多,手工比較很浪費時間,而且不能保證不出錯誤.對於表中的數據那就能多了,更不能靠這種方式比較.

    為了思考問題簡單和方便測試,首先先建立兩個測試表,並插入一些測試數據吧,sql如下:

 

  1. create table t_A 
  2.   id   VARCHAR2(36) not null, 
  3.   name VARCHAR2(100), 
  4.   age  NUMBER, 
  5.   sex  VARCHAR2(2) 
  6. ); 
  7.  
  8. insert into t_A (id, name, age, sex) 
  9. values ('1', '1', 1, '1'); 
  10. insert into t_A (id, name, age, sex) 
  11. values ('2', '2', 2, '2'); 
  12. commit; 
  13.  
  14.  
  15. create table t_B 
  16.   id    VARCHAR2(36) not null, 
  17.   name  VARCHAR2(100), 
  18.   age   NUMBER, 
  19.   clazz VARCHAR2(36) 
  20. ); 
  21.  
  22. insert into t_B (id, name, age, clazz) 
  23. values ('1', '1', 1, '1'); 
  24. insert into t_B (id, name, age, clazz) 
  25. values ('2', '2', 1, '3'); 
  26. insert into t_B (id, name, age, clazz) 
  27. values ('3', '3', 3, '3'); 
  28. commit; 
create table t_A
(
  id   VARCHAR2(36) not null,
  name VARCHAR2(100),
  age  NUMBER,
  sex  VARCHAR2(2)
);

insert into t_A (id, name, age, sex)
values ('1', '1', 1, '1');
insert into t_A (id, name, age, sex)
values ('2', '2', 2, '2');
commit;


create table t_B
(
  id    VARCHAR2(36) not null,
  name  VARCHAR2(100),
  age   NUMBER,
  clazz VARCHAR2(36)
);

insert into t_B (id, name, age, clazz)
values ('1', '1', 1, '1');
insert into t_B (id, name, age, clazz)
values ('2', '2', 1, '3');
insert into t_B (id, name, age, clazz)
values ('3', '3', 3, '3');
commit;

      解決過程:剛開始考慮使用存儲過程,用循環比較的方式處理,首先需要找出能得到表結構的sql,查閱資料得知,在Oracle中所有表結構信息都存儲在user_tab_columns中,那么查詢單個表的表結構信息很簡單: select column_name from user_tab_columns where table_name = 't_A'; 運行后發現查不到結果,為什么呢?去掉查詢條件后能查詢出結果,核對后發現原來在user_tab_columns中存儲的內容都是大寫的,原來如此,sql改為如下就可以查詢出結果了: select column_name from user_tab_columns where table_name = 'T_A'; 寫這樣一個存儲過程發現還是有點復雜的,網上找找有沒有現成的,自己寫了一會發現很復雜.網上找的時候找到了一個minus關鍵字.科普一下:在oracle中union 並集 intersect 交集  minus 差集;我可以用差集來實現那個需求嗎? 很快就寫出了sql:

 

 

  1. /*1.比較表結構 */ 
  2. (select column_name 
  3.           from user_tab_columns 
  4.          where table_name = 'T_A' 
  5.         minus 
  6.         select column_name 
  7.           from user_tab_columns 
  8.          where table_name = 'T_B') 
  9. union  
  10. (select column_name 
  11.          from user_tab_columns 
  12.         where table_name = 'T_B' 
  13.        minus 
  14.        select column_name 
  15.          from user_tab_columns 
  16.         where table_name = 'T_A'); 
  17.          
  18.  
  19. /* 2.比較表數據 */ 
  20. (select * 
  21.           from t_A 
  22.         minus 
  23.         select * from t_B) 
  24. union  
  25. (select * 
  26.          from t_B 
  27.        minus 
  28.        select * from t_A) 
/*1.比較表結構 */
(select column_name
          from user_tab_columns
         where table_name = 'T_A'
        minus
        select column_name
          from user_tab_columns
         where table_name = 'T_B')
union 
(select column_name
         from user_tab_columns
        where table_name = 'T_B'
       minus
       select column_name
         from user_tab_columns
        where table_name = 'T_A');
        

/* 2.比較表數據 */
(select *
          from t_A
        minus
        select * from t_B)
union 
(select *
         from t_B
       minus
       select * from t_A)

 

看看sql的運行效果吧:

表t_A結構及數據:

表t_B結構及數據:

表結構差異:

數據差異:

 

    反思:為什么我之前沒想到用差集呢? 1.數學沒有學好,沒有數學的思維.並集交集和差集的概念早就在中學學過,但數學思維沒有建立,所以....得補補數學啦~ 2.oracle函數不熟,看來我需要找一本oracle函數手冊,沒事的時候就翻翻.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM