zoukankan      html  css  js  c++  java
  • [oracle/Sql]怎样比较两表的差异?

    比如有这么一个表:

    create table test02(
       id number(8,0) primary key,
       name nvarchar2(20),
       sal number(5,0)
    )

    可以这样给它充值:

    insert into test02 
    select rownum,dbms_random.string('*',dbms_random.value(6,20)),dbms_random.value(1000,30000)  from dual
    connect by level<=200
    order by dbms_random.random

    然后以此建立一张新表:

    create table test03 as select * from test02;

    现在 test02,test03内容都是一致的。

    但在漫长的生活中,风化和侵袭会导致两者有所不同,下面用更新语句来模拟两者产生的变化:

    update test02 set name='bill' where id=1;
    update test02 set name='andy' where id=2;
    
    update test03 set sal=22222 where id=22;
    update test03 set sal=33333 where id=33;
    update test03 set sal=44444 where id=44;

    这样,两表就产生了五条数据的不同,那么怎么通过sql语句知道不同点呢?可以执行下面SQL:

    select t2.*,t3.* from test02 t2 full join test03 t3 on (t2.name=t3.name and t2.sal=t3.sal)
    where (t2.id IS NULL or t3.id IS NULL) order by t2.id,t3.id

    执行效果:

    SQL> select t2.*,t3.* from test02 t2 full join test03 t3 on (t2.name=t3.name and t2.sal=t3.sal)
      2  where (t2.id IS NULL or t3.id IS NULL) order by t2.id,t3.id;
    
            ID NAME                                            SAL         ID NAME                                    SAL
    ---------- ---------------------------------------- ---------- ---------- ---------------------------------------- ----------
             1 bill                                          11595
             2 andy                                          23646
            22 CTCUCDUAOFEDWDOSZJ                            24070
            33 SRMKTDAHKCSKMU                                 5758
            44 ZCFLYSMOSHYWNX                                22652
                                                                            1 ZUNZKJEEDGTHC                         11595
                                                                            2 SGRNZOIFBMITAMKSYTQE                  23646
                                                                           22 CTCUCDUAOFEDWDOSZJ                    22222
                                                                           33 SRMKTDAHKCSKMU                        33333
                                                                           44 ZCFLYSMOSHYWNX                        44444
    
    已选择10行。

    如果是两表没有差别呢?让我们以test02为基础创建一个test04来模拟:

    create table test04 as select * from test02;

    再比较一下:

    SQL> select t2.*,t4.* from test02 t2 full join test04 t4 on (t2.name=t4.name and t2.sal=t4.sal)
      2  where (t2.id IS NULL or t4.id IS NULL) order by t2.id,t4.id;
    
    未选定行

    这便是两表完全一致的情况。

    以上的方法也适用于View和查询结果的比较。

    --2020年1月29日--

  • 相关阅读:
    数仓1.3 |行为数据| 业务数据需求
    数仓1.1 |概述| 集群环境搭建
    麒麟Kylin
    ng--todolist
    mysql必知必会--用正则表达式 进行搜索
    mysql必知必会--用通配符进行过滤
    mysql必知必会--数 据 过 滤
    mysql必知必会--过 滤 数 据
    mysql必知必会--排序检索数据
    mysql必知必会--检 索 数 据
  • 原文地址:https://www.cnblogs.com/heyang78/p/12239597.html
Copyright © 2011-2022 走看看