# Database Transaction

> A group of changes that all happen, or none of them do.

**Field:** Software Development  
**Category:** Databases  
**Also known as:** Transaction, SQL Transaction

## Think of it like

Think of it like a card swap with a friend. You both hand a card over, or no one does.

## Definition

A transaction is a group of database changes that work or fail as one. You start it, make some changes, then COMMIT to keep them all. If something goes wrong, a ROLLBACK undoes each change in the group. This stops half-done work, like money that leaves one account but never reaches the other. All the big SQL databases support them.

## Example

Emma moves 30 euros from her account to Jack's. The app takes it from her and adds it to him in one transaction, so it can never vanish halfway.

## Example code

```sql
BEGIN;
UPDATE accounts SET balance = balance - 30 WHERE name = 'Emma';
ROLLBACK;
SELECT * FROM accounts;
-- Emma | 100
-- Jack | 50
```

The rollback undoes the change, so Emma still has 100.

## Why it matters

If a crash comes in the middle of a task, the data is left broken. Transactions stop that.

## Common mistake

Keeping a transaction open while waiting for a user or a slow API. It locks rows and blocks everyone else.

## Three principles

- **All or nothing.** Every change or none.
- **Commit or roll back.** Keep it all, or undo it all.
- **Keep it short.** Long ones block others.

## Related terms

- [Atomicity, Consistency, Isolation, Durability (ACID)](https://learn.benibix.com/term/development/acid)
- [Database](https://learn.benibix.com/term/development/database)
- [Structured Query Language (SQL)](https://learn.benibix.com/term/development/sql)
- [Relational Database](https://learn.benibix.com/term/development/relational-database)
- [Write-Ahead Log (WAL)](https://learn.benibix.com/term/development/write-ahead-log)

---

Source: https://learn.benibix.com/term/development/transaction
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
