从Oracle中的表中删除重复行

2022-01-10 00:00:00 sql duplicates oracle delete-row

我正在 Oracle 中测试一些东西并用一些示例数据填充了一个表,但在此过程中我不小心加载了重复记录,所以现在我无法使用某些列创建主键.

I'm testing something in Oracle and populated a table with some sample data, but in the process I accidentally loaded duplicate records, so now I can't create a primary key using some of the columns.

如何删除所有重复的行并只保留其中一个?

How can I delete all duplicate rows and leave only one of them?

推荐答案

使用 rowid 伪列.

DELETE FROM your_table
WHERE rowid not in
(SELECT MIN(rowid)
FROM your_table
GROUP BY column1, column2, column3);

其中 column1column2column3 构成每条记录的标识键.您可以列出所有列.

Where column1, column2, and column3 make up the identifying key for each record. You might list all your columns.

相关文章