日期:2014-05-16  浏览次数:20479 次

oracle 自增主键

一、创建表

???? create?table?tables(
???????????userid?
number(10)?primary?key,??/*主键,自动增加*/
???????????username?
varchar2(20)
???? );

?

二、创建序列

?? CREATE?SEQUENCE?id_Sequence
?????? INCREMENT?
BY?1???--?每次加几个??
???? ??START?WITH?1?????--?从1开始计数??
???? ??NOMAXVALUE???????--?不设置最大值??
???????NOCYCLE??????????--?一直累加,不循环??
???? ??CACHE?10;?

三、创建触发器

CREATE?TRIGGER?id_trigger BEFORE
insert?ON??tables FOR?EACH?ROW
begin
select?id_Sequence.nextval?into:New.userid?from?dual;
end;

四、测试

insert?into?Test_Increase(Username)