How to create id with AUTO_INCREMENT on Oracle?

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

    Here are three flavors:

    1. numeric. Simple increasing numeric value, e.g. 1,2,3,....
    2. GUID. globally univeral identifier, as a RAW datatype.
    3. GUID (string). Same as above, but as a string which might be easier to handle in some languages.

    x is the identity column. Substitute FOO with your table name in each of the examples.

    -- numerical identity, e.g. 1,2,3...
    create table FOO (
        x number primary key
    );
    create sequence  FOO_seq;
    
    create or replace trigger FOO_trg
    before insert on FOO
    for each row
    begin
      select FOO_seq.nextval into :new.x from dual;
    end;
    /
    
    -- GUID identity, e.g. 7CFF0C304187716EE040488AA1F9749A
    -- use the commented out lines if you prefer RAW over VARCHAR2.
    create table FOO (
        x varchar(32) primary key        -- string version
        -- x raw(32) primary key         -- raw version
    );
    
    create or replace trigger FOO_trg
    before insert on FOO
    for each row
    begin
      select cast(sys_guid() as varchar2(32)) into :new.x from dual;  -- string version
      -- select sys_guid() into :new.x from dual;                     -- raw version
    end;
    /
    

    update:

    Oracle 12c introduces these two variants that don't depend on triggers:

    create table mytable(id number default mysequence.nextval);
    create table mytable(id number generated as identity);
    

    The first one uses a sequence in the traditional way; the second manages the value internally.

    0 讨论(0)
  • 2020-11-21 05:36

    Trigger and Sequence can be used when you want serialized number that anyone can easily read/remember/understand. But if you don't want to manage ID Column (like emp_id) by this way, and value of this column is not much considerable, you can use SYS_GUID() at Table Creation to get Auto Increment like this.

    CREATE TABLE <table_name> 
    (emp_id RAW(16) DEFAULT SYS_GUID() PRIMARY KEY,
    name VARCHAR2(30));
    

    Now your emp_id column will accept "globally unique identifier value". you can insert value in table by ignoring emp_id column like this.

    INSERT INTO <table_name> (name) VALUES ('name value');
    

    So, it will insert unique value to your emp_id Column.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-21 05:37

    There is no such thing as "auto_increment" or "identity" columns in Oracle as of Oracle 11g. However, you can model it easily with a sequence and a trigger:

    Table definition:

    CREATE TABLE departments (
      ID           NUMBER(10)    NOT NULL,
      DESCRIPTION  VARCHAR2(50)  NOT NULL);
    
    ALTER TABLE departments ADD (
      CONSTRAINT dept_pk PRIMARY KEY (ID));
    
    CREATE SEQUENCE dept_seq START WITH 1;
    

    Trigger definition:

    CREATE OR REPLACE TRIGGER dept_bir 
    BEFORE INSERT ON departments 
    FOR EACH ROW
    
    BEGIN
      SELECT dept_seq.NEXTVAL
      INTO   :new.id
      FROM   dual;
    END;
    /
    

    UPDATE:

    IDENTITY column is now available on Oracle 12c:

    create table t1 (
        c1 NUMBER GENERATED by default on null as IDENTITY,
        c2 VARCHAR2(10)
        );
    

    or specify starting and increment values, also preventing any insert into the identity column (GENERATED ALWAYS) (again, Oracle 12c+ only)

    create table t1 (
        c1 NUMBER GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1),
        c2 VARCHAR2(10)
        );
    

    Alternatively, Oracle 12 also allows to use a sequence as a default value:

    CREATE SEQUENCE dept_seq START WITH 1;
    
    CREATE TABLE departments (
      ID           NUMBER(10)    DEFAULT dept_seq.nextval NOT NULL,
      DESCRIPTION  VARCHAR2(50)  NOT NULL);
    
    ALTER TABLE departments ADD (
      CONSTRAINT dept_pk PRIMARY KEY (ID));
    
    0 讨论(0)
  • 2020-11-21 05:37

    Maybe just try this simple script:

    http://www.hlavaj.sk/ai.php

    Result is:

    CREATE SEQUENCE TABLE_PK_SEQ; 
    CREATE OR REPLACE TRIGGER TR_SEQ_TABLE BEFORE INSERT ON TABLE FOR EACH ROW 
    
    BEGIN
    SELECT TABLE_PK_SEQ.NEXTVAL
    INTO :new.PK
    FROM dual;
    END;
    
    0 讨论(0)
  • 2020-11-21 05:40

    This is how I did this on an existing table and column (named id):

    UPDATE table SET id=ROWNUM;
    DECLARE
      maxval NUMBER;
    BEGIN
      SELECT MAX(id) INTO maxval FROM table;
      EXECUTE IMMEDIATE 'DROP SEQUENCE table_seq';
      EXECUTE IMMEDIATE 'CREATE SEQUENCE table_seq START WITH '|| TO_CHAR(TO_NUMBER(maxval)+1) ||' INCREMENT BY 1 NOMAXVALUE';
    END;
    CREATE TRIGGER table_trigger
      BEFORE INSERT ON table
      FOR EACH ROW
    BEGIN
      :new.id := table_seq.NEXTVAL;
    END;
    
    0 讨论(0)
提交回复
热议问题