BEGIN TRANSACTION syntax in SQL - Time & Space Complexity
We want to understand how the time to run a transaction changes as the amount of work inside it grows.
How does the number of operations inside a transaction affect the total time?
Analyze the time complexity of the following SQL transaction.
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
This code moves money between two accounts inside a transaction to keep data safe.
Look for repeated actions that take time.
- Primary operation: Two UPDATE statements inside the transaction.
- How many times: Each UPDATE runs once per transaction in this example.
As the number of UPDATE statements inside the transaction grows, the total work grows too.
| Input Size (number of UPDATEs) | Approx. Operations |
|---|---|
| 2 | 2 UPDATE operations |
| 10 | 10 UPDATE operations |
| 100 | 100 UPDATE operations |
Pattern observation: The total time grows roughly in direct proportion to the number of statements inside the transaction.
Time Complexity: O(n)
This means the time to complete the transaction grows linearly with the number of operations inside it.
[X] Wrong: "A transaction always takes the same time no matter how many operations it has."
[OK] Correct: More operations inside a transaction mean more work, so the time grows with the number of operations.
Understanding how transaction time grows helps you write efficient database code and explain your reasoning clearly in interviews.
"What if we added a loop that runs 100 UPDATE statements inside one transaction? How would the time complexity change?"
Practice
BEGIN TRANSACTION in SQL?Solution
Step 1: Understand what
This command starts a transaction, grouping multiple operations so they can be committed or rolled back as one.BEGIN TRANSACTIONdoesStep 2: Compare other options
Deleting data, creating databases, or displaying records are unrelated to starting transactions.Final Answer:
To start a group of database operations that can be saved or undone together -> Option BQuick Check:
BEGIN TRANSACTION = start safe group [OK]
- Confusing transaction start with data deletion
- Thinking it creates databases
- Assuming it shows data
Solution
Step 1: Recall standard SQL syntax for starting transactions
The correct syntax to start a transaction isBEGIN TRANSACTION;.Step 2: Check other options for correctness
START TRANSACTION;is valid in some systems but not standard SQL;BEGIN;alone is incomplete;TRANSACTION START;is invalid syntax.Final Answer:
BEGIN TRANSACTION; -> Option AQuick Check:
Correct syntax = BEGIN TRANSACTION; [OK]
- Using incomplete 'BEGIN;' without TRANSACTION
- Confusing order of words
- Using non-standard phrases
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT;
What is the effect of this transaction?
Solution
Step 1: Analyze the transaction block
The two updates are inside a transaction started byBEGIN TRANSACTION;and ended byCOMMIT;.Step 2: Understand transaction behavior
Both updates will be saved together if no errors occur; if an error happens, none will be saved.Final Answer:
Both updates happen together or not at all -> Option CQuick Check:
Transaction = all or nothing [OK]
- Thinking updates save separately
- Assuming immediate save without commit
- Believing transaction causes error here
BEGIN TRANSACTION
INSERT INTO users (name) VALUES ('Alice');
COMMIT;Solution
Step 1: Check syntax of each statement
BEGIN TRANSACTIONis missing a semicolon at the end, which is required to separate statements.Step 2: Verify order and validity of other statements
COMMIT;correctly comes after the insert; the insert syntax is valid.Final Answer:
Missing semicolon after BEGIN TRANSACTION -> Option DQuick Check:
Semicolon needed after BEGIN TRANSACTION [OK]
- Forgetting semicolon after BEGIN TRANSACTION
- Misordering COMMIT and INSERT
- Assuming insert syntax is wrong
Solution
Step 1: Understand the need for atomic transfer
Both debit and credit updates must happen together or not at all to keep balances consistent.Step 2: Evaluate each option
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;
usesBEGIN TRANSACTIONandCOMMITto group both updates safely.UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2;
lacks transaction, risking partial update.BEGIN TRANSACTION; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;
only credits without debit.BEGIN; UPDATE accounts SET balance = balance - 200 WHERE id = 1; ROLLBACK;
rolls back, so no change occurs.Final Answer:
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;
-> Option AQuick Check:
Atomic transfer needs BEGIN TRANSACTION + COMMIT [OK]
- Not grouping both updates in a transaction
- Forgetting COMMIT to save changes
- Rolling back changes unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
