oracle查询出表中某几个字段值不唯一的数据

假设有一张学生表,建表语句如下:

复制代码
--学生 student表
create table student(
sno varchar2(10) primary key,
sname varchar2(20),
sage number(2),
ssex varchar2(5),
birthday date
);

现计划创建唯一索引(sno,sname),由于数据录入失误导致这两列数据有重复数据,进而导致唯一索引创建失败。

因此我们需要先查询出表中这些捣乱的数据,并进行去重处理。

可使用如下语句查询重复数据:

复制代码
select T.*, T.ROWID
  from student  T
 where exists (select 1
          from student
         where sno = T.sno
           and sname = T.sname
           and ROWID != T.ROWID)

补充:

如果只是查询某一列的重复值(如:sno),语句更加简单,可以如下:

复制代码
select    t. *    from  student t , student tt  where  t.rowid != tt.rowid  and  t.sno = tt.sno
相关推荐
i220818 Faiz Ul4 小时前
计算机毕业设计|基于springboot + vue鲜花商城系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
Apple_羊先森6 小时前
ORACLE数据库巡检SQL脚本--22、检查碎片程度最高的业务表
数据库·sql·oracle
OnYoung7 小时前
更优雅的测试:Pytest框架入门
jvm·数据库·python
山岚的运维笔记8 小时前
SQL Server笔记 -- 第85章:查询提示
数据库·笔记·sql·microsoft·sqlserver
chilavert3188 小时前
技术演进中的开发沉思-371:final 关键字(中)
java·前端·数据库
tryCbest8 小时前
SQL Server数据库
数据库·sql server
_codemonster9 小时前
PreparedStatement 和 Statement的区别
数据库·oracle
恒云客9 小时前
python uv debug launch.json
数据库·python·json