Talking Through Transactions for Beginners
Episode-style notes, written like a friendly chat. No audio, no guests. Today's topic is transactions: how databases make sure a group of changes either all happen or none do.
What is a transaction, in one sentence?
A transaction is a group of database changes that are treated as a single unit: either all of them are saved, or none of them are.
Why would I need that?
The classic example is moving money between two accounts. You take money out of one and put it into the other. That is two changes:
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
If the program crashed after the first line, the money would vanish. A transaction makes sure that cannot happen.
So how do I write one?
You start it, make your changes, then commit:
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
COMMIT;
If something goes wrong in the middle, you roll back instead, which undoes everything since BEGIN:
ROLLBACK;
Do I have to type BEGIN every time?
Not always. Many databases run each single statement in its own small transaction automatically; this is often called autocommit. Many drivers and ORMs also start transactions for you. In Python's sqlite3 module, for example, changes are not saved until you call commit(), which surprises lots of beginners whose data "disappears".
I have heard of ACID. What is that?
It is a short way of remembering four promises a transaction makes:
| Letter | Word | Plain meaning |
|---|---|---|
| A | Atomicity | All or nothing |
| C | Consistency | The database's rules are still true afterwards |
| I | Isolation | Transactions running at the same time do not trip over each other's half-finished work |
| D | Durability | Once committed, the change survives a crash or power cut |
Where would I use one in a beginner project?
Anywhere one action changes more than one thing. In a club library app, borrowing a book might add a row to loans and mark the book as unavailable. Wrap both in a transaction so a book is never "borrowed" without being marked, or the other way round.
with conn: # sqlite3: commits on success, rolls back on error
conn.execute("INSERT INTO loans (book_id, member_id) VALUES (?, ?)", (book_id, member_id))
conn.execute("UPDATE books SET available = 0 WHERE id = ?", (book_id,))
Any common mistakes?
Two big ones. First, forgetting to commit, so changes are lost. Second, keeping a transaction open for a long time, for example while waiting for a user to click something. That can block other people's changes. Keep transactions short: start, change, commit.
One-line takeaway
A transaction is a promise: these changes happen together or not at all. If an action changes two things, it probably needs one.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative outline only: no live session, recording, registration or certificate is offered on this site. Any speakers, panels or schedules mentioned are hypothetical.