Oracle SQL |提供ID时,自动递增的ID不会递增
我有以下递增id:Oracle SQL |提供ID时,自动递增的ID不会递增,sql,oracle,identity-column,Sql,Oracle,Identity Column,我有以下递增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的患者,我会得到一个错误
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吗?如果为标识列提供(非空)值,则序列将保持相同的值。这意味着标识可以尝试插入您手动提供的值
在这里你可以走几条路
切勿为标识列提供值。将其设置为“始终生成”,以确保无人可以执行此操作:
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
不要提供一个值。这就是这个序列的目的。你不能两者都做。插入将使用您传入的任何内容,并且在该点对序列不做任何操作。