




已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
ORACLE 中刪除重復(fù)記錄中刪除重復(fù)記錄 平時(shí)工作中可能會(huì)遇見當(dāng)試圖對(duì)庫表中的某一列或幾列創(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ù)庫中是唯一的 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í)候 用以下語句效率會(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ù)記錄的情況 用以下語句效率會(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 用來存放重復(fù)的記錄 sql truncate table cz 清空 cz 表的數(shù)據(jù) 但保留 cz 表的結(jié)構(gòu) sql insert into cz select from test 再將臨時(shí)表 test 里的內(nèi)容反插回來 4 適用于有大量重復(fù)記錄的情況 exception into 子句法 采用 alter table 命令中的 exception into 子句也能確定出庫表中重復(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ù)庫中有 deny mobile 表 需要按照 mobile 去重復(fù) 刪除重復(fù)記錄為 Delete from SMS DENY
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年南陽理工學(xué)院輔導(dǎo)員考試真題
- 2024年南方科技大學(xué)輔導(dǎo)員考試真題
- 2024年內(nèi)江市市中區(qū)事業(yè)單位招聘筆試真題
- 2024年梅州蕉嶺縣專場(chǎng)招聘教師筆試真題
- 2024年吉林白山市事業(yè)單位招聘筆試真題
- 數(shù)據(jù)安全及保護(hù)策略試題及答案
- 2025屆甘肅省民樂縣七年級(jí)數(shù)學(xué)第二學(xué)期期末質(zhì)量檢測(cè)模擬試題含解析
- 法學(xué)概論核心要點(diǎn)總結(jié)試題及答案
- 降低備考焦慮的軟件設(shè)計(jì)師試題及答案
- 網(wǎng)絡(luò)安全措施試題及答案
- 電動(dòng)車店合伙人協(xié)議
- 小學(xué)二年級(jí)體育《快速跑》教案
- 開工儀式流程方案
- 2024國家安全員資格考試題庫(含答案)
- 社會(huì)工作知識(shí)點(diǎn)
- 情商認(rèn)知與提升智慧樹知到期末考試答案章節(jié)答案2024年河南大學(xué)
- DZ∕T 0302-2017 煤炭地質(zhì)勘查圖例圖式(正式版)
- 2024年全國青少年信息素養(yǎng)大賽初賽試題
- 2024-2034年中國有機(jī)復(fù)合絕緣子未來趨勢(shì)預(yù)測(cè)分析及投資規(guī)劃研究建議報(bào)告
- 《內(nèi)蒙古自治區(qū)扶持壯大嘎查村級(jí)集體經(jīng)濟(jì)項(xiàng)目和資金管理辦法》(2023修訂)
- 2024年山東省濟(jì)南市歷下區(qū)六年級(jí)下學(xué)期小升初招生語文試卷含答案
評(píng)論
0/150
提交評(píng)論