Search code examples
databaseoracle-databaseauto-increment

Error while creating database table with auto increment


I want to make auto increment here with the table creation but it gives an error. The database is an oracle database. The SQL is shown below.

CREATE TABLE Continents 
    ( 
      ConId   INT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) PRIMARY KEY, 
      Continent VARCHAR(25),
    );

Solution

  • Just remove this part

    (START WITH 1, INCREMENT BY 1)
    

    Use this syntax.

    CREATE TABLE Continents 
        ( 
          ConId   INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, 
          Continent VARCHAR(25),
        );
    

    The value of ConId will start at 1 (one) and always increment by one.

    Refer to this db<>fiddle
    Also refer to Oracle documentation1

    The following statement creates a table t1 with an identity column id. The sequence generator will always assign increasing integer values to id, starting with 1.
    CREATE TABLE t1 (id NUMBER GENERATED AS IDENTITY);

    1SQL Language Reference (Oracle 21c) - CREATE TABLE