joey0210

oracle实现主键自增

由于ORACLE设置主键是不会自动增加的,所以必须用 序列 和 触发器 来完成主键的递增

1、建立数据表

  

create table Test_Increase(
           userid 
number(10primary key,  /*建立主键*/
           username 
varchar2(20)
           );

2、创建自动增长序列

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

3、创建触发器

CREATE TRIGGER Test_Increase BEFORE
insert ON  Test_Increase FOR EACH ROW
begin
select TestIncrease_Sequence.nextval into:New.userid from dual;

end;
执行上面3步语句
完成!

分类:

技术点:

相关文章:

  • 2022-03-15
  • 2021-12-07
  • 2022-02-16
  • 2021-09-14
  • 2022-02-01
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-05-20
  • 2022-12-23
  • 2022-01-02
  • 2022-02-03
相关资源
相似解决方案