LearnContact
Lesson 5919 min read

Transactions

Learn what a transaction is, why it matters for data safety, and how the ACID properties keep your data consistent.

Introduction

Most of the statements you have written so far have run on their own, one at a time. But real-world operations are often made up of several steps that must all succeed together, or not happen at all. Imagine transferring money between two bank accounts — if the debit from one account succeeds but the credit to the other fails, money simply vanishes.

This is the problem transactions solve. A transaction groups multiple SQL statements into a single all-or-nothing unit of work. This lesson introduces transactions, why they matter, and the ACID properties that describe the guarantees they provide.

What You Will Learn
  • What a transaction is and how it groups statements together.
  • Why transactions matter, using a bank transfer as the running example.
  • How to begin a transaction with START TRANSACTION.
  • What the ACID properties (Atomicity, Consistency, Isolation, Durability) mean.
  • A full worked example simulating a transfer between two accounts.

What is a Transaction?

A transaction is a group of one or more SQL statements that are executed together as a single, indivisible unit. Either every statement in the transaction succeeds and is saved, or, if anything goes wrong, none of them are saved at all.

This "all or nothing" guarantee is what makes transactions so important for any operation where partial completion would leave your data in a broken or inconsistent state.

Simple Definition

Think of a transaction as a checkpoint system: you can make several changes, and only make them permanent once you are sure every single one of them worked.

Why Transactions Matter

Consider transferring $1,000 from Alice’s account to Bob’s account. This requires two separate statements: subtract $1,000 from Alice, and add $1,000 to Bob.

UPDATE accounts SET balance = balance - 1000 WHERE name = 'Alice';

-- What if the server crashes right here?

UPDATE accounts SET balance = balance + 1000 WHERE name = 'Bob';
Without a Transaction

If the first UPDATE runs but the server crashes, loses connection, or hits an error before the second UPDATE runs, $1,000 has been deducted from Alice and never credited to Bob. The money simply disappears — a serious, unacceptable bug in any real financial system.

Wrapping both statements in a transaction guarantees that either both updates happen, or neither does — the money is never lost in between.

Starting a Transaction

A transaction begins with START TRANSACTION (or the equivalent BEGIN), after which every statement is part of that transaction until you explicitly COMMIT or ROLLBACK (covered fully in the next lesson).

START TRANSACTION;

UPDATE accounts SET balance = balance - 1000 WHERE name = 'Alice';
UPDATE accounts SET balance = balance + 1000 WHERE name = 'Bob';

COMMIT;

COMMIT permanently saves every change made since START TRANSACTION. Until COMMIT runs, the changes are only visible within this same database connection — other connections still see the old data.

The ACID Properties

Transactions are built around four guarantees, commonly remembered by the acronym ACID. You may recall this term briefly from the earlier DBMS vs RDBMS lesson — here is what each letter actually means in practice.

LetterPropertyMeaning
AAtomicityAll statements in the transaction succeed together, or none of them are applied at all.
CConsistencyA transaction can only move the database from one valid state to another, never breaking its rules (like constraints).
IIsolationTransactions running at the same time do not interfere with or see each other’s unfinished changes.
DDurabilityOnce a transaction is committed, its changes survive permanently, even if the server crashes immediately after.
Why ACID Matters Here

The bank transfer example demonstrates Atomicity directly: both UPDATE statements form one atomic operation. Without it, a crash between the two statements would leave the database in an inconsistent, non-ACID-compliant state.

Worked Example: A Bank Transfer

Let’s put this together with a complete, runnable example.

setup.sql
CREATE TABLE accounts (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    balance DECIMAL(10,2)
);

INSERT INTO accounts VALUES
    (1, 'Alice', 5000.00),
    (2, 'Bob', 2000.00);
transfer.sql
START TRANSACTION;

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

COMMIT;

SELECT * FROM accounts;
Result
+----+-------+----------+
| id | name  | balance  |
+----+-------+----------+
| 1  | Alice | 4000.00  |
| 2  | Bob   | 3000.00  |
+----+-------+----------+
What Just Happened

Both UPDATE statements ran as one atomic unit. Because COMMIT was reached, both changes are now permanently saved together — $1,000 left Alice’s balance and arrived in Bob’s, with no in-between state ever visible to other connections.

Autocommit Mode

By default, MySQL runs in autocommit mode, meaning every single statement is automatically treated as its own tiny transaction and committed immediately. START TRANSACTION temporarily switches off autocommit until the next COMMIT or ROLLBACK.

SHOW VARIABLES LIKE 'autocommit';

SET autocommit = 0;

You rarely need to change autocommit manually — using START TRANSACTION explicitly, as shown above, is the normal and recommended approach.

Common Mistakes

Avoid These Mistakes
  • Forgetting to COMMIT, leaving changes uncommitted and invisible to other connections.
  • Assuming individual statements outside a transaction are automatically grouped together — they are not, unless autocommit is off.
  • Running long transactions that hold locks for a long time, slowing down other users.
  • Treating a SELECT-only sequence as needing a transaction when nothing is actually being changed.
  • Forgetting that a crash before COMMIT means none of the transaction’s changes are saved.

Best Practices

  • Wrap any group of related INSERT/UPDATE/DELETE statements that must succeed together in a transaction.
  • Keep transactions short — the longer they stay open, the longer they can block other users.
  • Always plan for the failure path: what should happen if one statement inside the transaction fails?
  • Use transactions for anything involving money, inventory counts, or any data where partial updates would cause real harm.
  • Test failure scenarios deliberately, not just the happy path.

Frequently Asked Questions

Do all MySQL storage engines support transactions?

No. InnoDB (the default engine) fully supports transactions. MyISAM, an older engine, does not.

Can a SELECT statement be part of a transaction?

Yes, though SELECT alone does not need transactional protection since it does not modify data. It is often included so you can read a consistent snapshot alongside writes.

What happens if I never call COMMIT or ROLLBACK?

The transaction stays open, holding any locks it has acquired, until you commit, roll back, or the connection closes — which can block other queries.

Is a transaction the same thing as a trigger or event?

No. Transactions group statements you run manually into one safe unit; triggers and events are separate mechanisms for automatically running SQL in response to changes or schedules.

Key Takeaways

  • A transaction groups multiple SQL statements into a single all-or-nothing unit.
  • Transactions matter whenever partial completion of a set of changes would corrupt your data.
  • START TRANSACTION begins one; COMMIT saves it permanently.
  • ACID stands for Atomicity, Consistency, Isolation, and Durability.
  • A bank transfer is the classic example of why atomicity matters.

Summary

Transactions let you treat a group of related SQL statements as one safe, indivisible operation, guaranteeing that your data never ends up in a broken, half-updated state.

In this lesson, you learned what a transaction is, why it matters using a bank transfer example, how to start one with START TRANSACTION, and what the ACID properties guarantee. Next, you will learn the full set of commands — COMMIT, ROLLBACK, and SAVEPOINT — used to control a transaction’s outcome.

Lesson 59 Completed
  • You understand what a transaction is.
  • You can explain why transactions matter using a real example.
  • You know what each ACID property guarantees.
  • You are ready to learn COMMIT, ROLLBACK, and SAVEPOINT.
Next Lesson →

COMMIT, ROLLBACK & SAVEPOINT