Change rows without losing the question

Choose safe INSERT, UPDATE, DELETE and PostgreSQL upsert statements while keeping predicates, defaults, constraints and transaction ownership visible. SQL mutations and schema evolution · Databases.

A one-line row change can silently target every row, reject a required value, or report a result that the caller mistakes for a committed transaction. First name the rows and columns the statement may touch; then decide what the surrounding transaction promises.

Insert a candidate row

INSERT creates rows in a target table. Give a column list when the statement does not intentionally provide every column, so the values stay aligned with their meaning when the schema grows. An omitted column or an explicit DEFAULT keyword receives its declared default; if there is no default, the engine uses its ordinary omitted-column behavior, commonly NULL, and NOT NULL or another constraint may reject the row. An explicitly supplied value, such as status = 'paid', remains that supplied value and is still checked; a default does not overwrite it.

Name the row change and its feedback

Read the steps as text

  1. Orders has a generated key, a required customer, a default status and a positive amount check; one pending and one cancelled row are the concrete inputs.
  2. The candidate insert names customer_id and amount, omits status intentionally, and can use the declared default or explicit DEFAULT; an explicitly supplied status such as paid remains paid.
  3. The keyed update and cancelled-row delete now have concrete conditions; only rows whose condition is TRUE are eligible for their statement.
  4. The statement returns affected-row values while the transaction seam remains separate; PostgreSQL defaults differ for unqualified UPDATE and DELETE.
  5. The mutation path separates named input and constraints, TRUE row predicates, PostgreSQL feedback defaults and transaction completion.

The first frame shows an orders table with a generated key, a required customer_id, a default status, and a positive-amount check. The learner compares a named-column insert with an insert that relies on column order. The candidate row is accepted only when required values, defaults and constraints agree. The scene uses a qualitative table and does not claim a universal identity or default implementation across engines.

INSERT INTO orders (customer_id, amount)
VALUES (42, 25.00)
RETURNING order_id, status;

In the PostgreSQL 18 profile, the returned row includes the generated order_id and the defaulted status because RETURNING evaluates the row actually inserted. RETURNING is a PostgreSQL extension; keep the profile attached when moving the example to another engine.

Target rows before changing them

UPDATE changes the named columns for rows satisfying its condition. DELETE removes rows satisfying its condition; without a WHERE, PostgreSQL deletes every row while leaving the table definition. In a WHERE condition, only TRUE survives. FALSE and UNKNOWN do not identify a row, so a NULL comparison is not a safe substitute for an explicit missingness test owned by the NULL lesson.

The predicate is therefore part of the mutation's safety contract. Start with a SELECT using the same predicate when reviewing a broad change, use a key or another uniqueness guarantee when one row is intended, and make an intentional all-row change visible in the statement and review. The preview is a safety inspection, not a lock or concurrency guarantee: a later mutation can observe a changed database, so its own predicate and the surrounding transaction/isolation contract determine the final effect. UPDATE ... FROM can use another table, but the join must not produce multiple source rows for one target row; otherwise the chosen source row is not predictable in PostgreSQL.

The predicate step keeps one pending order and one cancelled order visible. A correctly keyed UPDATE changes only the pending row; an unqualified DELETE is marked as an all-row operation, not as a one-row repair. A RETURNING result reports the rows that the mutation actually changed or removed. In PostgreSQL, unqualified UPDATE ... RETURNING expressions read post-update values, while unqualified DELETE ... RETURNING expressions read pre-delete values; use explicit OLD or NEW qualification when both sides are needed.

UPDATE orders
SET status = 'paid'
WHERE order_id = 42
RETURNING order_id, status;

DELETE FROM orders
WHERE status = 'cancelled'
RETURNING order_id;

Choose a conflict action and report the result

PostgreSQL INSERT ... ON CONFLICT chooses between DO NOTHING and DO UPDATE for a supported conflict target. DO NOTHING can handle supported unique or exclusion conflicts, but DO UPDATE needs a supported NOT DEFERRABLE unique constraint or unique index as its arbiter; an exclusion constraint is not a DO UPDATE arbiter. The conflict target identifies the constraint or indexed key, and the chosen action determines whether the conflicting row is left alone or updated. It is still a mutation with the target table's constraints and privileges. The optional RETURNING clause returns only rows that were successfully inserted or updated; a conflict path that does nothing returns no row.

Choose the conflict action

Read the steps as text

  1. Devices has one new candidate and one existing device; device_id is a NOT DEFERRABLE unique arbiter and no exclusion arbiter is used for DO UPDATE.
  2. On this conflict, DO NOTHING returns no row; a successful DO UPDATE returns one. An inserted candidate also returns one. Exclusion constraints are not DO UPDATE arbiters.
  3. For the successful insert or update path, the returned row is paired with an explicit transaction wrapper; feedback and completion are separate facts.
  4. The fresh device example transfers conflict-target reasoning and the seam between statement feedback and transaction completion.

The transfer dataset uses devices(device_id, owner_id, label) with a NOT DEFERRABLE unique device_id arbiter. The learner chooses between inserting a new device, updating one known device, ignoring a duplicate, and changing a duplicate through a conflict action; an exclusion constraint is not used as the DO UPDATE arbiter. A final frame places those statements inside or outside an explicit transaction without predicting engine-specific autocommit internals.

Keep transaction ownership visible

A row mutation is a statement-level operation. In PostgreSQL, a statement outside an explicit block runs in a single-statement transaction; BEGIN and COMMIT can group several statements, and ROLLBACK discards the current transaction's updates. The transaction lesson owns atomicity, isolation, savepoints and engine-specific session behavior. This lesson only marks the seam so a returned row is not mistaken for durable visibility.

Recap

  • Named columns, defaults and constraints define whether an inserted candidate row is valid.
  • UPDATE and DELETE change rows selected by a TRUE predicate; missing or broad predicates deserve deliberate review.
  • PostgreSQL RETURNING reports affected rows—new values for unqualified UPDATE, old values for unqualified DELETE—and ON CONFLICT uses DO NOTHING for supported unique/exclusion conflicts or DO UPDATE with a supported NOT DEFERRABLE unique arbiter; transaction boundaries remain a separate owner.
The PostgreSQL profile is deliberately narrow

This packet uses PostgreSQL 18 syntax for RETURNING, ON CONFLICT, UPDATE ... FROM and the statement-versus-explicit-block examples. SQL-standard availability and behavior vary by engine. It does not teach triggers, stored procedures, row-level security, physical plans, lock protocols or retry algorithms; those have separate owners.

For a DSA connection, Queue Fundamentals gives a compact way to trace an ordered mutation list. Next, Schema evolution and object lifecycle changes the table definition rather than one row at a time.