Changing data and structure
INSERT, UPDATE, DELETE
Goal: Add, change and remove rows safely · 10 min
- INSERT adds rows:
INSERT INTO customers (id, name, city) VALUES (6, 'Fay', 'Cologne'); - UPDATE changes rows:
UPDATE products SET price = price * 1.1 WHERE category = 'furniture'; - DELETE removes rows:
DELETE FROM orders WHERE id = 5;
The golden rule: UPDATE and DELETE without WHERE affect every row. Run the same WHERE as a SELECT first to see which rows will change. In production, wrap changes in a transaction (BEGIN; … COMMIT; or ROLLBACK;) so a mistake can be undone before it becomes permanent.
Worked example: A safe price increase
- Preview with SELECT using the same WHERE.
- UPDATE furniture by 10%.
- Check the new prices.
UPDATE products SET price = price * 1.1 WHERE category = 'furniture';
SELECT name, ROUND(price, 2) FROM products WHERE category = 'furniture' ORDER BY id;Output:
Desk | 330.0
Chair | 165.0
Lamp | 44.0