在Oracle中如何利用Rowid查找和刪除表中的重復記錄
2024-08-29 13:29:24
供稿:網友
平時工作中可能會遇到當試圖對庫表中的某一列或幾列創建唯一索引時,系統提示 ora-01452 :不能創建唯一索引,發現重復記錄。
下面總結一下幾種查找和刪除重復記錄的方法(以表cz為例):
表cz的結構如下:
sql> desc cz
name null? type
----------------------------------------- -------- ------------------
c1 number(10)
c10 number(5)
c20 varchar2(3)
刪除重復記錄的方法原理:
(1).在oracle中,每一條記錄都有一個rowid,rowid在整個數據庫中是唯一的,rowid確定了每條記錄是在oracle中的哪一個數據文件、塊、行上。
(2).在重復的記錄中,可能所有列的內容都相同,但rowid不會相同,所以只要確定出重復記錄中那些具有最大rowid的就可以了,其余全部刪除。
重復記錄判斷的標準是:
c1,c10和c20這三列的值都相同才算是重復記錄。
經查看表cz總共有16條記錄:
sql>set pagesize 100
sql>select * from cz;
c1 c10 c20
---------- ---------- ---
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
2 3 che
2 3 che
2 3 che
3 4 dff
3 4 dff
3 4 dff
4 5 err
5 3 dar
6 1 wee
7 2 zxc
20 rows selected.
1.查找重復記錄的幾種方法:
(1).sql>select * from cz group by c1,c10,c20 having count(*) >1;
c1 c10 c20
---------- ---------- ---
1 2 dsf
2 3 che
3 4 dff
(2).sql>select distinct * from cz;
c1 c10 c20
---------- ---------- ---
1 2 dsf
2 3 che
3 4 dff
(3).sql>select * from cz a where rowid=(select max(rowid) from cz where c1=a.c1 and c10=a.c10 and c20=a.c20);
c1 c10 c20
---------- ---------- ---
1 2 dsf
2 3 che
3 4 dff
2.刪除重復記錄的幾種方法:
(1).適用于有大量重復記錄的情況(在c1,c10和c20列上建有索引的時候,用以下語句效率會很高):
sql>delete cz where (c1,c10,c20) in (select c1,c10,c20 from cz group by c1,c10,c20 having count(*)>1) and rowid not in
(select min(rowid) from cz group by c1,c10,c20 having count(*)>1);
sql>delete cz where rowid not in(select min(rowid) from cz group by c1,c10,c20);
(2).適用于有少量重復記錄的情況(注意,對于有大量重復記錄的情況,用以下語句效率會很低):
sql>delete from cz a where a.rowid!=(select max(rowid) from cz b where a.c1=b.c1 and a.c10=b.c10 and a.c20=b.c20);
sql>delete from cz a where a.rowid<(select max(rowid) from cz b where a.c1=b.c1 and a.c10=b.c10 and a.c20=b.c20);
sql>delete from cz a where rowid <(select max(rowid) from cz where c1=a.c1 and c10=a.c10 and c20=a.c20);
(3).適用于有少量重復記錄的情況(臨時表法):
sql>create table test as select distinct * from cz; (建一個臨時表test用來存放重復的記錄)
sql>truncate table cz; (清空cz表的數據,但保留cz表的結構)
sql>insert into cz select * from test; (再將臨時表test里的內容反插回來)
(4).適用于有大量重復記錄的情況(exception into 子句法):
采用alter table 命令中的 exception into 子句也可以確定出庫表中重復的記錄。這種方法稍微麻煩一些,為了使用“excepeion into ”子句,必須首先創建 exceptions 表。創建該表的 sql 腳本文件為 utlexcpt.sql 。對于win2000系統和 unix 系統, oracle 存放該文件的位置稍有不同,在win2000系統下,該腳本文件存放在$oracle_home/ora90/rdbms/admin 目錄下;而對于 unix 系統,該腳本文件存放在$oracle_home/rdbms/admin 目錄下。
具體步驟如下:
sql>@?/rdbms/admin/utlexcpt.sql
table created.
sql>desc exceptions
name null? type
----------------------------------------- -------- --------------
row_id rowid
owner varchar2(30)
table_name varchar2(30)
constraint varchar2(30)
sql>alter table cz add constraint cz_unique unique(c1,c10,c20) exceptions into exceptions;
*
error at line 1:
ora-02299: cannot validate (test.cz_unique) - duplicate keys found
sql>create table dups as select * from cz where rowid in (select row_id from exceptions);
table created.
sql>select * from dups;
c1 c10 c20
---------- ---------- ---
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
1 2 dsf
1 2 dsf
1 2 dsf
1 2 dsf
2 3 che
2 3 che
2 3 che
2 3 che
3 4 dff
3 4 dff
3 4 dff
16 rows selected.
sql>select row_id from exceptions;
row_id
------------------
aaahd/aaiaaaadsaaa
aaahd/aaiaaaadsaab
aaahd/aaiaaaadsaac
aaahd/aaiaaaadsaaf
aaahd/aaiaaaadsaah
aaahd/aaiaaaadsaai
aaahd/aaiaaaadsaag
aaahd/aaiaaaadsaad
aaahd/aaiaaaadsaae
aaahd/aaiaaaadsaaj
aaahd/aaiaaaadsaak
aaahd/aaiaaaadsaal
aaahd/aaiaaaadsaam
aaahd/aaiaaaadsaan
aaahd/aaiaaaadsaao
aaahd/aaiaaaadsaap
16 rows selected.
sql>delete from cz where rowid in ( select row_id from exceptions);
16 rows deleted.
sql>insert into cz select distinct * from dups;
3 rows created.
sql>select *from cz;
c1 c10 c20
---------- ---------- ---
1 2 dsf
2 3 che
3 4 dff
4 5 err
5 3 dar
6 1 wee
7 2 zxc
7 rows selected.
從結果里可以看到重復記錄已經刪除。