🗃️Database Transaction
Also known as Transaction, SQL Transaction
A group of changes that all happen, or none of them do.
Think of it like
Think of it like a card swap with a friend. You both hand a card over, or no one does.
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.
Why it matters
If a crash comes in the middle of a task, the data is left broken. Transactions stop that.
Definition
Example code
sql
BEGIN;
UPDATE accounts SET balance = balance - 30 WHERE name = 'Emma';
ROLLBACK;
SELECT * FROM accounts;
-- Emma | 100
-- Jack | 50Common mistake
Key takeaways
- 1All or nothing. Every change or none.
- 2Commit or roll back. Keep it all, or undo it all.
- 3Keep it short. Long ones block others.