Sub topic 2
When the Rows to Change Depend on Another Table
This chapter assumes you know basic SELECT, WHERE, and INNER JOIN syntax, single-table UPDATE and DELETE, table aliases, and primary and foreign keys. The examples use PostgreSQL.
A single-table modification can choose rows from the target table alone:
UPDATE customers
SET status = 'inactive'
WHERE status = 'closed';
DELETE FROM orders
WHERE status = 'cancelled';Sometimes the condition depends on another table. For example, you might cancel orders belonging to closed customers or update product prices from a staging table. The second table supplies information that helps select or change rows in the target table.
The target table and the lookup table
A modifying statement has one target table. PostgreSQL changes rows in that table. Other tables can provide matching data, but they are not themselves modified.
| Goal | PostgreSQL form | Target table |
|---|---|---|
| Set values from another table | `UPDATE ... FROM` | The table after `UPDATE` |
| Delete rows matched through another table | `DELETE ... USING` | The table after `DELETE FROM` |
| Test whether a related row exists | `WHERE EXISTS` | The table being modified |
The relationship normally uses a foreign key. An order has a customer_id, so it can be matched with customers.id.
Introducing `UPDATE ... FROM`
PostgreSQL uses FROM to add tables to an update:
UPDATE orders AS o
SET status = 'cancelled'
FROM customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed'
AND o.status = 'open';Read this statement in two parts:
ois the target table. Only rows inorderscan change.csupplies related rows used by theWHEREclause.
The first condition, o.customer_id = c.id, is the join condition. The second condition selects closed customers. The final condition prevents already-cancelled or otherwise non-open orders from changing.
UPDATE ... FROM can also copy a value from the related table:
UPDATE products AS p
SET price = u.new_price
FROM price_updates AS u
WHERE u.product_id = p.id;Here, p.price receives a value from u.new_price. The FROM table is a source of values as well as a filter.
Introducing `DELETE ... USING`
PostgreSQL uses USING for a related table in a delete:
DELETE FROM orders AS o
USING customers AS c
WHERE o.customer_id = c.id
AND c.status = 'closed';The statement deletes matching rows from orders. It does not delete rows from customers. USING behaves like adding a table for a join, while the DELETE FROM table remains the only deletion target.
The two forms share the same basic model:
- Name the table that will change.
- Give each table a short, clear alias.
- Write the join condition.
- Add filters for the exact rows intended.
- Check whether one target row can match several source rows.
If the related table has no matching row, an inner-style FROM or USING match does not select the target row. Use EXISTS or NOT EXISTS when you need to express existence or nonexistence directly.
Exercise
Which rows can the following statement modify: UPDATE orders AS o SET status = 'review' FROM customers AS c WHERE o.customer_id = c.id AND c.status = 'active';?
Show answer
Only orders whose customer_id matches an active customer. The statement can modify orders rows, but never customers rows.