创建序列代码:
创建触发器代码:
--创建sequence
CREATE SEQUENCE test_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10 ;
创建触发器代码:
create or replace trigger tri_test_id
before insert on test --test 是表名
for each row
declare
nextid number;
begin
IF :new.id IS NULLor :new.id=0 THEN --id是列名
select test_sequence.nextval --SEQ_ID正是刚才创建的
into nextid
from sys.dual;
:new.id:=nextid;
end if;
end tri_test_id;