Menu

1 / 2

Part 12: Transactions

DEV Community: sql·Mohamed Idris·6 months ago
#jPQ8IfDu
#dev#transaction#balance#commit#article
Reading 0:00
15s threshold

Part of the "SQL: Zero to Ninja" series, written for junior web devs.

Picture this. A user pays for an order. Your code creates the order row. Then,

right before it saves the items... the server crashes. Now you have a paid order

with nothing in it. The customer is angry, your data is broken, and you are

debugging at 2am. A transaction is the seatbelt that stops this. Let's learn

it.

The idea in one line

A transaction groups several statements into one all-or-nothing unit: either

they all succeed, or none of them happen.

The metaphor: carrying a couch with a friend

You and a friend are carrying a heavy couch up the stairs. The rule is simple:

you both lift, or you both stop. You never leave the couch half-lifted,

balanced on the stairs, while one of you wanders off. That is dangerous and silly.

A transaction is the same deal between your statements. They go up the stairs

together, or they do not go at all.

Half-done is NOT allowed:

[ subtract money from A ] done [ add money to B ] crash!…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.