Oracle SQL |当提供ID时,不会自动增加ID



我有以下递增id:

create table PATIENT (
PATIENTID            INTEGER             
generated by default on null as identity ( start with 1 nocycle order)  not null
);

我注意到,当我提供一个id时(例如在我的第一次插入中),创建的序列中的id不会增加。

因此,如果我添加id为1的患者,然后添加id为NULL的患者,我会得到一个错误。

有办法避免这种情况吗?或者我必须从我的插入脚本中删除所有ID ?

如果为标识列提供一个(非空)值,则序列保持相同的值。这意味着标识可以尝试插入您手动提供的值。

这里有几个路径可以选择

永远不要为标识列提供值。将其设置为generated always,以确保没有人可以这样做:

create table patient (
patientid integer             
generated always as identity (
start with 1 nocycle order
)  not null primary key
);
insert into patient 
values ( 1 );

ORA-32795: cannot insert into a generated always identity column

允许脚本提供值,但在使用alter table后立即将标识序列重置为maxvalue列:

drop table  patient 
cascade constraints purge;
create table patient (
patientid integer             
generated by default on null as identity (
start with 1 nocycle order
)  not null primary key
);
insert into patient 
values ( 1 );
insert into patient 
values ( 11 );
commit;
insert into patient 
values ( default );

ORA-00001: unique constraint (CHRIS.SYS_C0024892) violated

alter table patient 
modify patientid  
generated by default on null as identity (
start with limit value 
);
insert into patient 
values ( default );
select * from patient;
PATIENTID   
1 
11 
12 

最新更新