1. 程式人生 > >oracle 實現主鍵自增

oracle 實現主鍵自增

with 授權 循環 col cycle arc scott 最小 每次

-- 創建表

drop table test;

create table test(id number(10), name varchar2(10));

-- 創建對列

drop sequence seq_id;

create sequence seq_id minvalue 1 nomaxvalue start with 1 increment by 1 nocycle nocache;

/*

minvalue 1 最小值

nomaxvalue 不設置最大值(由機器決定),或根據表字段的值範圍設置 maxvalue

start with 1 從1開始計數,數值可變

increment by 1 每次加1,數值可變

nocycle 一直累加,不循環

nocache 不建緩沖區,如果建立cache那麽系統將自動讀取cache值個seq,這樣會加快運行速度;

如果當機或oracle死了,那麽下次讀取的seq值將不連貫*/

-- sys 登陸授權

grant create trigger to scott;

-- 創建觸發器, 註意創建觸發器 “end;” 後面必須加一個回車和“/”,才可以執行;

create or replace trigger test_id_increment

before insert on test

for each row

begin

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

end;

/

-- 測試

select * from test;

insert into test(name) values(‘張三‘);

insert into test values(null, ‘張三‘);

select * from test;

oracle 實現主鍵自增