# What Is a Transaction in a Database?

> A transaction is a sequence of one or more database operations, reads and writes, treated as a single logical unit of work. The database guarantees that either every operation completes successfully or none take effect, leaving no partial result. A transaction starts with BEGIN and ends with COMMIT to save its changes or ROLLBACK to undo them.

Canonical: https://devsunite.com/resources/core-subjects/dbms/what-is-a-transaction
Last updated: 2026-11-22

Subject: DBMS · Category: ACID Properties · 3 min read

A transaction is a sequence of one or more database operations, reads and writes, that are treated as a single logical unit of work. The database guarantees that either every operation in that unit completes successfully, or none of them take effect at all. There's no partial result left behind for anyone else to see.

## Why transactions exist

Real-world actions on data are rarely a single operation. Transferring money between two accounts means two writes. Placing an order means writing to an orders table, an inventory table, and possibly a payments table, all at once. If the database only guaranteed individual statements and not the group as a whole, a crash or error partway through would leave data in a state that never corresponds to any real business event, money deducted with nothing credited, an order created with no payment recorded.

A transaction wraps that whole sequence in a boundary so the database can promise: either the entire sequence happened, exactly as written, or it's as if none of it happened at all.

## How a transaction actually runs

1. **BEGIN.** The application tells the database it's starting a new transaction. Every statement from here on is part of this one unit until it's explicitly closed.
2. **Statements execute.** Reads and writes run as normal, but writes aren't necessarily final yet, the database is prepared to undo them if needed.
3. **COMMIT or ROLLBACK.** If everything succeeded, the application issues COMMIT, and the database makes every change permanent at once. If something failed, or the application decides to cancel, it issues ROLLBACK, and the database undoes every change made since BEGIN, returning the data to exactly its prior state.

## A worked example

Consider booking a seat on a flight. The transaction needs to do two things: mark the seat as taken in a `seats` table, and insert a new row into a `bookings` table.

```sql
BEGIN;

UPDATE seats SET status = 'booked' WHERE seat_id = '14A' AND status = 'available';

INSERT INTO bookings (passenger_id, seat_id, booked_at)
VALUES (501, '14A', NOW());

COMMIT;
```

If the `UPDATE` finds the seat already booked (someone else grabbed it first) and updates zero rows, the application can detect that and issue `ROLLBACK` instead of `COMMIT`, ensuring no orphaned booking row gets created for a seat nobody actually reserved. If both statements succeed, `COMMIT` locks in both changes together, the seat is booked and the booking record exists, guaranteed together, never one without the other.

Running many such transactions at once, safely and with crash recovery, is the job of [transaction processing](/resources/core-subjects/dbms/what-is-transaction-processing), which covers transaction processing systems, transaction states and OLTP vs OLAP.

## Where this matters in real systems

Every real application that touches money, inventory, or any multi-step state change relies on transactions constantly, not as an occasional feature but as the default way multi-statement writes are done. Object-relational frameworks like Django's ORM or Rails' ActiveRecord wrap common multi-step operations in an implicit transaction automatically, precisely because forgetting to do this by hand is such a common and dangerous mistake.

## What interviewers actually ask about transactions

- **"What is a transaction, in one sentence?"** A group of one or more database operations executed as a single unit, either all succeed or none do.
- **"What do BEGIN, COMMIT, and ROLLBACK each do?"** BEGIN starts the unit, COMMIT makes every change in it permanent, ROLLBACK undoes every change back to the state before BEGIN.
- **"Why wrap multiple statements in one transaction instead of running them separately?"** So a failure partway through can't leave the data in an inconsistent state that corresponds to no real event.
- **"What happens if the application crashes mid-transaction, before COMMIT?"** The database's crash recovery treats it as if ROLLBACK was called, none of the transaction's changes are kept.

## Key takeaways

- A transaction groups multiple database operations into one all-or-nothing unit.
- BEGIN starts it, COMMIT finalizes every change, ROLLBACK undoes every change back to the starting point.
- Transactions prevent multi-step operations from leaving data half-applied after an error or crash.
- Most application frameworks wrap common multi-step writes in an implicit transaction by default.

## Related

- [Next: Atomicity in ACID Transactions](https://devsunite.com/resources/core-subjects/dbms/atomicity)
- [More in ACID Properties: Consistency in ACID Transactions](https://devsunite.com/resources/core-subjects/dbms/consistency)
- [More in ACID Properties: Isolation in ACID Transactions](https://devsunite.com/resources/core-subjects/dbms/isolation)
- [More in ACID Properties: Durability in ACID Transactions](https://devsunite.com/resources/core-subjects/dbms/durability)
- [More in ACID Properties: ACID Properties: Complete Overview](https://devsunite.com/resources/core-subjects/dbms/complete-overview)
- [All 174 DBMS interview questions](https://devsunite.com/resources/core-subjects/dbms)
