Memra

INSERT (and RETURNING)

Add rows — and get back what the database generated.

Add rows

INSERT adds rows. Name the columns, then give VALUES:

INSERT INTO authors (id, name, country, born)
VALUES (6, 'Nadia Frost', 'Canada', 1979);

Postgres has a gem most databases lack: RETURNING hands back columns from the rows you just wrote — no second query needed:

INSERT INTO authors (id, name, country, born)
VALUES (6, 'Nadia Frost', 'Canada', 1979)
RETURNING id, name;

(Every exercise here runs in a transaction that's rolled back afterward, so you can write freely — the data resets each run.)

NORMAL ~/memra/learn/postgresql/insert-returning utf-8 LF