日期:2014-05-16  浏览次数:20451 次

oracle 删除有大量重复数据且数据量很大的表且该表存在主键被引用的情况下清除重复数据
1 先将需要保留的数据保存下来放入临时表 USERCOMPANYINFO_tmp

  
create table USERCOMPANYINFO_tmp as (
 select * from USERCOMPANYINFO where rowid not in (SELECT A.ROWID FROM USERCOMPANYINFO A,(SELECT MAX(A.ROWID) RROWID,COMPANY FROM USERCOMPANYINFO A GROUP BY A.COMPANY HAVING COUNT(*) > 3) B 
     WHERE A.COMPANY = B.COMPANY AND A.ROWID <> B.RROWID))


2 当表中的主键被其他表参照时,需要执行下面语句 不然会报错:
 
 ORA-02266: unique/primary keys in table referenced by enabled foreign keys
   ORA-02266: 表中的唯一/主键被启用的外部关键字引用


alter table USERCOMPANYINFO disable primary key cascade; //清除外部引用

 3 
 truncate table USERCOMPANYINFO;//清空数据
 
 4 
 insert into USERCOMPANYINFO select * from USERCOMPANYINFO_tmp; //蒋临时表中的数据倒入目标数据表
 5
 alter table USERCOMPANYINFO enable primary key; //给目标数据表添加主键约束