oracle如何更改标识列的下一个自动生成的值

oracle how to change the next autogenerated value of the identity column

我创建了 table 个这样的项目:

CREATE TABLE projects (
  project_id NUMBER(10,0) GENERATED BY DEFAULT ON NULL AS IDENTITY ,
  project_name VARCHAR2(75 CHAR) NOT NULL

然后我在从旧 MySQL table 导入数据时插入了约 150,000 行。 MySQL 有我需要保留的现有 ID 号,因此我在插入期间将 ID 号添加到 SQL。现在,当我向 oracle table 中插入新行时,id 是一个非常低的数字。你能告诉我如何将 project_id 列上的计数器重置为从 150,001 开始,以免弄乱我现有的 ID 号码吗?本质上我需要 oracle 版本:

ALTER TABLE tbl AUTO_INCREMENT = 150001;

编辑:Oracle 12c 现在支持身份数据类型,允许自动编号主键,不需要我们创建序列 + 插入触发器。

解决方案: 经过一些创造性的 google 搜索词后,我能够在 oracle 文档站点上找到 this 线程。这是更改身份的 nextval 的解决方案:

ALTER TABLE     projects    MODIFY  project_id  GENERATED BY DEFAULT ON NULL AS IDENTITY ( START WITH   150000);

以下语句在示例模式 oe 中创建序列 customers_seq。当向客户 table.

添加行时,此序列可用于提供客户 ID 号

创建序列customers_seq 从 1000 开始 增加 1 无缓存 摩托车;

第一个引用 customers_seq.nextval returns 1000。第二个 returns 1001。每个后续引用都会 return 一个比前一个引用大 1 的值。

http://docs.oracle.com/cd/B12037_01/server.101/b10759/statements_6014.htm

这是我在 this oracle thread: 上找到的解决方案。这个概念是改变你的身份列而不是调整序列。实际上,自动创建的序列是不可编辑或不可拖放的。

ALTER TABLE     projects    MODIFY  project_id  GENERATED BY DEFAULT ON NULL AS IDENTITY ( START WITH   150000);

根据这个source,你可以这样做:

ALTER TABLE projects MODIFY project_id
    GENERATED BY DEFAULT ON NULL AS IDENTITY (START WITH LIMIT VALUE);

The START WITH LIMIT VALUE clause can only be specified with an ALTER TABLE statement (and by implication against an existing identity column). When this clause is specified, the table will be scanned for the highest value in the PROJECT_ID column and the sequence will commence at this value + 1.

OP 自己的回答中引用的 oracle thread 中也有同样的说明:

START WITH LIMIT VALUE, which is specific to identity_options, can only be used with ALTER TABLE MODIFY. If you specify START WITH LIMIT VALUE, then Oracle Database locks the table and finds the maximum identity column value in the table (for increasing sequences) or the minimum identity column value (for decreasing sequences) and assigns the value as the sequence generator's high water mark. The next value returned by the sequence generator will be the high water mark + INCREMENT BY integer for increasing sequences, or the high water mark - INCREMENT BY integer for decreasing sequences.