MySQL DELETE
DELETE removes rows from a table:
DELETE FROM table_name WHERE condition;The WHERE clause chooses the rows. As with UPDATE, leaving it out affects every row, so the habit of writing (and checking) the WHERE first is worth building now.
SELECT COUNT(*) AS lines_before FROM order_details WHERE order_id = 22;
DELETE FROM order_details WHERE order_id = 22 AND product_id = 20;
SELECT ROW_COUNT() AS deleted;
SELECT * FROM order_details WHERE order_id = 22;+--------------+
| lines_before |
+--------------+
| 2 |
+--------------+
+---------+
| deleted |
+---------+
| 1 |
+---------+
+----------+------------+----------+------------+----------+
| order_id | product_id | quantity | unit_price | discount |
+----------+------------+----------+------------+----------+
| 22 | 2 | 2 | 6.75 | 0.00 |
+----------+------------+----------+------------+----------+
Deleting several rows
Any condition works. Here we remove all cancelled orders. Their lines must go first, because order_details has a foreign key pointing at orders:
DELETE FROM order_details
WHERE order_id IN (SELECT order_id FROM orders WHERE status = 'cancelled');
DELETE FROM orders WHERE status = 'cancelled';
SELECT ROW_COUNT() AS orders_deleted;
SELECT status, COUNT(*) AS orders FROM orders GROUP BY status ORDER BY status;+----------------+
| orders_deleted |
+----------------+
| 2 |
+----------------+
+------------+--------+
| status | orders |
+------------+--------+
| delivered | 15 |
| processing | 3 |
| shipped | 2 |
+------------+--------+
Foreign keys protect related rows
If we try to delete a customer who has orders, the foreign key from orders.customer_id stops us. Deleting a customer without orders works fine:
DELETE FROM customers WHERE customer_id = 13; -- Mia, no orders
SELECT COUNT(*) AS customers_left FROM customers;
DELETE FROM customers WHERE customer_id = 1; -- Ana, 3 orders+----------------+
| customers_left |
+----------------+
| 14 |
+----------------+
ERROR 1451 (23000) at line 3: Cannot delete or update a parent row: a foreign key constraint fails (`shop`.`orders`, CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`))
The error message names the constraint (orders_ibfk_1, a name MySQL generated automatically) and the columns involved. In the Keys lesson you'll see ON DELETE CASCADE, which deletes the child rows automatically instead.
Deleting everything: DELETE vs TRUNCATE
DELETE FROM t; without WHERE removes all rows but keeps the table. TRUNCATE TABLE t; does the same much faster by recreating the table. The differences matter:
DELETE FROM t |
TRUNCATE TABLE t |
|
|---|---|---|
Can have WHERE |
yes | no |
| Speed on big tables | row by row, slow | instant |
AUTO_INCREMENT counter |
kept | reset to 1 |
| Can be rolled back in a transaction | yes | no (it's DDL) |
Fires DELETE triggers |
yes | no |
| Allowed when other tables reference it by FK | yes (if no rows are referenced) | no |
CREATE TABLE notes (id INT AUTO_INCREMENT PRIMARY KEY, txt VARCHAR(20));
INSERT INTO notes (txt) VALUES ('a'), ('b');
DELETE FROM notes;
INSERT INTO notes (txt) VALUES ('after delete');
TRUNCATE TABLE notes;
INSERT INTO notes (txt) VALUES ('after truncate');
SELECT * FROM notes;+----+----------------+
| id | txt |
+----+----------------+
| 1 | after truncate |
+----+----------------+
After DELETE, the next id would have been 3; after TRUNCATE it starts again at 1.
Deleting with a join
Like UPDATE, MySQL's DELETE can join tables. You list the table(s) to delete from before FROM:
DELETE d
FROM order_details AS d
JOIN orders AS o ON o.order_id = d.order_id
WHERE o.status = 'cancelled';Tip: On a real database, take a backup or wrap the statement in a transaction (
START TRANSACTION; DELETE ...; SELECT ...;thenCOMMITorROLLBACK). See the Transactions lesson.
Test yourself
What does DELETE FROM customers; do?
- Drops the table
- Deletes all rows but keeps the table
- Deletes the first row
- Nothing without WHERE
Which is true of TRUNCATE TABLE compared with DELETE?
- It accepts a WHERE clause
- It resets the AUTO_INCREMENT counter
- It fires DELETE triggers
- It is slower
Why did deleting customer 1 fail?
- Customer 1 does not exist
- Orders still reference the customer through a foreign key
- DELETE needs LIMIT
- Primary keys can't be deleted