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

oracle触发器写一个主键自增
1.首先创建一个序列:
create sequence s_user_info //user_info是表名
       increment by 1
       start with 1 maxvalue 99999999
       nocycle
       nocache
//以下是查看数据库里面的序列
select sequence_name from user_sequences

2.然后创建触发器
create or replace trigger tri_user_info
  before insert on user_info
  for each row
declare
  -- local variables here
  nextid number;
begin

    select s_user_info.nextval//此为上面创建的序列
    into nextid
    from sys.dual;
    :new.ID:=nextid;//ID为主键
end tri_user_info;