0
0
SQLquery~10 mins

ACID properties mental model in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ACID properties mental model
Start Transaction
Perform Operations
Atomicity
Commit or Rollback
End
This flow shows a transaction starting, operations running, checking ACID properties, then committing or rolling back.
Execution Sample
SQL
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
This code transfers 100 units from account 1 to account 2 within a transaction ensuring ACID properties.
Execution Table
StepActionAtomicityConsistencyIsolationDurabilityResult
1BEGIN TRANSACTIONTransaction started as a single unitDatabase state is stable before changesNo other transactions interfere yetChanges not yet permanentTransaction open
2UPDATE account 1 balance -100Change recorded but not finalBalance remains consistent (no negative if checked)Other transactions cannot see partial changeChange in memory, not on diskBalance decreased in transaction
3UPDATE account 2 balance +100Change recorded but not finalTotal money conserved, consistent stateIsolation maintained, no dirty readsChange in memory, not on diskBalance increased in transaction
4COMMITAll changes applied or noneDatabase moves to new consistent stateOther transactions now see changesChanges saved permanently on diskTransaction committed successfully
5ENDTransaction endsDatabase stableIsolation liftedDurability ensuredTransaction complete
💡 Transaction ends after commit, ensuring all ACID properties are met
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
account_1_balance1000900900900900
account_2_balance500500600600600
transaction_statenoneopenopencommittedclosed
Key Moments - 3 Insights
Why does the transaction not show partial changes to other users before commit?
Because of Isolation (see execution_table rows 2 and 3), changes are hidden from other transactions until commit.
What happens if the transaction fails before commit?
Atomicity ensures all changes are rolled back, so no partial updates remain (refer to execution_table step 4 commit importance).
How is data saved permanently after commit?
Durability guarantees changes are saved to disk and survive crashes (see execution_table step 4 durability column).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the account_1_balance after step 3?
A1000
B900
C800
D1100
💡 Hint
Check the variable_tracker for account_1_balance after step 3
At which step does the transaction become permanent and visible to others?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table step 4 commit action and durability column
If the transaction is rolled back instead of committed, what happens to the balances?
ABalances revert to start values
BBalances remain changed
COnly account_1_balance changes
DOnly account_2_balance changes
💡 Hint
Refer to key_moments about Atomicity and rollback behavior
Concept Snapshot
ACID properties ensure reliable transactions:
Atomicity: all or nothing
Consistency: valid data state
Isolation: no interference
Durability: changes saved
Use BEGIN, COMMIT, ROLLBACK to control transactions
Full Transcript
This visual execution shows a database transaction starting with BEGIN TRANSACTION. It performs two updates: subtracting 100 from account 1 and adding 100 to account 2. Throughout, Atomicity ensures these steps are treated as one unit, so either both succeed or none do. Consistency keeps the database valid, for example, total money conserved. Isolation hides these changes from other users until commit. Durability saves changes permanently on disk after COMMIT. The transaction ends successfully, leaving the database in a stable state. Variables like account balances and transaction state change step-by-step, showing how ACID properties protect data integrity.