我有一个表,其中包含一个从应用程序自动递增的主键列。如何将列修改为 身份栏在 Oracle 12c 中?
下面提供了一个示例案例-
create table tmp_identity (
id number(100) primary key,
value varchar2(100)
);
假设我们用以下数据填充了表格 -
ID VALUE
---------------
1 Sample 1
2 Sample 2
3 Sample 3
我们打算做的是转这个
id
列成身份栏这将——我该怎么做?
如果不可能,那么是否有任何可用的解决方法?
最佳答案
您无法将现有列转换为真实标识列,但您可以通过使用序列作为列的默认值来获得类似的行为。
create sequence seq_tmp_identity_id
start with 4
increment by 1;
然后使用:
alter table tmp_identity
modify id
default seq_tmp_identity_id.nextval;
使列使用序列作为默认值。如果你愿意,你可以使用
default on null
覆盖显式 null
插入期间提供的值(这是与标识列最接近的值)如果你想要一个 真实您需要删除当前的身份列
id
列,然后将其重新添加为标识列:alter table tmp_identity drop column id;
alter table tmp_identity
add id number(38)
generated always as identity;
请注意,您不应该添加
start with 4
在这种情况下,所有行都会获得一个新的唯一编号
关于sql - 甲骨文 12c : How can I modify an existing primary key column to an identity column?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32976743/