Oracle非主键自增长

实现原理:利用触发器自增长,

首先建一个 sequence;

create sequence JCDA_GFYYXX_ID
minvalue 1
maxvalue 99999999
start with 21
increment by 1
cache 20;

然后建一个触发器:

create or replace trigger JCDA_GFYYXX_TB_ID_CFQ


before insert on JCDA_GFYYXX_TB --JCDA_GFYYXX_TB为表名
for each row
declare
-- local variables here
nextid number; --自定义变量,用于存储从序列中取得的值
begin
IF :new.OBJECTID IS NULL or :new.OBJECTID !=0 THEN
select JCDA_GFYYXX_ID.NEXTVAL --从序列中取得下一个值,赋给变量nextid
into nextid
from DUAL;
:new.OBJECTID:=nextid;
end if;
end JCDA_GFYYXX_TB_ID_CFQ;

你可能感兴趣的:(oracle)