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
相关推荐
醇醛酸醚酮酯17 分钟前
Qt项目锻炼——TODO清单(二)
开发语言·数据库·qt
GreatSQL社区1 小时前
用systemd管理GreatSQL服务详解
数据库·mysql·greatsql
掘根1 小时前
【MySQL进阶】错误日志,二进制日志,mysql系统库
数据库·mysql
weixin_438335401 小时前
基础知识:mysql-connector-j依赖
数据库·mysql
小明铭同学1 小时前
MySQL 八股文【持续更新ing】
数据库·mysql
Mr_Xuhhh1 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
Fireworkitte1 小时前
Redis 源码 tar 包安装 Redis 哨兵模式(Sentinel)
数据库·redis·sentinel
qq_339282232 小时前
postgressql 如何修改模式的所有表的所有者
数据库
西岭千秋雪_3 小时前
Redis性能优化
数据库·redis·笔记·学习·缓存·性能优化