Oracle数据库删除两表中相同数据的方法

来源:互联网 发布:mac怎么装linux虚拟机 编辑:程序博客网 时间:2024/05/09 15:49
导读:
  在Oracle数据库中删除两表中相同数据的方法是用到in语句,本文通过实例来讲解删除两表中相同数据的方法。
  软件环境:
  1、Windows NT4.0+ORACLE 8.0.4。
  2、Oracle安装路径为:C:/ORANT。
  问题提出:
  1、在做数据转储业务的时候,如果发生操作错误,有可能出现主表和副表中都有同一种数据,
  这样结算的结果就有可能发生错误。
  实现方法:
  SQL> create table a (
  2 bm char(4), --编码
  3 mc varchar2(20) --名称
  4 )
  5 /bitsCN~com
  表已建立:
  SQL> insert into a values('1111','1111');
  SQL> insert into a values('1112','1111');
  SQL> insert into a values('1113','1111');
  SQL> insert into a values('1114','1111');
  SQL> insert into a values('1115','1111');
  SQL> create table b as select * from a where 1=2;
  [bitsCN.Com]
  表已建立:
  SQL> insert into b values('1111','1111');
  SQL> insert into b values('1112','1111');
  SQL> insert into b values('1113','1111');
  SQL> insert into b values('1114','1111');
  SQL> commit;[bitsCN_com]
  完全提交:
  SQL> select * from a;
  BM  MC
  ---- --------------------
  1111 1111
  1112 1111
  1113 1111
  1114 1111
  1115 1111
  SQL> select * from b;
  BM  MC
  ---- --------------------
  1111 1111
  1112 1111
  1113 1111
  1114 1111bbs.bitsCN.com
  方法一
  exists子句:
  SQL> delete from a where exists (select 'X' from b where a.bm=b.bm and a.mc=b.mc);www@bitscn@com
  删除4个记录。
  where条件:如果两个表中都拥有相同字段的主键(primary key),则只需比较两个主键就可以了。
  方法二
  in子句:
  SQL> delete from a where (bm,mc) in (select bm,mc from b);
  删除4个记录.
  SQL> select * from a;
  BM  MC
  ---- --------------------
  1115 1111bbs.bitsCN.com中国网管论坛
  实际测试结论
  在表不是很大时,用in子句速度还可以忍受,而如果记录量很多时(十万条以上),in子句速度很慢。

本文转自
http://www.bitscn.com/oracle/optimize/200709/109317.html
原创粉丝点击