oracle 自增 字段

1、創建序列

create sequence my_seq 
start with 10000000  
increment by 1  
nomaxvalue 

start with   #開始數

increment by   #步長

nomaxvalue   #不限制最大數


SQL語句自增字段賦值

insert into mytable(ID,VALUE)values(my_seq.nextval,'myvalue'); 


2、爲序列做一個insert時的觸發器

create trigger my_trigger 
before insert on mytable for each row 
begin 
select my_seq.nextval into :new.mytable_id from dual; 
end my_trigger; 

其中

my_trigger   #觸發器的名字

mytable   #加觸發器的表

my_seq   #剛纔加的序列名

mytable_id   #自增的主鍵名


PS:有了自增字段後,insert帶id不會被採用,會被序列裏面的ID替代。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章