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

          常用鏈接

          統(tǒng)計(jì)

          IT技術(shù)鏈接

          保險(xiǎn)相關(guān)

          友情鏈接

          基金知識(shí)

          生活相關(guān)

          最新評(píng)論

          如何查找、刪除表中重復(fù)的記錄

          軟件環(huán)境:

          1Windows NT4.0+ORACLE 8.0.4

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

          問題提出:

          1、當(dāng)我們想要為一個(gè)表創(chuàng)建唯一索引時(shí),如果該表有重復(fù)的記錄,則無法創(chuàng)建成功。

          方法原理:

          1Oracle中,每一條記錄都有一個(gè)rowidrowid在整個(gè)數(shù)據(jù)庫中是唯一的,

            rowid確定了每條記錄是在ORACLE中的哪一個(gè)數(shù)據(jù)文件、塊、行上。

          2、在重復(fù)的記錄中,可能所有列的內(nèi)容都相同,但rowid不會(huì)相同,所以只要確定出重復(fù)記錄中

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

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

          實(shí)現(xiàn)方法:

          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個(gè)記錄.

          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記錄.

          查出重復(fù)記錄

          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

           

          刪除重復(fù)記錄

          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個(gè)記錄.

          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

          -測(cè)試數(shù)據(jù)

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

          select * from tt

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

          id          pid        

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

          1           1

          1           1

          2           2

          3           3

          3           3

          3           3

          (所影響的行數(shù)為 6 行)

          首先,如何查詢table中有重復(fù)記錄

          select *,count(1) as rownum

          from tt

          group by id, pid

          having count(1) > 1

          id          pid         rownum     

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

          1           1           2

          3           3           3

           

          (所影響的行數(shù)為 2 行)

          方法一:使用distinct和臨時(shí)表

          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

          方法二:添加標(biāo)識(shí)列

          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

          --測(cè)試結(jié)果

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

          select * from tt

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

          id          pid        

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

          1           1

          2           2

          3           3

          (所影響的行數(shù)為 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')

          方法二:添加標(biāo)識(shí)列(最有效方法)

          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) 評(píng)論(0)  編輯  收藏 所屬分類: IT技術(shù)相關(guān)

          主站蜘蛛池模板: 榆树市| 兖州市| 卢氏县| 静宁县| 红河县| 枣阳市| 罗城| 阳江市| 桂平市| 辽宁省| 南江县| 青海省| 井陉县| 峡江县| 岑溪市| 永丰县| 江西省| 安远县| 木兰县| 达日县| 富顺县| 肥东县| 绥中县| 西安市| 塔河县| 车致| 中江县| 福安市| 淮安市| 赣榆县| 隆德县| 柘荣县| 永昌县| 包头市| 奎屯市| 巴林左旗| 金门县| 邛崃市| 射洪县| 沾益县| 正安县|