oracle删除多余重复数据,oracle 删除重复多余的数据

本文介绍了解决数据库中重复数据的问题,提供了三种不同的SQL删除重复记录的方法:通过ROWID比较、自连接表比较ROWID以及直接两表间ROWID比较。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建测试用的数据

drop table student;

create table student(sno number(10), sname varchar2(10),sage int);

insert into student  values(1,'aa',21);

insert into student  values(1,'aa',21);

insert into student  values(2,'bb',22);

insert into student  values(3,'cc',23);

insert into student  values(3,'cc',23);

insert into student  values(3,'cc',23);

0818b9ca8b590ca3270a3433284dd417.png

目的是是表student 出去重复多余的行。

方法一: delete from student where rowid not in (select min(rowid) from student group by sno );

rowid与分组后的rowid 最小值比较。

方法二:delete from student where rowid in (select a.rowid from student a, student b

where a.sno=b.sno and a.rowid > b.rowid);

先 自连之后比较两个表的rowid  。

方法三: delete from student a where a.rowid >(select min(b.rowid) from student b where a.sno=b.sno);

两表之间直接比较rowid,

0818b9ca8b590ca3270a3433284dd417.png