2

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. Commented Jun 4, 2013 at 18:20

2 Answers 2

8

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
       }
Sign up to request clarification or add additional context in comments.

1 Comment

those approaches don't work me and try to insert duplicate value :(
3

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,

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.