Oracle通过Sequence和触发器实现ID自动增涨
很多公司用的是Oracle数据库,又要求ID自动增涨,但是目前没有现成的方法像SQL Server一样用auto_increatment方法。
www.2cto.com
下面我写个Sequence和触发器实现自动增长功能。(如果有更好的方法请给我留言,谢谢)
1.创建一个Sequence
Sql代码
Create Sequence reader_sequence
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;
2.创建一张表
Sql代码
— 权限
create table privilege (
id number(19,0) not null,–流水号
name varchar2(255 char) not null unique,–权限名称
description varchar2(255 char) null, –权限描述
code varchar2(64 char) not null unique, — 代码
feature varchar2(64 char) not null, — 特性名称
module varchar2(64 char) not null — 模块名称
primary key (id)
);
www.2cto.com
3.给表增加触发器,自动获得ID
Sql代码
create or replace trigger &nboracle账号sp; privilege_trigger
before insert on privilege
referencing old as old new as new for each row
begin
select reader_sequence.nextval into :new.id from dual;
end;