UPDATE Basics
Result:
| id | name | price | stock |
|---|---|---|---|
| 4 | Desk | 273.90 | 15 |
| 5 | Chair | 141.90 | 30 |
| 8 | Lamp | 49.50 | 5 |
Always check the same condition with SELECT first. A missing WHERE changes every row.
UPDATE with a JOIN
MySQL lets you update one table using columns from another with UPDATE ... JOIN ... SET. Add a loyalty column and give 5 points per non-cancelled order:
Result:
| id | name | points |
|---|---|---|
| 1 | Asha Rao | 15 |
| 2 | Ben Carter | 10 |
| 3 | Chen Wei | 10 |
| 4 | Dina Cohen | 0 |
| 5 | Eli Brown | 5 |
| 6 | Fatima Khan | 5 |
The joined table is a derived table that counts orders. Customers without orders are not in the join, so they keep their default of 0.
UPDATE with ORDER BY and LIMIT
MySQL allows ORDER BY and LIMIT in a single-table UPDATE or DELETE, which is handy for batches:
Result:
| id | name | price | stock |
|---|---|---|---|
| 6 | Notebook | 3.50 | 600 |
| 7 | Pen Pack | 5.00 | 400 |
| 8 | Lamp | 45.00 | 0 |
Only the two cheapest products were touched. Deleting or updating in batches of 1,000 rows keeps transactions short on big tables.
DELETE Basics
Result:
| products_left |
|---|
| 8 |
DELETE with a JOIN
Delete rows of one table based on another. For example remove all items of cancelled orders:
Result:
(0 rows)
Result:
| items_left |
|---|
| 15 |
DELETE oi FROM ... names which table loses rows. You can even delete from two tables in one statement:
Tables linked by a foreign key can make a two-table delete fail, because InnoDB checks the key row by row (error 1451). It works for tables without that key, or when you delete child rows first:
Result:
| carts_left | items_left |
|---|---|
| 1 | 1 |
Deleting from a Table Using Itself
MySQL does not allow a subquery on the same table you modify (error 1093). Wrap it in a derived table:
ERROR 1093: You can't specify target table 'signups' for update in FROM clause
Result:
| id | |
|---|---|
| 1 | a |
| 3 | b |
TRUNCATE vs DELETE
| DELETE | TRUNCATE | |
|---|---|---|
| Condition | Can use WHERE | Whole table only |
| Speed | Row by row, slower | Very fast |
| AUTO_INCREMENT | Keeps counting | Restarts at 1 |
| Rollback | Yes, inside a transaction | No, it commits (DDL) |
| Foreign keys | Allowed (rows checked) | Refused if another table references it |
Result:
| id | msg |
|---|---|
| 4 | d |
Result:
| id | msg |
|---|---|
| 1 | d |
ERROR 1701: Cannot truncate a table referenced in a foreign key constraint ('shop'.'orders', CONSTRAINT 'orders_ibfk_1')
Safe Updates Mode
The mysql client has an option --safe-updates (also --i-am-a-dummy) that refuses an UPDATE or DELETE without a key condition or LIMIT. MySQL Workbench enables it by default. You can turn it on for a session:
ERROR 1175: You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.
Result:
| id | price |
|---|---|
| 1 | 900.00 |
Rows Matched vs Rows Changed
MySQL reports both: rows matched by WHERE and rows changed. If the new value equals the old one, the row matches but does not change:
Result:
| rows_changed |
|---|
| 0 |
Zero rows changed, although one matched. Application code should not treat "0 rows affected" as "row not found".
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
No WHERE on UPDATE or DELETE | Every row changes | SELECT first, use sql_safe_updates and transactions |
Subquery on the same table in DELETE | Error 1093 | Wrap in a derived table or use DELETE ... JOIN |
TRUNCATE inside a transaction | Cannot be undone | Use DELETE when you need rollback |
| Reading 0 rows affected as an error | False alarms | Remember unchanged rows count as 0 |
| Deleting millions of rows at once | Long locks, huge undo log | Delete in batches with LIMIT |
Practice
- Give each customer a
last_orderdate column filled with their latest order date (UPDATE with JOIN). - Delete order items of orders older than 2025-02-01 using a JOIN delete.
- Remove duplicate emails from a table, keeping the lowest id.
- Turn on
sql_safe_updatesand try an update without a key.
Conclusion
MySQL extends UPDATE and DELETE with joins, ORDER BY and LIMIT, refuses same-table subqueries, and offers sql_safe_updates. Check with SELECT first, use transactions, and do large changes in batches.

