Changing Data Lesson 18 of 21
SQL UPDATE and DELETE — Changing and Removing Data
Change existing rows with UPDATE ... SET ... WHERE, remove rows with DELETE, and avoid the classic disaster of forgetting WHERE.
Prices go up, stock gets sold, a customer changes their number, a utang gets paid. To change existing data, use UPDATE. To remove rows, use DELETE. Both are powerful, and both can be dangerous without a WHERE clause.
UPDATE syntax
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
Changing one row
UPDATE products
SET price = 45
WHERE id = 4;
SELECT * FROM products WHERE id = 4;
Use the primary key (id) in the WHERE when you want to change exactly one row. Names can have duplicates; ids can't.
Changing several columns
UPDATE customers
SET phone = '09998887777', barangay = 'Poblacion'
WHERE id = 3;
SELECT * FROM customers WHERE id = 3;
Calculations in UPDATE
UPDATE products SET stock = stock + 5 WHERE id = 3;
UPDATE products SET stock = stock - 3 WHERE id = 3;
SELECT name, stock FROM products WHERE id = 3;
UPDATE products
SET price = ROUND(price * 1.10, 2)
WHERE category = 'Drinks';
SELECT name, price FROM products WHERE category = 'Drinks';
Marking utang as paid
UPDATE orders
SET status = 'paid'
WHERE customer_id = 1 AND status = 'utang';
SELECT * FROM orders WHERE customer_id = 1;
The danger: UPDATE without WHERE
UPDATE products SET price = 1;
SELECT name, price FROM products;
DELETE
DELETE FROM table_name
WHERE condition;
DELETE FROM products
WHERE id = 15;
SELECT COUNT(*) AS products_left FROM products;
DELETE FROM orders
WHERE order_date < '2026-11-10';
SELECT * FROM orders;
Safe workflow
- Write a SELECT with the WHERE you plan to use:
SELECT * FROM orders WHERE order_date < '2026-11-10'; - Check that the rows shown are exactly the ones you mean.
- Replace
SELECT *withDELETE(orUPDATE ... SET ...), keeping the WHERE unchanged. - In real systems, back up the database first, or use a transaction (below).
Transactions: changes you can undo
A transaction groups changes so they either all happen or none do. Until you COMMIT, you can ROLLBACK:
BEGIN TRANSACTION;
DELETE FROM customers; -- oops, no WHERE!
SELECT COUNT(*) AS during FROM customers;
ROLLBACK; -- undo everything since BEGIN
SELECT COUNT(*) AS after_rollback FROM customers;
Banks use transactions for transfers: taking money from one account and adding it to another must both succeed, or neither should happen.
Deleting related data
If you delete an order, its rows in order_items would be left pointing to an order that no longer exists. Delete the items first, or set up the tables with ON DELETE CASCADE so the database removes them automatically. You'll see this in the next lesson.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04