Oracle中建立自動標識列

2021-06-16 04:03:37 字數 1649 閱讀 1079

在oracle中建立標識列,不同於sql server中的是,sqlserver中叫自動標識列entity,而oracle中叫序列(sequence)。

在oracle中建立序列(sequence)分為如下幾步:

建立乙個測試表

create tabletb_test(

useridnumber primary key,

usernaemvarchar2(128)not null,

lognamevarchar2(128)not null,

logpasswordvarchar2(128)not null,

departidnumber

);建立乙個序列(sequence),針對主鍵userid

create sequenceseq_userid    --建立乙個序列

minvalue1                    --該序列的最小值是1

start with1                  --從1開始遞增

increment by1                --每次遞增1

nomaxvalue--沒有最大值

nocache;                       --沒有快取

建立乙個觸發器(trigger)

begin

execute immediate'create or replace trigger trigger_userid  '||

before insert on "tb_test" '||

for each row '||

begin  '||

if inserting then '||

if :new."userid" is null then '||

select seq_userid.nextval into :new."userid" from dual; '||

end if; '||

end if; '||

end;';

end;

啟用觸發器(enable trigger)

alter table"scott"."tb_test"enable all triggers;    --scott為當前登入的資料庫管理員,tb_test為當前操作的表名

插入資料進行測試

insert intotb_test(username,logname,logpassword,departid)values('mr.wang','admin','pwd',2);

------------測試成功

Oracle中建立自動標識列

1 建立測試表admin create table admin id varchar2 20 name varchar2 10 2 建立乙個序列 sequence 針對主鍵id create sequence innerid 建立乙個序列 minvalue 1 該序列的最小值是1 maxvalue ...

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...