<ruby id="h6500"><table id="h6500"></table></ruby>
    1. <ruby id="h6500"><video id="h6500"></video></ruby>
          1. <progress id="h6500"><u id="h6500"><form id="h6500"></form></u></progress>
            • 軟件測試技術(shù)
            • 軟件測試博客
            • 軟件測試視頻
            • 開(kāi)源軟件測試技術(shù)
            • 軟件測試論壇
            • 軟件測試沙龍
            • 軟件測試資料下載
            • 軟件測試雜志
            • 軟件測試人才招聘
              暫時(shí)沒(méi)有公告

            字號: | 推薦給好友 上一篇 | 下一篇

            SQL在軟件測試數據庫中刪除重復記錄

            發(fā)布: 2010-6-24 16:26 | 作者: 網(wǎng)絡(luò )轉載 | 來(lái)源: 領(lǐng)測軟件測試網(wǎng)采編 | 查看: 45次 | 進(jìn)入軟件測試論壇討論

            領(lǐng)測軟件測試網(wǎng) 學(xué)習sql有一段時(shí)間了,發(fā)現在我建了一個(gè)用來(lái)測試的表(沒(méi)有建索引)中出現了許多的重復記錄。后來(lái)總結了一些刪除重復記錄的方法,在Oracle中,可以通過(guò)唯一rowid實(shí)現刪除重復記錄;還可以建臨時(shí)表來(lái)實(shí)現...這個(gè)只提到其中的幾種簡(jiǎn)單實(shí)用的方法,希望可以和大家分享(以表employee為例)。MILY: 'Times New Roman'">

            SQL> desc employee

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

            emp_id                                                NUMBER(10)
            emp_name                                           VARCHAR2(20)

            salary                                                  NUMBER(10,2)

            可以通過(guò)下面的語(yǔ)句查詢(xún)重復的記錄:

            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í)現

            SQL>create table temp_emp as (select distinct * from employee) 

            SQL> truncate table employee; (清空employee表的數據)

            SQL> insert into employee select * from temp_emp;  (再將臨時(shí)表里的內容插回來(lái))

             (2)通過(guò)唯一rowid實(shí)現刪除重復記錄.Oracle中,每一條記錄都有一個(gè)rowid,rowid在整個(gè)數據庫中是唯一的,rowid確定了每條記錄是在Oracle中的哪一個(gè)數據文件、塊、行上。在重復的記錄中,可能所有列的內容都相同,但rowid不會(huì )相同,所以只要確定出重復記錄中那些具有最大或最小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

            SQL> desc employee

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

            emp_id                                                NUMBER(10)
            emp_name                                           VARCHAR2(20)

            salary                                                  NUMBER(10,2)

            可以通過(guò)下面的語(yǔ)句查詢(xún)重復的記錄:

            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

            延伸閱讀

            文章來(lái)源于領(lǐng)測軟件測試網(wǎng) http://kjueaiud.com/

            TAG: sql SQL Sql 記錄 軟件測試 刪除 數據庫


            關(guān)于領(lǐng)測軟件測試網(wǎng) | 領(lǐng)測軟件測試網(wǎng)合作伙伴 | 廣告服務(wù) | 投稿指南 | 聯(lián)系我們 | 網(wǎng)站地圖 | 友情鏈接
            版權所有(C) 2003-2010 TestAge(領(lǐng)測軟件測試網(wǎng))|領(lǐng)測國際科技(北京)有限公司|軟件測試工程師培訓網(wǎng) All Rights Reserved
            北京市海淀區中關(guān)村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
            技術(shù)支持和業(yè)務(wù)聯(lián)系:info@testage.com.cn 電話(huà):010-51297073

            軟件測試 | 領(lǐng)測國際ISTQBISTQB官網(wǎng)TMMiTMMi認證國際軟件測試工程師認證領(lǐng)測軟件測試網(wǎng)

            老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月
              <ruby id="h6500"><table id="h6500"></table></ruby>
              1. <ruby id="h6500"><video id="h6500"></video></ruby>
                    1. <progress id="h6500"><u id="h6500"><form id="h6500"></form></u></progress>