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

2022-04-04 19:27:52 字數 1574 閱讀 5105

-- 建立序列

create sequence 序列名稱

start with 1 -- 起始值

increment by 1 -- 增量

maxvalue 99999999 -- 最大值

nocycle -- 達到最大值後是否重新計算,當前為不重新計算,cycle為重新計算

nocache; -- 不要快取,容易跳號

-- 建立觸發器

create or replace trigger 觸發器名稱

before insert on 表名稱 for each row

begin

select 序列名稱.nextval into :new.欄位名稱 from dual;

end;

說明:需要先建個序列,再建與此對應的觸發器。最好每張表都這麼建。

每次插入記錄時就會自動向標識列中插值了。

測試**:

--建立表

create table t_windturbine_day(id number primary key,createdate nvarchar2(25),

windturbinecode varchar(5),indicode number(5),indivalue varchar2(10),

passflage integer default(0) );

--建立序列

create sequence seq_windturbine_day increment by 1 start with 1000000

nomaxvalue nominvalue nocache ;

--建立觸發器

create or replace trigger tr_windturbine_day

before insert on t_windturbine_day for each row

begin select seq_windturbine_day.nextval into :new.id from dual; end;

--插入資料

insert into t_windturbine_day(createdate,windturbinecode,indicode,indivalue)

values('20131108',18,10002,'3.95');

--通過這種方式插入,步長為1,但是id一次長2(第3條數)。為什麼?我認為是觸發器增長了1,

--seq_windturbine_day.nextval又增長了1所導致的。不知道對不對?

insert into t_windturbine_day(id,createdate,windturbinecode,indicode,indivalue)

values(seq_windturbine_day.nextval,'20131108',18,10002,'3.95');

--查詢資料

select * from t_windturbine_day t;

測試結果:

oracle建表並設定ID為自動增長

create tablespace shopping datafile d oracle mypc oradata orcl shopping.dbf size 20m autoextend on create user shopping 建立 使用者 使用者名稱 identified by sho...

oracle建表並設定ID為自動增長

create tablespace shopping datafile d oracle mypc oradata orcl shopping.dbf size 20m autoextend on create user shopping 建立 使用者 使用者名稱 identified by sho...

設定sde表空間為自動增長

有的使用者在測試資料時,希望在sde表空間裡面不受限制地匯入資料,於是需要將sde的表空間設定為自動增長。過程描述 1 可以在建立sde表空間的時候,新增引數autoextend on,修改後建立命令如下 create tablespace sde datafile location sde.dbf...