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
相关推荐
紫金修道2 小时前
【DeepAgent】概述
开发语言·数据库·python
孟章豪3 小时前
《SQL拼接 vs 参数化,为什么公司禁止拼接SQL?(附真实案例)》
服务器·数据库·sql
荒川之神3 小时前
ORACLE LEVEL函数练习
数据库·oracle
·云扬·3 小时前
【MySQL】实战:用pt-table-sync修复主从数据一致性问题
数据库·mysql·ffmpeg
swIn KWAL4 小时前
【MySQL】环境变量配置
数据库·mysql·adb
shark22222224 小时前
【JOIN】关键字在MySql中的详细使用
数据库·mysql
RATi GORI4 小时前
MySQL中的CASE WHEN语句:用法、示例与解析
android·数据库·mysql
坊钰4 小时前
Java 死锁问题及其解决方案
java·开发语言·数据库
onebound_noah4 小时前
【实战教程】如何通过API快速获取淘宝/天猫商品评论数据(含多语言Demo)
大数据·数据库