oracle實現自增

2021-06-21 22:16:32 字數 1087 閱讀 9644

--oracle實現自增id

--建立一張t_studentinfo表

create

table t_studentinfo 

(  "id"

integer

notnull

primary

key, 

xsname nvarchar2(120) not

null, 

xsage integer

notnull, 

mobile varchar(12), 

email varchar(50), 

address nvarchar2(300)  

); 

--建立乙個序列,序列名字叫seq_studentinfo_identity

--建立乙個序列(序列名的規則一般建議是以seq開頭,然後下劃線,後面跟你的表名,表名前的t_可以去掉,然後以_identity結尾,用來表示我這個序列是用在id自增字段的序列)

create

sequence seq_studentinfo_identity      

increment by 1 --每次增加幾個,我這裡是每次增加1

start with 1   --從1開始計數

nomaxvalue      --不設定最大值

nocycle         --一直累加,不迴圈

nocache;        --不建緩衝區

--你只有了表和序列還不夠,還需要乙個觸發器來執行它

--建立乙個觸發器 觸發器的名字叫trg_studentinfo_identity

--我自己建議觸發器以trg開頭_後面跟表名,在後面根據情況自己看著辦

create

trigger trg_studentinfo_identity before 

insert

on t_studentinfo for each row when(new.id is

null)  

begin

select id_sequence.nextval into:new.id from dual; 

end; 

ORACLE實現主鍵自增

建序列 create sequence sq public minvalue 1 maxvalue 999999 start with 1 increment by 1 建觸發器 sql create or replace trigger exam tri 2 before insert on t ...

oracle實現主鍵自增

由於oracle設定主鍵是不會自動增加的,所以必須用 序列 和 觸發器 來完成主鍵的遞增 1 建立資料表 create table t test id number 10 primary key,建立主鍵 uname varchar2 20 age number 0,10 varchar2 20 a...

Oracle實現主鍵自增

建表mshow sysuser info create table mshow sysuser info id int primary key,role id int not null,login name varchar2 50 not null,user name varchar2 20 use...