實例講解Oracle數(shù)據(jù)庫自動增加ID的sql
本文的主要內(nèi)容包括:在Oracle中實現(xiàn)自增型ID,刪除數(shù)據(jù)表中的重復記錄。
一.自增型ID
1.首先創(chuàng)建 sequence
create sequence seqmax increment by 1
2.得到一個ID
select seqmax.nextval ID from dual
3.若要刪除一個sequence
drop sequence seqmax;
二.刪除數(shù)據(jù)表中的重復記錄
1.先創(chuàng)建一個表
Create TABLE 'APPTEST' (
'ID' INTEGER primary key NOT NULL,
'MOBILE' nvarchar2(50) NOT NULL
);
2.假設其中手機號大量重復,要刪除重復記錄,可以有如下兩種方法:
(1)簡單利用rowid刪除
delete from APPTEST a where rowid not in (select max(rowid) from APPTEST b where a.mobile=b.mobile);
據(jù)說,這種方法在數(shù)據(jù)量很大時,效率并不高
(2)利用分析函數(shù)
delete APPTEST where rowid in (
select rid from
(select rowid rid,row_number() over(partition by mobile order by id desc) rn from APPTEST )
where rn > 1) ;
(3)做temp表
