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
相关推荐
suoyue_zhan4 分钟前
GBase的管理监控平台GEM实践指南
数据库
哈哈老师啊1 小时前
Springboot学生综合测评系统hxtne(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·数据库·spring boot
小小8程序员1 小时前
Redis-10
数据库·redis·缓存
liuzhilongDBA1 小时前
从collation mismatch异常到其原理
数据库·version·glibc·postgres·collation
梁萌1 小时前
MySQL数据库分库分表介绍
数据库·mysql·shardingsphere·分库分表
占疏3 小时前
dify API访问工作流/聊天
开发语言·数据库·python
Cat God 0073 小时前
SQL使用及注意事项
数据库·sql·mysql
@老蝴4 小时前
MySQL数据库 - 约束和联合查询
android·数据库·mysql
程序猿20234 小时前
MySQL索引使用--最左前缀法则
数据库·mysql
老华带你飞4 小时前
列车售票|基于springboot 列车售票系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring