📖 Scenario: Imagine a bank system where multiple people are sending money to each other at the same time. We want to make sure that the money is always correct and no one loses or gains money by mistake.
🎯 Goal: Build a simple example that shows how transactions help keep data consistent when multiple changes happen together.
📋 What You'll Learn
Create a dictionary called accounts with these exact entries: 'Alice': 1000, 'Bob': 500
Create a variable called transaction_active and set it to False
Write a function called transfer that takes sender, receiver, and amount and updates accounts only if transaction_active is True
Set transaction_active to True before calling transfer and set it back to False after
💡 Why This Matters
🌍 Real World
Banks and many other systems use transactions to keep data accurate when many changes happen at once.
💼 Career
Understanding transactions is important for database administrators, software developers, and anyone working with data systems to ensure data integrity.
Progress0 / 4 steps
1
Create the initial accounts data
Create a dictionary called accounts with these exact entries: 'Alice': 1000 and 'Bob': 500
DBMS Theory
Hint
Use curly braces to create a dictionary with keys 'Alice' and 'Bob' and their balances.
2
Add a transaction status variable
Create a variable called transaction_active and set it to False
DBMS Theory
Hint
This variable will help us know if a transaction is happening.
3
Write the transfer function with transaction check
Write a function called transfer that takes sender, receiver, and amount. Inside the function, check if transaction_active is True. If yes, subtract amount from accounts[sender] and add it to accounts[receiver]
DBMS Theory
Hint
Use an if statement to check transaction_active before changing balances.
4
Use transaction_active to control the transfer
Set transaction_active to True before calling transfer('Alice', 'Bob', 200) and set it back to False after
DBMS Theory
Hint
This shows how transactions control when data changes happen.
Practice
(1/5)
1. What is the main reason transactions ensure data consistency in a database?
easy
A. They speed up the database queries.
B. They allow multiple users to edit the same data at once without control.
C. They make sure all parts of a task complete successfully or none do.
D. They store data in multiple formats.
Solution
Step 1: Understand the role of transactions
Transactions group multiple operations into one unit that either fully succeeds or fully fails.
Step 2: Connect to data consistency
This all-or-nothing approach prevents partial updates that could corrupt data.
Final Answer:
They make sure all parts of a task complete successfully or none do. -> Option C
Quick Check:
All-or-nothing = Data consistency [OK]
Hint: Remember: transactions are all-or-nothing actions [OK]
Common Mistakes:
Thinking transactions speed up queries
Believing transactions allow uncontrolled edits
Confusing data formats with consistency
2. Which of the following is the correct SQL command to start a transaction?
easy
A. RUN TRANSACTION;
B. START QUERY;
C. OPEN DATABASE;
D. BEGIN TRANSACTION;
Solution
Step 1: Identify the standard SQL command
The standard command to begin a transaction is BEGIN TRANSACTION;.
Step 2: Eliminate incorrect options
Other options like START QUERY; or OPEN DATABASE; are not valid SQL commands for transactions.
Final Answer:
BEGIN TRANSACTION; -> Option D
Quick Check:
Start transaction = BEGIN TRANSACTION [OK]
Hint: Transaction always starts with BEGIN TRANSACTION [OK]
Common Mistakes:
Using START QUERY instead of BEGIN TRANSACTION
Confusing transaction start with database open
Using RUN TRANSACTION which is invalid
3. Consider this sequence of SQL commands: BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT; What is the main result of this transaction?
medium
A. Both updates are saved together or none if error occurs.
B. Only the first update is saved, second is ignored.
C. The transaction rolls back automatically after updates.
D. The updates happen but data may become inconsistent.
Solution
Step 1: Analyze the transaction commands
The commands start a transaction, perform two updates, then commit the changes.
Step 2: Understand commit effect on data consistency
Commit saves all changes together. If any error occurs before commit, none are saved, ensuring consistency.
Final Answer:
Both updates are saved together or none if error occurs. -> Option A
Quick Check:
Commit = all-or-nothing save [OK]
Hint: COMMIT saves all changes together [OK]
Common Mistakes:
Thinking only first update saves
Assuming automatic rollback without error
Believing updates cause inconsistency
4. A developer writes this code: BEGIN TRANSACTION; UPDATE inventory SET quantity = quantity - 5 WHERE product_id = 10; -- Missing COMMIT here UPDATE sales SET total = total + 100 WHERE sale_id = 20; What is the main problem with this transaction?
medium
A. The transaction will never start.
B. The changes may not be saved due to missing COMMIT.
C. The updates will cause syntax errors.
D. The transaction will auto-commit after each update.
Solution
Step 1: Identify missing transaction command
The code starts a transaction but does not include a COMMIT command to save changes.
Step 2: Understand effect of missing COMMIT
Without COMMIT, changes remain uncommitted and may be lost if connection closes or rollback occurs.
Final Answer:
The changes may not be saved due to missing COMMIT. -> Option B
Quick Check:
Missing COMMIT = no save [OK]
Hint: Always end transactions with COMMIT or ROLLBACK [OK]
Common Mistakes:
Assuming transaction never starts
Expecting syntax errors from missing COMMIT
Believing auto-commit happens without command
5. A bank uses transactions to transfer money between accounts. If the system crashes after deducting money from one account but before adding it to another, how do transactions help maintain data consistency?
hard
A. They roll back all changes so no partial update remains.
B. They automatically retry the failed operation endlessly.
C. They save the deducted amount and ignore the addition.
D. They allow partial updates to keep data moving.
Solution
Step 1: Understand partial failure risk in money transfer
If money is deducted but not added, data becomes inconsistent and incorrect.
Step 2: Explain transaction rollback mechanism
Transactions ensure either all steps succeed or all fail. On crash, rollback undoes partial changes.
Step 3: Connect rollback to data consistency
Rollback prevents partial updates, keeping data accurate and consistent after failure.
Final Answer:
They roll back all changes so no partial update remains. -> Option A
Quick Check:
Rollback = no partial updates [OK]
Hint: Rollback undoes partial changes after failure [OK]
Common Mistakes:
Thinking partial updates are allowed
Believing system retries endlessly automatically
Assuming partial data is saved anyway
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.