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
相关推荐
APIshop14 分钟前
微店关键词搜索商品列表 API 实战:micro.item_search 全流程指南
数据库
翔云12345641 分钟前
在MySQL中,使用物理备份工具 xtrabackup备份扩容从库,从库上的gtid_executed和gtid_purged变化过程
数据库·mysql
liulilittle1 小时前
Linux shell 搜索指定后缀名文件,并复制到指定目录。
linux·服务器·数据库
必胜刻1 小时前
Redis哨兵模式(Linux)
linux·数据库·redis
dualven_in_csdn2 小时前
【数据库损坏】关于一次现场数据库损坏
数据库·mysql
锦衣夜行?2 小时前
oracle 未知长度从左到右截取某个字符串
数据库·oracle
han_hanker2 小时前
@JsonIgnore,@JsonProperty, @JsonInclude,@JsonFormat
数据库·oracle
hanyi_qwe3 小时前
MySQL事务基础
数据库·mysql
l1t3 小时前
三种用SQL解决Advent of Code 2022第8题 树顶木屋 的比较和分析
数据库·sql·oracle·duckdb·advent of code
如果未来,3 小时前
Oracle的Redo log和Undo log的区别
数据库·oracle