`

oracle 删除表重复的记录

阅读更多
引用地址:
1 http://dev.csdn.net/author/Rayfly/b850da865b3b4cc89008a2004dad56fc.html
2 http://maping.iteye.com/blog/309295
3 http://zhangyou1010.iteye.com/blog/512555

1 针对表中所有字段相同的处理方式:
  通过创建临时表的方式
  缺点:对于一个千万级记录的表,会给系统带来很大的开销,不可行。
  原理:通过创建临时表,把数据先导入到一个临时表中,然后删除原表的数据,再把临时表的数据导回原表,SQL语句如下:
creat table tbl_tmp (select distinct* from tbl);
truncate table tbl;//清空表记录
insert into tbl select * from tbl_tmp;//将临时表中的数据插回来.

2 通过rowid属性
  缺点:表的每条记录只有一条重复的,这个sql语句适用,表的每个字段都要写上.麻烦呀
原理:在oracle中,每一条记录都有一个rowid,rowid在整个数据库中是唯一的,rowid确定了每条记录是 oracle中的哪一个数据文件、块、行上。在重复的记录中,可能所有列的内容都相同,但rowid不会相同。SQL语句如下:
delete from tbl where rowid in (select a.rowid from tbl a, tbl b where a.rowid>b.rowid and a.col1=b.col1 and a.col2 = b.col2)

C 综合删除
delete from tablesA a
where (a.id) in   (select id from tablesA group by id having count(*) > 1)
and rowid not in (select min(rowid) from tablesA group by id having count(*)>1)

3 查询重复记录
select * from tablename where id in (select id from tablename group by id
having count(id) > 1) 




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics