日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 芯聞號 > 充電吧
[導(dǎo)讀]學(xué)習(xí)sql有一段時間了,發(fā)現(xiàn)在我建了一個用來測試的表(沒有建索引)中出現(xiàn)了許多的重復(fù)記錄。后來總結(jié)了一些刪除重復(fù)記錄的方法,在Oracle中,可以通過唯一rowid實現(xiàn)刪除重復(fù)記錄;還可以建臨時表來實

學(xué)習(xí)sql有一段時間了,發(fā)現(xiàn)在我建了一個用來測試的表(沒有建索引)中出現(xiàn)了許多的重復(fù)記錄。后來總結(jié)了一些刪除重復(fù)記錄的方法,在Oracle中,可以通過唯一rowid實現(xiàn)刪除重復(fù)記錄;還可以建臨時表來實現(xiàn)...這個只提到其中的幾種簡單實用的方法,希望可以和大家分享(以表employee為例)。

SQL> desc employee

?Name????????????????????????????????????? Null???? Type
?----------------------------------------- -------- ------------------

emp_id??????????????????????????????????????????????? NUMBER(10)
emp_name?????????????????????????????????????????? VARCHAR2(20)

salary???????????????????????????????????????????????? ?NUMBER(10,2)

?

?

可以通過下面的語句查詢重復(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)通過建立臨時表來實現(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; ?(再將臨時表里的內(nèi)容插回來)

?

( 2)通過唯一rowid實現(xiàn)刪除重復(fù)記錄.在Oracle中,每一條記錄都有一個rowid,rowid在整個數(shù)據(jù)庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個數(shù)據(jù)文件、塊、行上。在重復(fù)的記錄中,可能所有列的內(nèi)容都相同,但rowid不會相同,所以只要確定出重復(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)也是通過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

?

?

?

?

SQL> desc employee

?Name????????????????????????????????????? Null???? Type
?----------------------------------------- -------- ------------------

emp_id??????????????????????????????????????????????? NUMBER(10)
emp_name?????????????????????????????????????????? VARCHAR2(20)

salary???????????????????????????????????????????????? ?NUMBER(10,2)

?

?

可以通過下面的語句查詢重復(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)通過建立臨時表來實現(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; ?(再將臨時表里的內(nèi)容插回來)

?

( 2)通過唯一rowid實現(xiàn)刪除重復(fù)記錄.在Oracle中,每一條記錄都有一個rowid,rowid在整個數(shù)據(jù)庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個數(shù)據(jù)文件、塊、行上。在重復(fù)的記錄中,可能所有列的內(nèi)容都相同,但rowid不會相同,所以只要確定出重復(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)也是通過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)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉