Oracle中建立自動標識列

2021-07-23 08:49:05 字數 1206 閱讀 6892

1、建立測試表admin

create table admin

(id varchar2(20),

name varchar2(10));

2、建立乙個序列(sequence),針對主鍵id:

create sequence innerid//--建立乙個序列

minvalue 1//--該序列的最小值是1

maxvalue 99999999999999//--最大值    

nomaxvalue   //--沒有最大值

start with 1//--初始值從1開始遞增

increment by 1//--每次遞增1

cache 20//--設定快取cache個序列,如果系統down掉了或者其它情況將會導致序列不連續,也可以設定為nocache

nocache    //--序列不放在快取中

cycle      

//--到最大值從初始值開始

nocycle    

//--一直累加,序列不迴圈

order;     //

3、向表admin中插入值:

currval=返回 sequence的當前值

nextval=增加sequence的值,然後返回 sequence 值

insert into admin values (innerid.nextval,'a');

insert into admin values (innerid.nextval,'b');

insert into admin values (innerid.nextval,'c');

insert into admin values (innerid.nextval,'d');

4、建立觸發器(注意這裡無法設定id的預設值為innerid.nextval ).

create or replace trigger admin_tg

before insert 

on admin 

for each row 

as begin 

select innerid.nextval into 

:new.id from dual;

end;

需要注意的是:建立觸發器時表名、序列名必須大寫.

Oracle中建立自動標識列

在oracle中建立標識列,不同於sql server中的是,sqlserver中叫自動標識列entity,而oracle中叫序列 sequence 在oracle中建立序列 sequence 分為如下幾步 建立乙個測試表 create tabletb test useridnumber prima...

Oracle中為表設定自動增長的標識列

建立序列 create sequence 序列名稱 start with 1 起始值 increment by 1 增量 maxvalue 99999999 最大值 nocycle 達到最大值後是否重新計算,當前為不重新計算,cycle為重新計算 nocache 不要快取,容易跳號 建立觸發器 cr...

SQL SERVER重置自動編號列 標識列

兩種方法 一種是用truncate truncate table name 可以刪除表內所有值並重置標識值 二是用dbcc checkident dbcc checkident table name reseed,new reseed value 如dbcc checkident bc pos re...