How to create id with AUTO_INCREMENT on Oracle?

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

    Oracle Database 12c introduced Identity, an auto-incremental (system-generated) column. In the previous database versions (until 11g), you usually implement an Identity by creating a Sequence and a Trigger. From 12c onward, you can create your own Table and define the column that has to be generated as an Identity.

    The following article explains how to use it:

    Identity columns - A new entry in Oracle Database 12c

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

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

    Assuming you mean a column like the SQL Server identity column?

    In Oracle, you use a SEQUENCE to achieve the same functionality. I'll see if I can find a good link and post it here.

    Update: looks like you found it yourself. Here is the link anyway: http://www.techonthenet.com/oracle/sequences.php

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

    In Oracle 12c onward you could do something like,

    CREATE TABLE MAPS
    (
      MAP_ID INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) NOT NULL,
      MAP_NAME VARCHAR(24) NOT NULL,
      UNIQUE (MAP_ID, MAP_NAME)
    );
    

    And in Oracle (Pre 12c).

    -- create table
    CREATE TABLE MAPS
    (
      MAP_ID INTEGER NOT NULL ,
      MAP_NAME VARCHAR(24) NOT NULL,
      UNIQUE (MAP_ID, MAP_NAME)
    );
    
    -- create sequence
    CREATE SEQUENCE MAPS_SEQ;
    
    -- create tigger using the sequence
    CREATE OR REPLACE TRIGGER MAPS_TRG 
    BEFORE INSERT ON MAPS 
    FOR EACH ROW
    WHEN (new.MAP_ID IS NULL)
    BEGIN
      SELECT MAPS_SEQ.NEXTVAL
      INTO   :new.MAP_ID
      FROM   dual;
    END;
    /
    
    0 讨论(0)
  • 2020-11-21 05:52
    FUNCTION UNIQUE2(
     seq IN NUMBER
    ) RETURN VARCHAR2
    AS
     i NUMBER := seq;
     s VARCHAR2(9);
     r NUMBER(2,0);
    BEGIN
      WHILE i > 0 LOOP
        r := MOD( i, 36 );
        i := ( i - r ) / 36;
        IF ( r < 10 ) THEN
          s := TO_CHAR(r) || s;
        ELSE
          s := CHR( 55 + r ) || s;
        END IF;
      END LOOP;
      RETURN 'ID'||LPAD( s, 14, '0' );
    END;
    
    0 讨论(0)
  • 2020-11-21 05:55
      create trigger t1_trigger
      before insert on AUDITLOGS
      for each row
       begin
         select t1_seq.nextval into :new.id from dual;
       end;
    

    only I have to just change the table name (AUDITLOGS) with your table name and new.id with new.column_name

    0 讨论(0)
提交回复
热议问题