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