How to create id with AUTO_INCREMENT on Oracle?

后端 未结 16 1647
死守一世寂寞
死守一世寂寞 2020-11-21 04:52

It appears that there is no concept of AUTO_INCREMENT in Oracle, up until and including version 11g.

How can I create a column that behaves like auto increment in Or

16条回答
  •  天涯浪人
    2020-11-21 05:47

    Starting with Oracle 12c there is support for Identity columns in one of two ways:

    1. Sequence + Table - In this solution you still create a sequence as you normally would, then you use the following DDL:

      CREATE TABLE MyTable (ID NUMBER DEFAULT MyTable_Seq.NEXTVAL, ...)

    2. Table Only - In this solution no sequence is explicitly specified. You would use the following DDL:

      CREATE TABLE MyTable (ID NUMBER GENERATED AS IDENTITY, ...)

    If you use the first way it is backward compatible with the existing way of doing things. The second is a little more straightforward and is more inline with the rest of the RDMS systems out there.

提交回复
热议问题