INSERT data and RETURING values

suggest change

If you are inserting data into a table with an auto increment column and if you want to get the value of the auto increment column.

Say you have a table called my_table:

CREATE TABLE my_table
(
id serial NOT NULL, -- serial data type is auto incrementing four-byte integer
name character varying,
contact_number integer,
CONSTRAINT my_table_pkey PRIMARY KEY (id)
);

If you want to insert data into my_table and get the id of that row:

INSERT INTO my_table(name, contact_number) VALUES ( 'USER', 8542621) RETURNING id;

Above query will return the id of the row where the new record was inserted.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents