Aral Code

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

SQL The price of corned beef goes up
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

SQL Customer updates her info
UPDATE customers
SET phone = '09998887777', barangay = 'Poblacion'
WHERE id = 3;

SELECT * FROM customers WHERE id = 3;

Calculations in UPDATE

SQL 5 more cans of sardines arrive; then 3 are sold
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;
SQL All drinks go up by 10%
UPDATE products
SET price = ROUND(price * 1.10, 2)
WHERE category = 'Drinks';

SELECT name, price FROM products WHERE category = 'Drinks';

Marking utang as paid

SQL Maria pays her utang
UPDATE orders
SET status = 'paid'
WHERE customer_id = 1 AND status = 'utang';

SELECT * FROM orders WHERE customer_id = 1;

The danger: UPDATE without WHERE

SQL Oops! Every price becomes ₱1
UPDATE products SET price = 1;

SELECT name, price FROM products;

DELETE

DELETE FROM table_name
WHERE condition;
SQL Stop selling chips
DELETE FROM products
WHERE id = 15;

SELECT COUNT(*) AS products_left FROM products;
SQL Delete all orders from before November 10
DELETE FROM orders
WHERE order_date < '2026-11-10';

SELECT * FROM orders;

Safe workflow

  1. Write a SELECT with the WHERE you plan to use: SELECT * FROM orders WHERE order_date < '2026-11-10';
  2. Check that the rows shown are exactly the ones you mean.
  3. Replace SELECT * with DELETE (or UPDATE ... SET ...), keeping the WHERE unchanged.
  4. 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:

SQL Rolling back a mistake
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.

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. 🙂

1. What does UPDATE products SET price = 10; do?
2. Which removes the customer with id 6?
3. Before running an UPDATE or DELETE, what should you do?
4. What does ROLLBACK do?

By Sir Reyel · Last updated 2026-12-04