




已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
ORACLE 中刪除重復(fù)記錄中刪除重復(fù)記錄 平時(shí)工作中可能會(huì)遇見(jiàn)當(dāng)試圖對(duì)庫(kù)表中的某一列或幾列創(chuàng)建唯一索引時(shí) 系統(tǒng)提示 ora 01452 不能創(chuàng)建唯一索引 發(fā)現(xiàn)重復(fù)記錄 下面總結(jié)一下幾種查找和刪除重復(fù)記錄的方法 以表 cz 為例 表 cz 的結(jié)構(gòu)如下 sql desc cz name null type c1 number 10 c10 number 5 c20 varchar2 3 刪除重復(fù)記錄的方法原理 1 在 oracle 中 每一條記錄都有一個(gè) rowid rowid 在整個(gè)數(shù)據(jù)庫(kù)中是唯一的 rowid 確定了 每條記錄是在 oracle 中的哪一個(gè)數(shù)據(jù)文件 塊 行上 2 在重復(fù)的記錄中 可能所有列的內(nèi)容都相同 但 rowid 不會(huì)相同 所以只要確定出重復(fù)記錄 中那些具有最大 rowid 的就能了 其余全部刪除 重復(fù)記錄判斷的標(biāo)準(zhǔn)是 c1 c10 和 c20 這三列的值都相同才算是重復(fù)記錄 經(jīng)查看表 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 查找重復(fù)記錄的幾種方法 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 刪除重復(fù)記錄的幾種方法 1 適用于有大量重復(fù)記錄的情況 在 c1 c10 和 c20 列上建有索引的時(shí)候 用以下語(yǔ)句效率會(huì)非 常高 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 適用于有少量重復(fù)記錄的情況 注意 對(duì)于有大量重復(fù)記錄的情況 用以下語(yǔ)句效率會(huì)非常 低 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 rowiddelete from cz a where rowid create table test as select distinct from cz 建一個(gè)臨時(shí)表 test 用來(lái)存放重復(fù)的記錄 sql truncate table cz 清空 cz 表的數(shù)據(jù) 但保留 cz 表的結(jié)構(gòu) sql insert into cz select from test 再將臨時(shí)表 test 里的內(nèi)容反插回來(lái) 4 適用于有大量重復(fù)記錄的情況 exception into 子句法 采用 alter table 命令中的 exception into 子句也能確定出庫(kù)表中重復(fù)的記錄 這種方法稍微麻 煩一些 為了使用 excepeion into 子句 必須首先創(chuàng)建 exceptions 表 創(chuàng)建該表的 sql 腳本文件為 utlexcpt sql 對(duì)于 win2000 系統(tǒng)和 unix 系統(tǒng) oracle 存放該文件的位置稍有 不同 在 win2000 系統(tǒng)下 該腳本文件存放在 oracle home ora90 rdbms admin 目錄下 而對(duì)于 unix 系統(tǒng) 該腳本文件存放在 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 從結(jié)果里能看到重復(fù)記錄已刪除 3 實(shí)例 數(shù)據(jù)庫(kù)中有 deny mobile 表 需要按照 mobile 去重復(fù) 刪除重復(fù)記錄為 Delete from SMS DENY
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 單神經(jīng)病的臨床護(hù)理
- 2025年商業(yè)寫(xiě)字樓租賃合同模板
- 浙江國(guó)企招聘2025臺(tái)州市城市建設(shè)投資發(fā)展集團(tuán)有限公司所屬企業(yè)招聘13人筆試參考題庫(kù)附帶答案詳解
- 陜西一年級(jí)上試卷及答案
- 肇慶市實(shí)驗(yàn)中學(xué)高中歷史二:第課戰(zhàn)后資本主義經(jīng)濟(jì)的調(diào)整高效課堂教學(xué)設(shè)計(jì)
- 2025年中國(guó)勾環(huán)市場(chǎng)調(diào)查研究報(bào)告
- 紡織品及針織品售后服務(wù)考核試卷
- 木材與竹材的干燥技術(shù)對(duì)制漿影響考核試卷
- 石油開(kāi)采與全球能源供需考核試卷
- 腈綸纖維在風(fēng)力發(fā)電葉片的應(yīng)用考核試卷
- 髖臼骨折護(hù)理查房
- 《支持向量機(jī)SVM》課件
- 住院醫(yī)師規(guī)范化培訓(xùn)中的病例討論總結(jié)
- 砂石廠現(xiàn)場(chǎng)管理方案
- 雙人心肺復(fù)蘇術(shù)考核評(píng)分標(biāo)準(zhǔn)
- 學(xué)會(huì)傾聽(tīng) 養(yǎng)成習(xí)慣
- 循環(huán)流化床鍋爐主要設(shè)備及系統(tǒng)課件
- 扁桃體切除術(shù)與術(shù)后并發(fā)癥
- 防溺水自救施救技能培訓(xùn)內(nèi)容
- GB/T 10561-2023鋼中非金屬夾雜物含量的測(cè)定標(biāo)準(zhǔn)評(píng)級(jí)圖顯微檢驗(yàn)法
- 人工智能技術(shù)在初中英語(yǔ)教學(xué)中的應(yīng)用
評(píng)論
0/150
提交評(píng)論