INSERT, UPDATE and DELETE
So far every lesson only read data; the tables never changed. This lesson changes data: adding a row, changing a few cells, removing a few rows.
Every example on this page starts from a fresh copy of the shop's data, so what one example changes is gone in the next. Change things freely. After a change, SELECT again in the same example to see what it did.
INSERT: adding a row
The shop has a new customer. INSERT INTO table (column, column, …) VALUES (value, value, …) puts one row into the table, each value matching a column:
1INSERT INTO customers (id, name, city, joined, email)2VALUES (11, 'Kai Weber', 'Madrid', '2025-10-28', '[email protected]');3 4SELECT * FROM customers WHERE id >= 10;
| id | name | city | joined | |
|---|---|---|---|---|
| 10 | Julia Costa | Sydney | 2025-09-14 | [email protected] |
| 11 | Kai Weber | Madrid | 2025-10-28 | [email protected] |
A statement that changes data has no result table; the page shows Query OK, 1 row affected: it worked, and 1 row was changed. The SELECT after it shows the new customer is in the table.
You can insert several rows at once, each in its own parentheses, separated by commas:
1INSERT INTO products (id, name, category, price, stock)2VALUES (11, 'Sticky notes', 'Stationery', 3.50, 150),3 (12, 'Thermos', 'Kitchen', 29.00, 30);4 5SELECT * FROM products WHERE id > 10;
| id | name | category | price | stock |
|---|---|---|---|---|
| 11 | Sticky notes | Stationery | 3.50 | 150 |
| 12 | Thermos | Kitchen | 29.00 | 30 |
Columns you leave out
You can list only some of the columns. The ones you leave out get NULL (unless the table gave them a default value; lesson 19 covers that):
1INSERT INTO customers (id, name)2VALUES (11, 'Kai Weber');3 4SELECT * FROM customers WHERE id = 11;
| id | name | city | joined | |
|---|---|---|---|---|
| 11 | Kai Weber | NULL | NULL | NULL |
Tip: You can leave out the column names too: INSERT INTO customers VALUES (…) fills the columns in the table's order. But the day the table gets another column, or you remember the order wrong, values land in the wrong place. Naming the columns is safer.
When a row won't go in
Tables have rules. id is the primary key, so no two rows may share one. Product 3 already exists; insert another product 3:
1INSERT INTO products (id, name, category, price, stock)2VALUES (3, 'Umbrella', 'Bags', 35.00, 20);
ERROR 1062 (23000): Duplicate entry '3' for key 'products.PRIMARY'
ERROR 1062 says the value 3 is a duplicate. In the same way, the name column must not be empty (NOT NULL), and putting NULL there gives ERROR 1048. A statement that fails inserts nothing at all. These rules are set when a table is created; lesson 19, where you create your own tables, goes into them.
UPDATE: changing data
UPDATE table SET column = new value WHERE condition changes the rows that match. The notebook's price goes up to 5:
1UPDATE products2SET price = 5.003WHERE id = 1;4 5SELECT id, name, price FROM products WHERE id = 1;
| id | name | price |
|---|---|---|
| 1 | Notebook | 5.00 |
The new value can be worked out from the old one, and you can change several columns at once, separated by commas. New stationery has arrived: 50 more of each, and 5% off:
1UPDATE products2SET stock = stock + 50,3 price = price * 0.954WHERE category = 'Stationery';5 6SELECT name, price, stock FROM products WHERE category = 'Stationery';
| name | price | stock |
|---|---|---|
| Notebook | 4.28 | 170 |
| Pens (10) | 5.70 | 130 |
price * 0.95 comes out as 4.275, but the price column keeps only two decimal places, so 4.28 is what gets stored.
How many rows will the page say were changed?
1UPDATE products2SET stock = stock - 13WHERE category = 'Electronics';
Only the rows WHERE keeps are changed. There are 4 electronics products (headphones, desk lamp, USB cable, phone case), so 4 rows change.
DELETE: removing data
DELETE FROM table WHERE condition removes the rows that match. The desk lamp is out of stock and has never sold, so take it off the shop:
1DELETE FROM products2WHERE stock = 0;3 4SELECT id, name, stock FROM products;
| id | name | stock |
|---|---|---|
| 1 | Notebook | 120 |
| 2 | Pens (10) | 80 |
| 3 | Backpack | 15 |
| 4 | Water bottle | 40 |
| 5 | Headphones | 10 |
| 7 | USB cable | 200 |
| 8 | Coffee mug | 60 |
| 9 | Phone case | 35 |
| 10 | Tote bag | 25 |
But if a row is still used by another table, the database won't let you remove it. Product 1, the notebook, appears in several orders' items:
1DELETE FROM products2WHERE id = 1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`town`.`order_items`, CONSTRAINT `order_items_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`))
ERROR 1451: rows in order_items still point to it, and without the product those order items would no longer say what was bought. This is a foreign key; lesson 19 covers it. To remove the product, you'd first have to remove the rows that point to it.
Forgetting WHERE
WHERE is optional in UPDATE and DELETE, and leaving it out means "every row". You meant to set just one product's price to 0:
1UPDATE products2SET price = 0;3 4SELECT name, price FROM products;
| name | price |
|---|---|
| Notebook | 0.00 |
| Pens (10) | 0.00 |
| Backpack | 0.00 |
| Water bottle | 0.00 |
| Headphones | 0.00 |
| Desk lamp | 0.00 |
| USB cable | 0.00 |
| Coffee mug | 0.00 |
| Phone case | 0.00 |
| Tote bag | 0.00 |
10 rows affected: every price in the table is now 0. DELETE FROM products; is the same: it empties the whole table. In a real database there's no undo for this (unless you use a transaction, lesson 20).
SELECT first, then change
A good habit: before changing anything, write a SELECT with the same WHERE to see which rows it will touch. Once that looks right, swap SELECT … FROM for UPDATE … SET or DELETE FROM:
1SELECT id, status2FROM orders3WHERE customer_id = 1 AND status = 'pending';4 5UPDATE orders6SET status = 'shipped'7WHERE customer_id = 1 AND status = 'pending';8 9SELECT id, status FROM orders WHERE customer_id = 1;
| id | status |
|---|---|
| 9 | pending |
| id | status |
|---|---|
| 1 | shipped |
| 3 | shipped |
| 9 | shipped |
The first query finds only order 9, so that's the only one changed.
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it. The last result table is what's checked, so SELECT again after your change.
A new customer
✓Insert a new customer into customers: id 11, name "Kai Weber", city "Madrid", joined 2025-10-30, no email. Then get id, name, city and email of customers 9 and up, ordered by id.
| id | name | city | |
|---|---|---|---|
| 9 | Ivan Petrov | Berlin | NULL |
| 10 | Julia Costa | Sydney | [email protected] |
| 11 | Kai Weber | Madrid | NULL |
1SELECT id, name, city, email2FROM customers3WHERE id >= 94ORDER BY id;
Kitchen sale
✓Take 10% off every kitchen product (price times 0.9). Then get the name and price of the kitchen products, ordered by id.
| name | price |
|---|---|
| Water bottle | 10.80 |
| Coffee mug | 8.91 |
1SELECT name, price2FROM products3WHERE category = 'Kitchen'4ORDER BY id;
Remove the cancelled order
✓Order 4 was cancelled and should be removed for good. The code below gives an error: first remove its items from order_items, then the order. Finally, get the id and status of customer 4's orders, ordered by id.
| id | status |
|---|---|
| 12 | pending |
1DELETE FROM orders2WHERE id = 4;3 4SELECT id, status5FROM orders6WHERE customer_id = 47ORDER BY id;
What you learned
INSERT INTO table (column, …) VALUES (…), (…);adds one or more rows; columns you leave out getNULL.UPDATE table SET column = value, column = value WHERE …;changes rows; the new value can use the old one.DELETE FROM table WHERE …;removes rows; a row another table still points to can't be removed (ERROR 1451).- Without
WHERE, every row is changed (or removed). - Before a change, run a
SELECTwith the sameWHERE; after it,SELECTto see the result. - A duplicate primary key is
ERROR 1062;NULLin aNOT NULLcolumn isERROR 1048.