3. Modifying Data, Performance & Capstone

INSERT, UPDATE, DELETE & Safe Transactions

Add new records with INSERT, modify existing data safely with UPDATE and DELETE, and protect multi-step updates using database transactions.

3 min readUpdated 2026-11-29
In This Lesson
  • Insert new records safely using explicit column lists
  • Prevent accidental table-wide edits when running UPDATE and DELETE
  • Protect multi-step changes using BEGIN, COMMIT, and ROLLBACK transactions

Querying existing data with SELECT is only half of building real applications. Apps constantly register new signups, update user settings, process payments, and remove cancelled records—and doing those writes carelessly can overwrite an entire production table in milliseconds.

Let's learn the three SQL write commands alongside the professional guardrails engineers use around them.

Adding New Rows with INSERT INTO

Always list the exact target columns after the table name before writing VALUES. That keeps your query readable and prevents bugs if someone later adds a new column to the table:

sql
INSERT INTO students (full_name, email)
VALUES
  ('Meera Nair', 'meera@example.com'),
  ('Vikram Joshi', 'vikram@example.com');

Separating tuples with commas lets you insert multiple rows cleanly in a single database round-trip.

Modifying Rows Safely with UPDATE

Use UPDATE together with SET to assign new values and WHERE to target only the exact row(s) you intend to touch:

sql
UPDATE courses
SET estimated_hours = 8, level = 'Beginner'
WHERE slug = 'python-fundamentals';
Never Forget the WHERE Clause!

Running UPDATE students SET active = FALSE; without a WHERE clause updates every single row in the entire table with zero confirmation prompt. Adopt this golden habit: write a SELECT * FROM ... WHERE ... query first to preview the exact rows your filter matches, and only then swap SELECT * for UPDATE or DELETE.

Removing Records with DELETE

Just like UPDATE, DELETE depends on WHERE to avoid wiping the whole table:

sql
DELETE FROM enrollments
WHERE id = 42;

In production apps, teams frequently prefer a soft delete—setting a timestamp (UPDATE users SET deleted_at = CURRENT_TIMESTAMP WHERE id = 42) rather than permanently erasing rows—so accidental deletions can be audited or restored without losing related invoice history.

All-or-Nothing Safety with Transactions

Imagine transferring $1,000 from Account A to Account B. Internally, that requires two separate updates:

  1. Subtract $1,000 from Account A.
  2. Add $1,000 to Account B.

What happens if the power cuts out or a constraint error occurs right between the first update and the second? Account A loses $1,000 while Account B never receives it!

Wrapping both changes inside a transaction solves this cleanly. A transaction guarantees all-or-nothing execution: either every statement inside completes and is saved permanently (COMMIT), or every statement is cleanly undone (ROLLBACK) as if nothing ever ran:

sql
BEGIN;

UPDATE accounts SET balance = balance - 1000 WHERE id = 1;
UPDATE accounts SET balance = balance + 1000 WHERE id = 2;

COMMIT;

If an error happens before COMMIT, running ROLLBACK; restores both balances to their exact starting numbers.

Try It Live: Transferring Funds Safely

Test an atomic balance transfer in SQLite right here—try subtracting from Account 1, adding to Account 2 inside a transaction, and inspecting the final balances:

SQL (SQLite)Live SQL — Transaction Transfer Example
Output

Click “Run Code” to execute this snippet right in your browser.

Quick Checkpoints

Check Your Understanding

What happens if you execute DELETE FROM orders; without a WHERE clause?