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技術相關

          主站蜘蛛池模板: 寿阳县| 龙里县| 大冶市| 南和县| 定兴县| 将乐县| 苍溪县| 岗巴县| 沙湾县| 南皮县| 永吉县| 巨野县| 卢氏县| 湖南省| 吉安县| 句容市| 萍乡市| 盐城市| 临夏县| 马龙县| 鹿邑县| 长宁区| 措勤县| 平阳县| 岐山县| 土默特左旗| 塘沽区| 隆昌县| 吐鲁番市| 北海市| 鄯善县| 土默特左旗| 大方县| 沿河| 浦江县| 天长市| 金坛市| 长乐市| 东至县| 南通市| 涞源县|