skip to Main Content

I am trying to auto increment a table with a single column which is a primary key, eg:

CREATE TABLE my_table (id PRIMARY KEY NOT NULL);

I would usually just not include the primary key column in the insert statment in order to autoincrement it, but this doesn’t seem possible in the case where there is only a single column. The below results in "syntax error at or near ")""

INSERT INTO my_table () VALUES ();

2

Answers


  1. You can set value of id = Auto Value when insert
    Ex:

    CREATE TABLE my_table (id Serial PRIMARY KEY NOT NULL);
    INSERT INTO my_table(id) VALUES( nextval('my_table_id_seq'::regclass));
    
    Login or Signup to reply.
  2. Define your column as an identity then on insert specify the keyword DEFAULT.

    create table dumb_id ( id   integer  generated always as identity primary key); 
    insert into dumb_id (id) values (default);
    

    Note: You do not use quotes around default. Demo here
    But what is the point of such a table?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search