How to create id with AUTO_INCREMENT on Oracle?

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

    SYS_GUID returns a GUID-- a globally unique ID. A SYS_GUID is a RAW(16). It does not generate an incrementing numeric value.

    If you want to create an incrementing numeric key, you'll want to create a sequence.

    CREATE SEQUENCE name_of_sequence
      START WITH 1
      INCREMENT BY 1
      CACHE 100;
    

    You would then either use that sequence in your INSERT statement

    INSERT INTO name_of_table( primary_key_column, <<other columns>> )
      VALUES( name_of_sequence.nextval, <<other values>> );
    

    Or you can define a trigger that automatically populates the primary key value using the sequence

    CREATE OR REPLACE TRIGGER trigger_name
      BEFORE INSERT ON table_name
      FOR EACH ROW
    BEGIN
      SELECT name_of_sequence.nextval
        INTO :new.primary_key_column
        FROM dual;
    END;
    

    If you are using Oracle 11.1 or later, you can simplify the trigger a bit

    CREATE OR REPLACE TRIGGER trigger_name
      BEFORE INSERT ON table_name
      FOR EACH ROW
    BEGIN
      :new.primary_key_column := name_of_sequence.nextval;
    END;
    

    If you really want to use SYS_GUID

    CREATE TABLE table_name (
      primary_key_column raw(16) default sys_guid() primary key,
      <<other columns>>
    )
    
    0 讨论(0)
  • 2020-11-21 05:57

    Here is complete solution w.r.t exception/error handling for auto increment, this solution is backward compatible and will work on 11g & 12c, specifically if application is in production.

    Please replace 'TABLE_NAME' with your appropriate table name

    --checking if table already exisits
    BEGIN
        EXECUTE IMMEDIATE 'DROP TABLE TABLE_NAME';
        EXCEPTION WHEN OTHERS THEN NULL;
    END;
    /
    
    --creating table
    CREATE TABLE TABLE_NAME (
           ID NUMBER(10) PRIMARY KEY NOT NULL,
           .
           .
           .
    );
    
    --checking if sequence already exists
    BEGIN
        EXECUTE IMMEDIATE 'DROP SEQUENCE TABLE_NAME_SEQ';
        EXCEPTION WHEN OTHERS THEN NULL;
    END;
    
    --creating sequence
    /
    CREATE SEQUENCE TABLE_NAME_SEQ START WITH 1 INCREMENT BY 1 MINVALUE 1 NOMAXVALUE NOCYCLE CACHE 2;
    
    --granting rights as per required user group
    /
    GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE_NAME TO USER_GROUP;
    
    -- creating trigger
    /
    CREATE OR REPLACE TRIGGER TABLE_NAME_TS BEFORE INSERT OR UPDATE ON TABLE_NAME FOR EACH ROW
    BEGIN    
        -- auto increment column
        SELECT TABLE_NAME_SEQ.NextVal INTO :New.ID FROM dual;
    
        -- You can also put some other required default data as per need of your columns, for example
        SELECT SYS_CONTEXT('USERENV', 'SESSIONID') INTO :New.SessionID FROM dual;
        SELECT SYS_CONTEXT('USERENV','SERVER_HOST') INTO :New.HostName FROM dual;
        SELECT SYS_CONTEXT('USERENV','OS_USER') INTO :New.LoginID FROM dual;    
        .
        .
        .
    END;
    /
    
    0 讨论(0)
  • 2020-11-21 05:58

    oracle has sequences AND identity columns in 12c

    http://www.oracle-base.com/articles/12c/identity-columns-in-oracle-12cr1.php#identity-columns

    I found this but not sure what rdb 7 is http://www.oracle.com/technetwork/products/rdb/0307-identity-columns-128126.pdf

    0 讨论(0)
  • 2020-11-21 05:59
    FUNCTION GETUNIQUEID_2 RETURN VARCHAR2
    AS
    v_curr_id NUMBER;
    v_inc NUMBER;
    v_next_val NUMBER;
    pragma autonomous_transaction;
    begin 
    CREATE SEQUENCE sequnce
    START WITH YYMMDD0000000001
    INCREMENT BY 1
    NOCACHE
    select sequence.nextval into v_curr_id from dual;
    if(substr(v_curr_id,0,6)= to_char(sysdate,'yymmdd')) then
    v_next_val := to_number(to_char(SYSDATE+1, 'yymmdd') || '0000000000');
    v_inc := v_next_val - v_curr_id;
    execute immediate ' alter sequence sequence increment by ' || v_inc ;
    select sequence.nextval into v_curr_id from dual;
    execute immediate ' alter sequence sequence increment by 1';
    else
    dbms_output.put_line('exception : file not found');
    end if;
    RETURN 'ID'||v_curr_id;
    END;
    
    0 讨论(0)
提交回复
热议问题