Sub topic 13
Updating and Deleting Based on Other Tables
This chapter assumes you can write SELECT queries with WHERE and JOIN, understand primary and foreign keys, and know the basic forms of UPDATE, DELETE, and transactions.
An UPDATE or DELETE usually uses values from the target table. Sometimes the decision depends on a related table. For example, you might mark every order from a particular customer as high priority. A join lets the data change use that relationship.
Updating with another table
The following statement updates orders belonging to Ada. The orders.customer_id foreign key matches customers.id.
UPDATE orders
SET status = 'vip'
FROM customers
WHERE orders.customer_id = customers.id
AND customers.email = '[email protected]';The target table is orders. The FROM clause makes customers available for filtering. The WHERE clause contains both the relationship and the condition that identifies Ada.
PostgreSQL and SQL Server support this UPDATE ... FROM form, with some syntax differences between them. MySQL uses UPDATE ... JOIN instead:
UPDATE orders
JOIN customers
ON orders.customer_id = customers.id
SET orders.status = 'vip'
WHERE customers.email = '[email protected]';Before changing data, preview the exact rows with a SELECT that uses the same join and conditions:
SELECT orders.id, orders.status, customers.email
FROM orders
JOIN customers
ON orders.customer_id = customers.id
WHERE customers.email = '[email protected]';The preview helps you check both the relationship and the number of affected rows.
Deleting based on another table
Suppose you want to delete orders belonging to inactive customers. The exact syntax differs by database system, so check the dialect before running it.
In PostgreSQL, you can use USING:
DELETE FROM orders
USING customers
WHERE orders.customer_id = customers.id
AND customers.status = 'inactive';In MySQL, use a joined delete:
DELETE orders
FROM orders
JOIN customers
ON orders.customer_id = customers.id
WHERE customers.status = 'inactive';Preview the rows first:
SELECT orders.id, orders.customer_id
FROM orders
JOIN customers
ON orders.customer_id = customers.id
WHERE customers.status = 'inactive';| Database | Common related-table syntax |
|---|---|
| PostgreSQL | `UPDATE ... FROM`, `DELETE ... USING` |
| MySQL | `UPDATE ... JOIN`, joined `DELETE` |
| SQL Server | `UPDATE ... FROM`, joined `DELETE` |
For a destructive operation, use a transaction when supported:
BEGIN;
SELECT orders.id
FROM orders
JOIN customers
ON orders.customer_id = customers.id
WHERE customers.status = 'inactive';
DELETE FROM orders
USING customers
WHERE orders.customer_id = customers.id
AND customers.status = 'inactive';
-- Inspect other results, then choose one:
ROLLBACK;
-- COMMIT;ROLLBACK undoes the deletion. Replace it with COMMIT only after checking the result.
Exercise
Which two conditions should a related-table update usually contain?
Show answer
It should contain a join condition linking the target table to the related table, plus a filter that identifies the intended related rows.