Oracle数据库增加自增字段
1.创建表
1 create table person{ 2 id varchar2(50) not null, 3 name varchar2(50), 4 nums number not null --自增字段 5 }
2.创建序列
1 create sequence PERSON_NUMS_SEQ --PERSON_NUMS_SEQ为序列名称 2 minvalue 1 --最小值 3 nomaxvalue --不设置最大值 4 start with 1 --开始值 5 increment by 1 --递增值 6 nocycle --一直累加,不循环 7 nocache --不建立缓冲区
3.测试序列
insert into person (id,name,nums) values(SYS_GUID(),'张三',PERSON_NUMS_SEQ.nextval)
4.创建触发器
1 create or replace trigger PERSON_AUTO_ADD --PERSON_AUTO_ADD为触发器名称 2 before insert on person --person为目标表 3 for each row 4 begin 5 select PERSON_NUMS_SEQ.nextval into :new.nums from dual; --nums为自增字段 6 end;
5.测试
insert into person (id,name) values(SYS_GUID(),'李四')
浙公网安备 33010602011771号