Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials MySQL UPDATE & DELETE
MySQL Intermediate FREE

UPDATE & DELETE

Lesson 2 of 16 Intermediate Interactive

UPDATE modifies rows; DELETE removes rows. Always use WHERE!

TRUNCATE

Removes all rows faster than DELETE. Resets auto-increment.

Transactions

Use START TRANSACTION, COMMIT, ROLLBACK for atomicity.

Syntax

MYSQL
UPDATE users SET email = "new@mail.com" WHERE id = 1;
UPDATE products SET price = price * 1.10 WHERE category = "Electronics";
DELETE FROM users WHERE status = "inactive";
TRUNCATE TABLE logs;
UPDATE and DELETE
SQL
-- Update single row
UPDATE users
SET city = "Multan"
WHERE id = 1;

-- Update multiple columns
UPDATE users
SET name = "Alice Khan", city = "Lahore"
WHERE email = "alice@example.com";

-- Update with condition
UPDATE users
SET status = "active"
WHERE age >= 18;

-- Delete specific row
DELETE FROM users
WHERE id = 5;

-- Delete older than 2024
DELETE FROM users
WHERE created_at < "2024-01-01";

-- Verify no orphans
SELECT COUNT(*) FROM users;

Practice

1
Exercise

50% off all Clearance products.

Answer
UPDATE products SET price = price * 0.50 WHERE category = "Clearance";

Quick Quiz

1

UPDATE without WHERE?

All rows are updated.

Interview Questions

Ensures atomicity � all changes succeed or none do, preventing inconsistent states.