Skip to content
wannacode

ACID: all or nothing

In short: A transaction makes several queries one unit that either fully happens or not at all, and ACID names its four promises: atomicity, consistency, isolation and durability.

Problem

The order that stopped halfway

Teodora orders two loaves of bread and something the program took from an old basket: product 99, which no longer exists. The program makes the order in two queries: first the order itself, then its lines. Run them:

SQLdatabase: shop

The second query stops with an error: product 99 does not exist. But the first one has already gone through. The database is left with order 61 and not a single line:

SQLdatabase: shop

An order for 0 products. The courier will get an empty bag, and the reports will count one order too many. The same happens if the server stops between the two queries.

The idea

A transaction

The action “place an order” is several queries that make sense only together. A transaction makes them one whole:

  • BEGIN; starts it.
  • COMMIT; confirms it: everything goes in at once.
  • ROLLBACK; cancels it: as if nothing had happened.

Here is the whole order: the order, its two lines and the stock, in one transaction:

SQLdatabase: shop

If any query between BEGIN and COMMIT fails, the program says ROLLBACK and the database goes back to how it was. Here is the cancel:

SQLdatabase: shop

There are still 60 orders. Order 61 existed only inside the transaction.

Try it

Undo it

A colleague started a transaction and deleted all reviews. Add a line that undoes the delete, before the count.

SQLdatabase: shop

The idea

ACID

Databases promise four things about transactions. From the first letters this is called ACID:

  • Atomicity. All or nothing. An order without lines cannot be left behind.
  • Consistency. After the transaction, all the database's rules hold: keys, CHECK, UNIQUE.
  • Isolation. Two transactions running at the same time do not get in each other's way, as if they ran one after the other. How far that goes, we will see in the lesson on concurrent work.
  • Durability. After COMMIT the change is on disk. A power cut does not erase it.
More precisely, if you are curious

Every single query is already a small transaction: an INSERT with three rows, one of which breaks a rule, puts in none. That is why in the first field the bread line did not get in either. The trouble starts when one action takes several queries. Then the program writes BEGIN and COMMIT.

Exercise

Yavor's order

Yavor (customer 20) orders 1 pack of coffee beans (product 17, 14.90 €). Create order 61 dated today (2026-10-25) with status new, its line, and reduce the stock, all in one transaction.

SQLdatabase: shop

Test

Check yourself

1. A transaction stopped with an error on the third of five queries and was rolled back. What is left in the database?
2. Which letter of ACID promises that after COMMIT the order will not vanish in a power cut?
3. One INSERT with three rows, the third of which breaks a rule. How many rows get in?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·