如何查找、刪除表中重復(fù)的記錄
軟件環(huán)境:
1、Windows NT4.0+ORACLE 8.0.4
2、ORACLE安裝路徑為:C:"ORANT
問題提出:
1、當(dāng)我們想要為一個(gè)表創(chuàng)建唯一索引時(shí),如果該表有重復(fù)的記錄,則無法創(chuàng)建成功。
方法原理:
1、Oracle中,每一條記錄都有一個(gè)rowid,rowid在整個(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)