<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    konhon

    忘掉過去,展望未來。找回自我,超越自我。
    逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

    Google

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks
     

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

    SQL> desc employee

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

    emp_id                                                NUMBER(10)
    emp_name                                           VARCHAR2(20)

    salary                                                  NUMBER(10,2)

     

     

    可以通過下面的語句查詢重復的記錄:

    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;  (再將臨時表里的內容插回來)

     

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

     

     

    可以通過下面的語句查詢重復的記錄:

    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;  (再將臨時表里的內容插回來)

     

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

    posted on 2005-08-25 02:40 konhon 優(yōu)華 閱讀(747) 評論(0)  編輯  收藏 所屬分類: MS SQL Server
    主站蜘蛛池模板: 四虎国产精品免费久久| 久久久久久夜精品精品免费啦 | 人妻18毛片a级毛片免费看| 全部免费毛片免费播放| 免费无码国产V片在线观看| 亚洲日韩人妻第一页| 成人片黄网站色大片免费观看APP| 亚洲Av无码专区国产乱码DVD| 久久久久久久岛国免费播放| 亚洲人成777在线播放| 在线jlzzjlzz免费播放| 欧美日韩亚洲精品| 青青青国产在线观看免费| 色婷五月综激情亚洲综合| 国产在线播放免费| 成人无码区免费A∨直播| 91精品国产亚洲爽啪在线观看| 免费视频专区一国产盗摄| 最新亚洲人成无码网站| 亚洲成AV人在线播放无码| 巨波霸乳在线永久免费视频 | 久久精品国产亚洲AV天海翼| 红杏亚洲影院一区二区三区 | 波多野结衣中文一区二区免费| 成人免费乱码大片A毛片| 亚洲人成7777影视在线观看| 亚洲AV无码一区二三区| 热re99久久6国产精品免费| 亚洲狠狠婷婷综合久久蜜芽| 久久亚洲国产精品一区二区| 亚洲黄色免费网站| 一边摸一边爽一边叫床免费视频| 亚洲AV电影院在线观看| 免费一级做a爰片性色毛片| 免费精品无码AV片在线观看| 特级毛片A级毛片100免费播放| 亚洲欧洲免费视频| www.亚洲精品.com| 在线看无码的免费网站| 精品人妻系列无码人妻免费视频| 亚洲AV永久无码精品一福利|