Module 8: Changing data

Insert, update and delete rows safely, return what changed, and upsert with ON CONFLICT.

  1. INSERT: one row, many rows, from a SELECT, with RETURNING: INSERT INTO t (cols) VALUES (…), (…) or INSERT INTO t (cols) SELECT …; name the columns, and add RETURNING to get the new rows back.
  2. UPDATE and DELETE: UPDATE t SET col = … WHERE … and DELETE FROM t WHERE …: always WHERE, check with SELECT first, delete children before parents.
  3. Upsert with ON CONFLICT, and MERGE: INSERT … ON CONFLICT (key) DO NOTHING | DO UPDATE SET col = EXCLUDED.col is an atomic upsert; MERGE applies a whole source table.

Ends with a 12-question checkpoint (80% passes) and a project: Close the day for the food app.