SQL Lesson 10: INSERT, UPDATE and DELETE Safely

📎 This article includes 2 downloadable practice files ↓

⏱ 3 min read

📘 SQL Beginner Course · Lesson 10 of 12

In this article
  1. INSERT: add rows
  2. UPDATE: change rows
  3. DELETE: remove rows
  4. The safety routine
  5. Constraints protect you
  6. Where beginners go wrong
  7. Practice

Reading data can’t hurt anything. Changing it can: one UPDATE without WHERE rewrites every row in the table. This lesson teaches the commands and, more importantly, the habits that keep you safe.

⚠️ Practise on a COPY of shop.db. In real jobs, never run changes on production data without a backup and permission.

INSERT: add rows

INSERT INTO products (name, category, price, gst_rate)
VALUES ('Webcam', 'Peripherals', 1899, 0.18);

List the columns explicitly. The id column fills itself (INTEGER PRIMARY KEY in SQLite; IDENTITY or SERIAL elsewhere). Several rows: VALUES (...), (...), (...).

UPDATE: change rows

UPDATE products SET price = ROUND(price * 1.05, 2) WHERE category = 'Stationery';
UPDATE orders   SET status = 'Delivered'           WHERE status = 'Shipped';

DELETE: remove rows

DELETE FROM orders WHERE status = 'Cancelled';

The safety routine

  1. Write the WHERE as a SELECT first: SELECT * FROM orders WHERE status = 'Cancelled'; Check the rows and the count.
  2. Change SELECT * to UPDATE/DELETE, keeping the exact same WHERE.
  3. Run it inside a transaction:
BEGIN;
DELETE FROM orders WHERE status = 'Cancelled';
SELECT COUNT(*) FROM orders;      -- check
ROLLBACK;                         -- undo   (or COMMIT; to keep)

Until COMMIT, nothing is permanent. DB Browser for SQLite also holds changes until you click Write Changes (Ctrl+S); Revert Changes undoes them.

💡 Many teams soft-delete instead: add an is_deleted or status column and UPDATE it, rather than DELETE. Nothing is lost, and reports simply filter it out.

Constraints protect you

NOT NULL, UNIQUE, CHECK (price >= 0) and FOREIGN KEY rules make the database refuse bad changes, like an order for a product that doesn’t exist. (In SQLite, run PRAGMA foreign_keys = ON; to enforce foreign keys.)

Where beginners go wrong

Disaster Prevention
UPDATE without WHERE: every row changed SELECT first; transaction; some tools warn
Deleting customers who still have orders Foreign keys; check with a JOIN first
No backup Copy the file / take a backup before bulk changes
Running on the live system “just to test” Use a test copy

Practice

Download shop.db and this lesson’s exercise file below. Open the database in the free DB Browser for SQLite (or sqlite3 shop.db), paste the exercises into the Execute SQL tab and write a query for each task. The expected result is printed under every task; answers are at the bottom of the file.

📎 Practice files for this article

  • 📄
    Practice database (SQLite)shop.db: 12 customers, 10 products and 300 orders (April-September 2026).
    ⬇ DB · 28 KB
  • 🗃️
    Lesson 10 exercisesTasks with the expected result under each, and answers at the bottom.
    ⬇ SQL · 2 KB

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong