0

In mySQL i am able to do

INSERT INTO table_name (column_name1, column_name2) VALUES('John', 'Doe);

As you can see I do not have to mention the ID, how would I do this in postgreSQL.

Thank you

1
  • 1
    Assuming ID is serial (i.e. PostgreSQL equivalent of auto-increment), you can return the just-generated value using the RETURNING statement. Jun 4 '13 at 18:20
6

Approach #1: You can declare your ID column as Serial In this case it will create an implicit sequence for your column.

Example :

CREATE TABLE MyTable
(
   ID     serial   NOT NULL,
   column1 type,
   column2 type
}

Approach #2: Or you can manually define a sequence and then assign its next value as Default value for the column.

CREATE SEQUENCE my_sequence START 1;

CREATE TABLE MyTable
       (
       ID     integer   DEFAULT nextval('my_sequence'::regclass) NOT NULL,
       column1 type,
       column2 type
       }
1
  • those approaches don't work me and try to insert duplicate value :( Dec 28 '20 at 13:01
2

This is not because of mysql that happens. You can make such this kind of query because you have set id as an auto_increment column

You can actually do the same thing in postgreSQL by using the serial pseudo data type instead

Example of primary column serial

id   serial PRIMARY KEY,

Not the answer you're looking for? Browse other questions tagged or ask your own question.