Oracle主键自增的解决办法

来源:互联网 发布:淘宝怎么赚钱 编辑:程序博客网 时间:2024/06/15 19:15

首先,要创建一个表

create table userinfo

(

   userid   int  not  null  primary key,

   username  varchar(20)  not  null,

   password  varchar(20)  not null,

   sex  varchar(4),

   age  int

)


然后,创建一个序列

create sequence userinfo_seq  minvalue 1 maxvalue

increment by 1

start with 1;


最后,创建一个触发器

create or replace trigger  userinfo_tri

before insert on userinfo

  for each row

begin

select   userinfo_seq.nextval  into :new.userid  from  dual;

end;

/

注意事项:

触发器最后要加一个“/”,用以执行创建触发器的语句。  

0 0
原创粉丝点击