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
相关推荐
l1t18 分钟前
DeepSeek总结的DuckLake 入门
数据库
Joseph Cooper27 分钟前
RAG 与 AI Agent:智能体真的需要检索增强生成吗?
数据库·人工智能·ai·agent·rag·上下文工程
light blue bird30 分钟前
主子端台二分法任务汇总组件
前端·数据库·.net·桌面端winform
DevilSeagull1 小时前
MySQL(2) 客户端工具和建库
开发语言·数据库·后端·mysql·服务
小李来了!1 小时前
Navicate/plsql连接Oracle数据库教程
数据库·oracle
苍煜1 小时前
慢SQL优化实战教学
java·数据库·sql
zhaoyong2222 小时前
MySQL 存储过程中字符集与排序规则不匹配导致查询性能下降的解决方案
jvm·数据库·python
sinat_383437362 小时前
golang如何从Python转型Go开发_golang从Python转型Go开发攻略
jvm·数据库·python
远洪2 小时前
claude code 国内安装使用
数据库·mysql
雨辰AI2 小时前
SpringBoot3 + 人大金仓 V9 微服务监控实战|Prometheus+Grafana+SkyWalking 全链路监控
数据库·后端·微服务·grafana·prometheus·skywalking