Chapter 14: Updating and Deleting Data

Up until now, we’ve been creating tables, inserting data, and reading it with SELECT. But real databases are alive — prices change, stock gets updated, customers cancel orders, old records get archived… That’s what UPDATE and DELETE are for.

We’ll also cover transactions — the safety net that lets you say: “Either do all these changes perfectly, or undo everything if something goes wrong.”

We’ll use our familiar bookshop database. Let’s quickly remind ourselves of the books table (with a few extra columns for realism):

book_id title author_id price stock is_available last_updated
1 The Room on the Roof 1 250.00 45 1 2026-01-01 10:00:00
2 2 States 3 299.00 120 1 2026-01-02 11:00:00
3 The White Tiger 2 399.00 35 1 2026-01-03 12:00:00
4 Wise and Otherwise 5 199.00 80 1 2026-01-04 13:00:00
5 The Immortals of Meluha 4 349.00 65 1 2026-01-05 14:00:00

1. The UPDATE Statement – Changing Existing Data

UPDATE lets you modify one or more columns in existing rows.

Basic Syntax:

SQL

The WHERE clause is CRITICAL! Without it, ALL rows will be updated — a very common and very dangerous mistake!

Example 1 – Increase price of all books by 10%

SQL

Example 2 – Safely update price of only one book

SQL

Example 3 – Update multiple columns + use calculation

SQL

Example 4 – Update based on condition (multiple rows)

SQL

Example 5 – Update using a subquery (advanced but common)

SQL

Best Practice Rule #1: ALWAYS write and test your WHERE clause first with SELECT!

SQL

2. The DELETE Statement – Removing Rows

DELETE removes entire rows from a table.

Syntax:

SQL

Again — WHERE is CRITICAL! No WHERE = delete everything!

Example 1 – Delete one specific book

SQL

Example 2 – Delete all books with low stock

SQL

Example 3 – Delete books older than 20 years

SQL

Example 4 – Delete using JOIN (delete from one table based on another)

SQL

Best Practice Rule #2: ALWAYS preview with SELECT first!

SQL

Important:

  • DELETE removes rows but keeps the table structure (columns, indexes, constraints)
  • If you want to delete all rows but keep structure faster, use TRUNCATE TABLE (from Chapter 4) — but it cannot be rolled back in most databases.

3. Transactions – The Safety Net (BEGIN, COMMIT, ROLLBACK)

A transaction is a group of SQL statements that are treated as one single unit: All succeed, or none succeed.

Basic commands:

  • BEGIN or START TRANSACTION → start a transaction
  • COMMIT → save all changes permanently
  • ROLLBACK → undo everything since BEGIN

Real-life example – Bulk price update with safety:

SQL

What happens if power fails or error occurs mid-transaction? Most databases automatically ROLLBACK incomplete transactions.

Important in production:

SQL

ACID Properties (why transactions matter):

  • Atomic: All or nothing
  • Consistent: Rules (constraints) always respected
  • Isolated: Other users don’t see half-done changes
  • Durable: Once COMMIT, changes survive crashes

Full Realistic Workflow Example

SQL

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *