oracle主键自动增长

oracle的主键没有自动怎样功能,小编今天给大家说说怎样设置主键自动增长:
1、创建表;
create table tb_user
(
id NUMBER(10) not null,
createtime DATE not null,
constraint PK_tb_user primary key (id)
);
2、创建序列
create sequence seq_tb_user
minvalue 1
nomaxvalue
start with 1
increment by 1
nocycle –一直累加,不循环
–nocache; –不缓存
cache 10; –缓存10条
打开之后,设置好最大、最小、开始位置、每次递增多少、是否循环、是否缓存等
3、创建触发器(如果insert语句没有传ID自动递增)
CREATE OR REPLACE TRIGGER tr_tb_user
BEFORE INSERT ON tb_user FOR EACH ROW WHEN (new.id is null)
begin
select seq_tb_user.nextval into:new.id from dual;
end;
打开触发器模版向导窗口之后,填写好名称,触发方式,事件,表名
在begin的后面添加:select 表名.nextval into:new.id from dual;
小编这个上面有plsql的操作流程,大家可以上去看看:
http://note.youdao.com/noteshare?id=9e69774ec83dcb4f4d04b15549a5172d

猜你喜欢

转载自blog.csdn.net/qq_36323075/article/details/80091325