Oracle 重复数据查询以及删除

Create table test
(id number(2),
names varchar2(20));
insert into test values(1,'张三');
insert into test values(2,'李四');
insert into test values(3,'马七');
select * from test;
--查找重复数据
select * from test a where (a.id,a.names) in
(select id,names from test group by id,names having count(*) > 1)

--删除重复数据,只留rowid最小的那行;
delete from test a where (a.id,a.names) in
(select id,names having count(*) > 1)
and rowid not in (select min(rowid) from test group by id,names having count(*)>1)
--查找重复数据,不含rowid最小的行
select * from test a where (a.id,a.names) in
(select id,names having count(*) > 1)
and rowid not in (select min(rowid) from test group by id,names having count(*)>1)


---1.以上是重复数据根据多列来判断


以下是重复数据根据单列来判断

1、首先,查找表中多余的重复记录,重复记录是根据单个字段(id)来判断

select * from test where id in(select id from test group by having count(id) >1)

2、删除表中多余的重复记录,重复记录是根据单个字段(id)来判断,只留有rowid最小的记录

delete from test where (id) in (select id from test group by id having count(id) >1) and rowid not in (select min(rowid) from test group by id having count(*)>1)

相关文章

数据库版本:11.2.0.4 RAC(1)问题现象从EM里面可以看到,在23号早上8:45~8:55时,数据库等待会话暴增...
(一)问题背景最近在对一个大约200万行数据的表查看执行计划时,发现存在异常,理论上应该返回100多万...
(一)删除备份--DELETE命令用于删除RMAN备份记录及相应的物理文件。当使用RMAN执行备份操作时,会在RM...
(1)DRA介绍 数据恢复顾问(Data Recovery Advise)是一个诊断和修复数据库的工具,DRA能够修复数据文...
RMAN(Recovery Manager)是Oracle恢复管理器的简称,是集数据库备份(backup)、修复(restore)和恢复...
(1)备份对象 可以使用RMAN进行的备份对象如下: --整个数据库:备份所有的数据文件和控制文件; --数...