• Oracle表数据和表结构对比


      在工作中需要完成这么一个需求:比较两个表的表结构是否形相同,并找出差异.比较两个表中的数据是否相同,并找出差异数据?

         分析:由于表结构中字段比较多,手工比较很浪费时间,而且不能保证不出错误.对于表中的数据那就能多了,更不能靠这种方式比较.

         为了思考问题简单和方便测试,首先先建立两个测试表,并插入一些测试数据吧,sql如下:

    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.比较表结构 */  
    (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结构及数据:

    表结构差异:

    数据差异:

  • 相关阅读:
    搭建woocomerce网站
    Cozmo 机器人编程环境搭建
    DevExpress Wizard的控件使用方法
    DevExpress 地图的控件使用方法
    DevExpress 摄像机的控件使用方法
    大疆第一人称视角眼镜goggle激活
    iis支持asp.net4.0的注册命令使用方法
    【转】PowerDesigner删除外键关系,而不删除外键列
    【转】ABP webapi三种方式
    【转】OAuth2.0的refresh token
  • 原文地址:https://www.cnblogs.com/Jace06/p/6919777.html
Copyright © 2020-2023  润新知