軟件環境:
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