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

create table a(
       bm char(4),
       mc varchar2(200)
);

--表已建立:

insert into a values('1111','1111');
insert into a values('1112','1112');
insert into a values('1113','1113');
insert into a values('1114','1114');

 

create table b as select * from a where 1=2  --复制表结构

 

insert into b values('1111','1111');
insert into b values('1112','1112');
insert into b values('1113','1113');
insert into b values('1114','1114');

 

方法一

exists子句:

 

   delete from a where exists (select 'X' from b where a.bm=b.bm and a.mc=b.mc);

删除4个记录。

 

where条件:如果两个表中都拥有相同字段的主键(primary key),则只需比较两个主键就可以了。

方法二

in子句:

 delete from a where (bm,mc) in (select bm,mc from b);
 

实际测试结论

在表不是很大时,用in子句速度还可以忍受,而如果记录量很多时(十万条以上),in子句速度很慢。

你可能感兴趣的:(数据库oracle)