How to create id with AUTO_INCREMENT on Oracle?

后端 未结 16 1595
死守一世寂寞
死守一世寂寞 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:36

    it is called Identity Columns and it is available only from oracle Oracle 12c

    CREATE TABLE identity_test_tab
    (
       id            NUMBER GENERATED ALWAYS AS IDENTITY,
       description   VARCHAR2 (30)
    );
    

    example of insert into Identity Columns as below

    INSERT INTO identity_test_tab (description) VALUES ('Just DESCRIPTION');
    

    1 row created.

    you can NOT do insert like below

    INSERT INTO identity_test_tab (id, description) VALUES (NULL, 'ID=NULL and DESCRIPTION');
    

    ERROR at line 1: ORA-32795: cannot insert into a generated always identity column

    INSERT INTO identity_test_tab (id, description) VALUES (999, 'ID=999 and DESCRIPTION');
    

    ERROR at line 1: ORA-32795: cannot insert into a generated always identity column

    useful link

提交回复
热议问题