17. UPDATE and DELETE: changing and removing rows
Prices go up, customers move, cancelled orders go. This chapter changes rows that are already there, and removes them, without hurting the rest.
Changing a row with UPDATE
UPDATE changes rows already in a table. After SET comes the column and its new value; after WHERE, which rows to change. The Orange Juice has run out and 60 bottles just came in:
UPDATE product SET stock = 60 WHERE id = 3;
SELECT id, name, stock FROM product WHERE id = 3;| id | name | stock |
|---|---|---|
| 3 | Orange Juice | 60 |
1 row changed, and only that one: every other product keeps its stock. WHERE here works exactly as in SELECT (chapters 3 and 4).
Several columns, and values worked out
Several columns change in one SET, separated by commas. Customer 3 has moved to Chiang Rai and got a phone:
UPDATE customer SET city = 'Chiang Rai', phone = '089-000-1234' WHERE id = 3;
SELECT * FROM customer WHERE id = 3;| id | name | city | phone | joined |
|---|---|---|---|---|
| 3 | Anan Wongsa | Chiang Rai | 089-000-1234 | 2025-04-20 |
The new value can be worked out from the old one. Drinks go up by 10%: each row's price becomes its own price times 1.1, rounded to the satang:
UPDATE product SET price = round(price * 1.1, 2) WHERE category_id = 1;
SELECT id, name, price FROM product WHERE category_id = 1;| id | name | price |
|---|---|---|
| 1 | Coffee | 60.5 |
| 2 | Green Tea | 49.5 |
| 3 | Orange Juice | 44.0 |
Three drinks, three rows changed. On the right of = the column means the value before the change, so stock = stock + 20 adds 20 to whatever each row had.
Why round? 55 * 1.1 is not exactly 60.5 to a computer: decimals are kept in binary, and 55 * 1.1 comes out a hair above. Round money to 2 places when you work it out.
Look before you change: forgetting WHERE
An UPDATE or DELETE without WHERE changes every row of the table, and the database doesn't ask "are you sure?". To see it without hurting the shop, we make a copy of the product table first (CREATE TABLE ... AS SELECT makes a table from a result; chapter 20 tells more):
CREATE TABLE product_copy AS SELECT * FROM product;
UPDATE product_copy SET price = 0;SELECT name, price FROM product_copy LIMIT 4;| name | price |
|---|---|
| Coffee | 0.0 |
| Green Tea | 0.0 |
| Orange Juice | 0.0 |
| Croissant | 0.0 |
One forgotten line, and all 12 products are free. On the real table this is a very bad day.
Before an UPDATE or a DELETE, run a SELECT with the same WHERE first. It shows exactly the rows that will change. When those are the rows you meant, swap the SELECT ... FROM for UPDATE ... SET (or DELETE FROM) and keep the WHERE as it is.
The habit in practice: products with nothing in stock should get 10 each. Look first:
SELECT id, name, stock FROM product WHERE stock = 0;| id | name | stock |
|---|---|---|
| 12 | Highlighter | 0 |
One product (the Orange Juice already has 60 now). The same WHERE, now changing:
UPDATE product SET stock = 10 WHERE stock = 0;changes() tells how many rows the last INSERT, UPDATE or DELETE changed, handy in a program that has no "1 row changed" to show:
SELECT changes();| changes() |
|---|
| 1 |
A subquery in SET, and UPDATE ... FROM
The new value can come from another table through a subquery (chapter 13). Order 10 is paid but not sent yet; the shop decides to charge it at today's prices:
UPDATE order_item
SET price = (SELECT price FROM product WHERE product.id = order_item.product_id)
WHERE order_id = 10;
SELECT * FROM order_item WHERE order_id = 10;| order_id | product_id | qty | price |
|---|---|---|---|
| 10 | 2 | 1 | 49.5 |
The subquery runs once for each row being changed, and order_item.product_id is that row's product. The Green Tea is now at its new price.
UPDATE ... FROM joins another table to the one being changed. Order 9 is being shipped, so each product's stock goes down by what the order takes:
SELECT product_id, qty FROM order_item WHERE order_id = 9 ORDER BY product_id;| product_id | qty |
|---|---|
| 1 | 2 |
| 4 | 4 |
| 6 | 2 |
UPDATE product
SET stock = product.stock - item.qty
FROM order_item AS item
WHERE item.product_id = product.id AND item.order_id = 9;
UPDATE orders SET status = 'shipped' WHERE id = 9;SELECT id, name, stock FROM product WHERE id IN (1, 4, 6);| id | name | stock |
|---|---|---|
| 1 | Coffee | 118 |
| 4 | Croissant | 26 |
| 6 | Cheesecake | 6 |
The Coffee went from 120 to 118, the Croissant from 30 to 26, the Cheesecake from 8 to 6.
Write the table being changed by its own name (product), with no AS short name: Phai SQL refuses a short name on the table of an UPDATE or a DELETE. A short name on the table after FROM (order_item AS item) is fine. When the FROM finds several rows for one row being changed, only one of them is used, so join on something that finds one.
Removing rows with DELETE
DELETE FROM removes rows; WHERE says which. Order 14 was cancelled, and the shop wants it gone. Try to delete the order:
DELETE FROM orders WHERE id = 14;The database refuses: order 14 still has lines in order_item that point at it (a foreign key, chapter 22), and deleting it would leave them pointing at nothing. Delete the lines first, then the order:
DELETE FROM order_item WHERE order_id = 14;
DELETE FROM orders WHERE id = 14;
SELECT changes();| changes() |
|---|
| 1 |
The order's 1 line went first, then the order. WHERE can use anything SELECT's WHERE can, a subquery too. The other cancelled order, 5, the same way, by status:
DELETE FROM order_item WHERE order_id IN (SELECT id FROM orders WHERE status = 'cancelled');
DELETE FROM orders WHERE status = 'cancelled';
SELECT count(*) FROM orders;| count(*) |
|---|
| 13 |
13 orders are left of the 15. A deleted row is gone for good: there is no wastebasket. Chapter 19 shows how to try a change and take it back (ROLLBACK).
- Make the Pen (
id11) cost 18 baht. - Give every Snacks product (
category_id3) 10 more in stock, and check how many rows changed withchanges(). - Delete the customers who have never ordered. Look at them with a
SELECTfirst.
Show the answer
UPDATE product SET price = 18 WHERE id = 11;
UPDATE product SET stock = stock + 10 WHERE category_id = 3;
SELECT changes();
SELECT id, name FROM customer WHERE id NOT IN (SELECT customer_id FROM orders);
DELETE FROM customer WHERE id NOT IN (SELECT customer_id FROM orders);| changes() |
|---|
| 3 |
| id | name |
|---|---|
| 9 | Tawan Rungruang |
| 10 | Lalita Meesuk |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made