ORACLE如何实现ID自动增长

createsequenceTBL_PIC_PRODUCT_SEQ

minvalue1

maxvalue9999999999999999999999999999

startwith1

incrementby1

cache20;

createorreplacetriggerTBL_PIC_PRODUCT_TRIGbeforeinsertonTBL_PIC_PRODUCT

referencingoldasoldnewasnew

foreachrow

declarenext_idnumber;

begin

select

TBL_PIC_PRODUCT_SEQ.nextval

into

next_id

from

dual;

:new.info_id:=next_id;

end;

/

---------------

--建立序列

createsequenceseq_name

incrementby1

startwith1

maxvalue99999999

nocycle

cache10

--调用:

insertintotable(id,name)values(seq_name.nextval,yourname);

-----------

第一步:创建SEQUENCE

createsequences_country_idincrementby1startwith1maxvalue999999999;

第二步:创建一个基于该表的beforeinsert触发器,在触发器中使用该SEQUENCE

createorreplacetriggerbef_ins_t_country_define

beforeinsertont_country_define

referencingoldasoldnewasnewforeachrow

begin

selects_country_id.nextvalinto:new.country_idfromdual;

end;

/

相关推荐