快速刪除ORACLE重復(fù)記錄
在Oracle中,可以通過(guò)唯一rowid實(shí)現(xiàn)刪除重復(fù)記錄;還可以建臨時(shí)表來(lái)實(shí)現(xiàn)...這個(gè)只提到其中的幾種簡(jiǎn)單實(shí)用的方法,希望可以和大家分享(以表employee為例)。
SQL> desc employee
Name Null? Type
emp_id NUMBER(10) emp_name VARCHAR2(20)
salary NUMBER(10,2)
可以通過(guò)下面的語(yǔ)句查詢(xún)重復(fù)的記錄: SQL> select * from employee;
EMP_ID EMP_NAME SALARY
1 sunshine 10000
1 sunshine 10000
2 semon 20000
2 semon 20000
3 xyz 30000
2 semon 20000
SQL> select distinct * from employee;
EMP_ID EMP_NAME SALARY
1 sunshine 10000
2 semon 20000
3 xyz 30000
SQL> select * from employee group by emp_id,emp_name,salary having count (*)>1
EMP_ID EMP_NAME SALARY
1 sunshine 10000
2 semon 20000
SQL> select * from employee e1;
where rowid in (select max(rowid) from employe e2; where e1.emp_id=e2.emp_id and;
e1.emp_name=e2.emp_name and e1.salary=e2.salary);
EMP_ID EMP_NAME SALARY
1 sunshine 10000
3 xyz 30000
2 semon 20000;
2. 刪除的幾種方法:
(1)通過(guò)建立臨時(shí)表來(lái)實(shí)現(xiàn)
SQL>create table temp_emp as (select distinct * from employee);
SQL> truncate table employee; (清空employee表的數(shù)據(jù))
SQL> insert into employee select * from temp_emp; (再將臨時(shí)表里的內(nèi)容插回來(lái))
( 2)通過(guò)唯一rowid實(shí)現(xiàn)刪除重復(fù)記錄.在Oracle中,每一條記錄都有一個(gè)rowid,rowid在整個(gè)數(shù)據(jù)庫(kù)中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個(gè)數(shù)據(jù)文件、塊、行上。在重復(fù)的記錄中,可能所有列的內(nèi)容都相同,但rowid不會(huì)相同,所以只要確定出重復(fù)記錄中那些具有最大或最小rowid的就可以了,其余全部刪除。
SQL>delete from employee e2 where rowid not in ( select max(e1.rowid) from employee e1 where;
e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);--這里用min(rowid)也可以。
SQL>delete from employee e2 where rowid <( select max(e1.rowid) from employee e1 where; e1.emp_id=e2.emp_id and e1.emp_name=e2.emp_name and e1.salary=e2.salary);
(3)也是通過(guò)rowid,但效率更高。
SQL>delete from employee where rowid not in ( select max(t1.rowid) from employee t1 group by t1.emp_id,t1.emp_name,t1.salary);--這里用min(rowid)也可以。
EMP_ID EMP_NAME SALARY
1 sunshine 10000
3 xyz 30000
2 semon 20000
相關(guān)文章:
1. MySQL基本調(diào)度策略淺析2. Windwos下MySQL 64位壓縮包的安裝方法學(xué)習(xí)記錄3. 成批更改sql server數(shù)據(jù)庫(kù)所有者的對(duì)象4. 整理Oracle數(shù)據(jù)庫(kù)碎片5. MySQL存儲(chǔ)引擎選擇InnoDB還是MyISAM6. SQL Server 2000 在 win2003上安裝不上,我的解決辦法7. ORACLE中的物化視圖8. MySQL基礎(chǔ)教程9 —— 函數(shù)之日期和時(shí)間函數(shù)9. SQL Server 2005數(shù)據(jù)加密技術(shù)的實(shí)際應(yīng)用10. 傳甲骨文將增加對(duì)MySQL投資與微軟競(jìng)爭(zhēng)
