What is a transaction?
A transaction groups several database changes so they all happen together or not at all, keeping the data consistent.
Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?
A transaction is a bundle of database operations treated as one unit. Moving money is the classic picture: take 50 from account A and add 50 to account B. If the power fails after the first step, the money has vanished. Inside a transaction, either both steps are saved (commit) or neither is (rollback), so the books always balance.
Databases promise ACID properties for transactions: atomic, consistent, isolated and durable. Every web framework opens one around a request or a form submission, often without you noticing. You use them explicitly when several tables must change together, and you keep them short, because a long open transaction blocks other users from touching the same rows.
BEGIN;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
COMMIT;
See also: What is a database? and What is SQL?
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0067 Your first SQL queries: SELECT, WHERE, ORDER BY, LIMIT and GROUP BY Read data from a table, keep only the rows you want, sort them, take the first few, and summarize groups with COUNT,… Beginner · Step by step · Any system Article Queries 15 Oct 2026
- 0069 NULL in SQL: why = NULL never matches and how to handle missing values NULL means "unknown", so comparisons with it are neither true nor false. Use IS NULL, COALESCE and careful aggregates to avoid rows… Intermediate · Explains an idea · Any system Article Queries 15 Oct 2026
- 0071 Fix: column “x” must appear in the GROUP BY clause or be used in an aggregate function PostgreSQL rejects a grouped query that selects a column with more than one possible value per group. Add the column to GROUP… Intermediate · Fixes an error · Any system Article Queries 15 Oct 2026
- 0077 SQL cheat sheet: a runnable reference for everyday queries One SQL file that builds a tiny database and demonstrates filtering, sorting, grouping, joins, CASE, CTEs, window functions, safe updates and indexes.… Beginner · Cheat sheet · Any system Script Joins · Queries 15 Oct 2026
- 0215 mysql command-line client cheat sheet The connection options, built-in commands and everyday SQL you need in the mysql (and mariadb) command-line client, on one page. Beginner · Cheat sheet · Any system Article Administration · Queries 15 Oct 2026
- 0260 Design and build a library database in SQLite: tables, keys, joins, reports and backups Model books, members and loans, create tables with keys and constraints, load sample data, write join and aggregate reports, add indexes checked… Beginner · Guided project · Any system Article Joins · Queries 15 Oct 2026
Repos in the wild
- DuckDB duckdb/duckdb An in-process analytical SQL database. Run fast SQL directly on CSV, Parquet and JSON files, from the command line or inside Python and other languages. GitHub
- SQLite (official Git mirror) sqlite/sqlite The source tree of SQLite, the self-contained SQL database engine embedded in phones, browsers and countless applications, mirrored to GitHub by its developers. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Dominik +880 chars