The important thing in life is to have a great aim , and the determination

          導航

          <2008年1月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(7)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊

          閱讀排行榜

          評論排行榜

          常用鏈接

          統計

          IT技術鏈接

          保險相關

          友情鏈接

          基金知識

          生活相關

          最新評論

          如何查找、刪除表中重復的記錄

          軟件環境:

          1、Windows NT4.0+ORACLE 8.0.4

          2、ORACLE安裝路徑為:C:"ORANT

          問題提出:

          1、當我們想要為一個表創建唯一索引時,如果該表有重復的記錄,則無法創建成功。

          方法原理:

          1、Oracle中,每一條記錄都有一個rowid,rowid在整個數據庫中是唯一的,

            rowid確定了每條記錄是在ORACLE中的哪一個數據文件、塊、行上。

          2、在重復的記錄中,可能所有列的內容都相同,但rowid不會相同,所以只要確定出重復記錄中

            那些具有最大rowid的就可以了,其余全部刪除。

          3、以下語句用到了3項技巧:rowid、子查詢、別名。

          實現方法:

          SQL> create table a (

           2 bm char(4),            --編碼

           3 mc varchar2(20)            --名稱

           4 )

           5 /

          表已建立.

          SQL> insert into a values('1111','1111');

          SQL> insert into a values('1112','1111');

          SQL> insert into a values('1113','1111');

          SQL> insert into a values('1114','1111');

          SQL> insert into a select * from a;

          插入4個記錄.

          SQL> commit;

          完全提交.

          SQL> select rowid,bm,mc from a;

          ROWID              BM   MC

          ------------------ ---- -------

          000000D5.0000.0002 1111 1111

          000000D5.0001.0002 1112 1111

          000000D5.0002.0002 1113 1111

          000000D5.0003.0002 1114 1111

          000000D5.0004.0002 1111 1111

          000000D5.0005.0002 1112 1111

          000000D5.0006.0002 1113 1111

          000000D5.0007.0002 1114 1111

           

          查詢到8記錄.

          查出重復記錄

          SQL> select rowid,bm,mc from a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);

          ROWID              BM   MC

          ------------------ ---- --------------------

          000000D5.0000.0002 1111 1111

          000000D5.0001.0002 1112 1111

          000000D5.0002.0002 1113 1111

          000000D5.0003.0002 1114 1111

           

          刪除重復記錄

          SQL> delete from a a where a.rowid!=(select max(rowid) from a b where a.bm=b.bm and a.mc=b.mc);

          刪除4個記錄.

          SQL> select rowid,bm,mc from a;

          ROWID              BM   MC

          ------------------ ---- --------------------

          000000D5.0004.0002 1111 1111

          000000D5.0005.0002 1112 1111

          000000D5.0006.0002 1113 1111

          000000D5.0007.0002 1114 1111

           

          come from :: http://dev.csdn.net/article/59/59333.shtm

          -測試數據

          /*-----------------------------

          select * from tt

          -----------------------------*/

          id          pid        

          ----------- -----------

          1           1

          1           1

          2           2

          3           3

          3           3

          3           3

          (所影響的行數為 6 行)

          首先,如何查詢table中有重復記錄

          select *,count(1) as rownum

          from tt

          group by id, pid

          having count(1) > 1

          id          pid         rownum     

          ----------- ----------- -----------

          1           1           2

          3           3           3

           

          (所影響的行數為 2 行)

          方法一:使用distinct和臨時表

          if object_id('tempdb..#tmp') is not null

          drop table #tmp

          select distinct * into #tmp from tt

          truncate table tt

          insert into tt select * from #tmp

          方法二:添加標識列

          alter table tt add NewID int identity(1,1)

          go 

          delete from tt where exists(select 1 from tt a where a.newid>tt.newid and tt.id=a.id and tt.pid=a.pid)

          go

          alter table tt drop column NewID

          go

          --測試結果

          /*-----------------------------

          select * from tt

          -----------------------------*/

          id          pid        

          ----------- -----------

          1           1

          2           2

          3           3

          (所影響的行數為 3 行)

          *---*-- * 8 8 * * * * 8* * * * 8 8 *

          USE CEO

          CREATE TABLE TT

          (

          TTNO CHAR(4),

          TTNAME VARCHAR(10)

          )

          INSERT INTO TT (TTNO,TTNAME) VALUES ('1425','WHERE')

          INSERT INTO TT (TTNO,TTNAME) VALUES ('1425','WHERE')

          INSERT INTO TT (TTNO,TTNAME) VALUES ('1424','WHEREIS')

          INSERT INTO TT (TTNO,TTNAME) VALUES ('1435','WHEREIS')

          INSERT INTO TT (TTNO,TTNAME) VALUES ('1435','WHEREIS')

          方法二:添加標識列(最有效方法)

          alter table tt add newid2 int identity(1,1)

          go

          delete from tt where exists( select 1 from tt   a where a.newid2>tt.newid2 and tt.ttno=a.ttno and tt.ttname=a.ttname)

          alter table tt drop column newid2

          go

          select * from tt

          posted on 2008-01-22 01:57 鴻雁 閱讀(296) 評論(0)  編輯  收藏 所屬分類: IT技術相關

          主站蜘蛛池模板: 白朗县| 雅安市| 太保市| 三都| 耒阳市| 双峰县| 嵊州市| 洛浦县| 鱼台县| 堆龙德庆县| 墨脱县| 泽普县| 和硕县| 八宿县| 玛多县| 琼结县| 比如县| 阜城县| 积石山| 武宣县| 那坡县| 确山县| 汾西县| 乌兰县| 临清市| 平遥县| 隆安县| 阿合奇县| 邵东县| 昭苏县| 崇礼县| 蓝田县| 新闻| 凤城市| 沙雅县| 青铜峡市| 当涂县| 淄博市| 德阳市| 扶风县| 九龙县|