如何解决甲骨文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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。