skip to content

What do START TRANSACTION, COMMIT and ROLLBACK do, and what happens without them?

level: juniorimportance: must knowfreq 82%

answer

  1. think in units of work
  2. what wraps a lone UPDATE?
  3. the default mode commits per statement
  4. one keyword opens it, two can close it

basics

~20 s

START TRANSACTION (or BEGIN) opens an explicit transaction; COMMIT ends it and makes all its work permanent; ROLLBACK ends it and discards all its work. Without them, autocommit wraps each statement in its own transaction that commits immediately.

solid answer

~40 s

A transaction is the unit of work the database treats as all-or-nothing, and three statements draw its boundaries. `START TRANSACTION` (spelled `BEGIN` in several engines) opens one explicitly; every statement after it belongs to that transaction until it ends. `COMMIT` ends the transaction and makes everything it did permanent and visible to other sessions. `ROLLBACK` ends it and throws all of that work away, leaving the database as if the transaction never ran. If you never write these, most clients run in **autocommit** mode: each individual statement is its own transaction that commits the instant it succeeds, so two UPDATEs are two independent transactions — the first is already permanent when the second fails. That is exactly why multi-statement work must be wrapped explicitly.

code

sql · 4 lines
sql
-- Autocommit: two independent transactions
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- If the second fails, the first is already permanent

go deeper

for a junior

Be able to name the three statements and say what each one leaves behind, and to explain why two related UPDATEs typed without BEGIN are not safe together.

for a middle

Explain autocommit precisely — that it is a session/driver setting, not part of the standard's model — and that COMMIT is a statement whose failure you must handle like any other.

for a senior

Show you decide transaction boundaries deliberately: which writes belong in one unit, what the error path does, and that a dropped connection rolls back rather than commits.

for a principal

Own the convention across a codebase: where boundaries are declared, that no code path reaches COMMIT by accident after an error, and that autocommit defaults are pinned rather than inherited from whatever client connects.

## The transaction is the unit, not the statement A transaction is a group of statements the database treats as one indivisible unit of work: either every effect it produced becomes part of the database, or none of them does. The three statements that define where that unit starts and stops are `START TRANSACTION` (or `BEGIN`), `COMMIT` and `ROLLBACK`. Everything else about transactions — what other sessions can see while yours runs, how the engine undoes work — sits underneath these three; the language surface is deliberately tiny. ## Autocommit: what happens if you write none of them If you open a client, type an `UPDATE` and press enter, it takes effect. There was still a transaction — you just did not write it. In **autocommit** mode the session implicitly wraps each statement in its own transaction and commits it the moment the statement succeeds. Two statements typed in a row are therefore two separate transactions: ```sql UPDATE accounts SET balance = balance - 100 WHERE id = 1; -- committed UPDATE accounts SET balance = balance + 100 WHERE id = 2; -- separate transaction ``` If the second statement fails, or the process dies between the two, the first is already permanent and the money has vanished. There is no `ROLLBACK` you can issue afterwards, because there is nothing still open to roll back. Autocommit is a client/session setting, not part of the SQL standard's core model — the standard says a transaction begins implicitly at the first statement and stays open until you end it explicitly. In practice the majors and their drivers ship autocommit **on**: MySQL's `autocommit` defaults to 1, JDBC connections default to `autoCommit = true`, and most interactive clients commit per statement. Oracle's classic clients are the well-known exception: a transaction starts at your first DML and waits for you. So never assume; know what your client does. ## Opening a transaction explicitly ```sql START TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT; ``` Now the two updates are one unit. `START TRANSACTION` is the portable spelling. `BEGIN` is accepted as a synonym by several engines (and SQL Server spells it `BEGIN TRANSACTION`), but in standard SQL/PSM `BEGIN` opens a compound statement block, which is why `START TRANSACTION` is the safer thing to write in portable scripts. ## COMMIT `COMMIT` (optionally `COMMIT WORK`) ends the transaction and makes its effects permanent: the changes become visible to other sessions and the resources the transaction held are released. The next statement you run starts a new transaction. Two things candidates forget: `COMMIT` is itself a statement that can **fail** — deferred constraint checks and serialization failures surface there, and a failed `COMMIT` leaves you rolled back — and once it succeeds there is no `ROLLBACK` that takes it back; the only way to reverse committed work is a new transaction that writes compensating changes. ## ROLLBACK `ROLLBACK` (or `ROLLBACK WORK`) ends the transaction and discards everything it did since it began. It is the normal response to an application-level error mid-way through a unit of work. What it does **not** undo is anything outside the database's control: rows already sent to the client, a message you published to a broker, an email you sent. It also does not give back identity or sequence values the transaction consumed — those are deliberately non-transactional so concurrent sessions never wait on each other for a number, which is why gaps in generated keys are normal. ## Endings you did not write A transaction can also end without any of your statements. If the connection drops or the client exits with a transaction still open, the engine rolls it back — an unfinished transaction is never silently committed. If the server crashes, the same is true after recovery. And on some engines a DDL statement inside your transaction forces an implicit `COMMIT` of everything before it, which is the single most surprising way an explicit transaction ends early. ## What interviewers listen for They want to hear that you know a bare statement is still a transaction, that you can name the three boundary statements and what each leaves behind, and that you reach for an explicit `START TRANSACTION` the moment a piece of work spans more than one statement. Saying "I always wrap related writes in a transaction, and I decide explicitly whether the error path commits or rolls back" is the whole answer at this level.

  • Can COMMIT itself fail, and what state are you in if it does?
    Yes. Deferred constraint checks, serialization failures and I/O errors surface at `COMMIT`. When it fails the transaction is over and rolled back — none of its work survives. Treat `COMMIT` as a statement whose error you must handle, not as a formality that always succeeds.
  • A client crashes with an explicit transaction open and no COMMIT sent. What happens to its changes?
    They are rolled back. When the session ends without a `COMMIT`, the engine discards the open transaction's work, and the same happens after crash recovery. An unfinished transaction is never silently committed — the failure mode to worry about is the opposite one, work that was already committed statement by statement under autocommit.
  • Is BEGIN a portable way to open a transaction?
    Not fully. `START TRANSACTION` is the portable spelling. `BEGIN` works as a synonym in PostgreSQL and MySQL, SQL Server needs `BEGIN TRANSACTION`, and in standard SQL/PSM a bare `BEGIN` opens a compound statement block rather than a transaction. In scripts meant to run anywhere, write `START TRANSACTION`.

Autocommit is an editor that saves to disk after every keystroke; an explicit transaction is opening a document, making several edits, and then choosing Save or Discard for the batch.

saying these in an interview costs you the question

  • Thinks a statement run without BEGIN is outside any transaction
  • Believes ROLLBACK can undo an already committed transaction
  • Assumes every client and driver defaults to autocommit off
  • Treats COMMIT as a statement that cannot fail
  • Expects ROLLBACK to return consumed sequence or identity values

context