INSERT data and RETURNING values

suggest change

If you are inserting into a table with a serial column and you want that generated value back:

Say you have a table called my_table:

CREATE TABLE my_table
(
id serial NOT NULL, -- serial is an integer column backed by a sequence
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;

The query above returns the id of the inserted row. RETURNING can list other columns, or *.

Feedback about page:

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



Table Of Contents