删除a表中和b表相同的数据

来源:互联网 发布:js验证身份证 编辑:程序博客网 时间:2024/05/19 20:42
SQL> create table a (
  2 bm char(4), --编码
  3 mc varchar2(20) --名称
  4 )
  5 /
  
  表已建立.
  
  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;
  
  表已建立.
  
  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;
  
  完全提交.
  
  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 1111
  
  方法一:exists子句
  SQL> delete from a where exists (select 'X' from b where a.bm=b.bm and a.mc=b.mc);
  
  删除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 1111
原创粉丝点击