Transactions are the backbone of data integrity in SQL Server. They ensure your data stays accurate even when operations fail, systems crash, or multiple users modify data at the same time.
But when should we explicitly use them? What happens if we don’t? How does SQL Server behave internally?
This 360-degree guide explains SQL transactions through:
-
Auto-commit vs Explicit transactions
-
Meaningful real examples
-
Errors + rollback scenarios
-
Flowcharts and diagrams
At the end, you’ll be able to confidently decide when, why, and how to use transactions.
🔵 1. Understanding SQL Transaction Modes
SQL Server supports three modes:
In this article, we will focus on:
-
Auto-commit
-
Explicit transactions
These are the modes used 99% of the time in real systems.
🔵 2. Auto-Commit Mode (Default Mode)
Every INSERT, UPDATE, or DELETE is automatically committed.
Let’s create two simple tables:
Insert example data:
✔ Output
| Table | Amount |
|---|---|
| Savings | 100 |
| Checking | 25 |
Auto-commit is simple — you issue the statement, SQL Server commits it.
🔵 3. The Problem With Auto-Commit (Real Example)
We now simulate a small real banking operation:
Attempt to move ₹100 from Savings → Checking
❌ The second insert fails
(because the message is too long or violates a rule)
But the first insert already succeeded, and auto-commit cannot undo it.
SELECT SUM(Amount) AS [Total], -- SELECT Statement
'Savings' AS [AccountType]
FROM dbo.Savings
UNION ALL
SELECT SUM(Amount) AS [Total],
'Checking' AS [AccountType]
FROM dbo.Checking;
GO
✔ Result: Bad Data
| ID | Total |
|---|---|
| 1 | 0.00 ❌ (should not happen) |
| 2 | 25.00 |
This is the main weakness of auto-commit.
🔵 4. Explicit Transactions: The Fix
Explicit transactions allow us to group multiple steps into one logical unit.
Syntax:
🔵 5. Real Example: Fixing the Savings → Checking Transfer
We run the exact same operation using a transaction:
When we use XACT_ABORT ON keyword, we no need write rollback, it automatically rollback the transaction when it fails, below is the code
✔ Behaviour
If any statement fails, SQL Server automatically:
-
Rolls back
-
Restores all data
-
Maintains integrity
SELECT SUM(Amount) AS [Total], -- SELECT Statement
'Savings' AS [AccountType]
FROM dbo.Savings
UNION ALL
SELECT SUM(Amount) AS [Total],
'Checking' AS [AccountType]
FROM dbo.Checking;
GO✔ Result (Correct)
🔵 6. Using TRY…CATCH for Better Safety
Best practice in SQL Server:
This ensures:
-
Partial inserts don’t occur
-
Errors are logged
-
Data stays consistent
🔵 7. Understanding Deadlocks (Simple Visual Explanation)
A deadlock happens when:
-
Transaction A waits for resources held by B
-
Transaction B waits for resources held by A
Result → SQL Server kills one transaction
🔶 Deadlock Diagram
Real Example
Session A
Session B
💥 Deadlock occurs
✔ Deadlock Fix
Always update tables in the same order:
This prevents circular waits.
🔵 8. Understanding Isolation Levels (With Diagrams)
Isolation levels control how transactions interact.
8.1 READ UNCOMMITTED (Dirty Reads Allowed)
8.2 READ COMMITTED (Default Level – No Dirty Reads)
8.3 REPEATABLE READ (Prevents Non-Repeatable Reads)
8.4 SERIALIZABLE (Strictest — Fully Locked)
🔵 9. Flowchart: How SQL Transaction Works
🔵 10. Putting It All Together (360° View)
✔ When Auto-Commit Is Enough
-
Simple INSERT of one row
-
Logging data
-
Background audit entries
✔ When You MUST Use Explicit Transactions
-
Multi-step financial operations
-
Inventory deductions
-
Any operation affecting multiple tables
-
Critical business logic
-
Data migrations
✔ Why TRY…CATCH Matters
-
Prevents partial changes
-
Enables clean rollback
-
Improves reliability
✔ Why XACT_ABORT Is Useful
-
Automatically rolls back on runtime errors
-
Prevents stuck transactions
✔ Deadlock Awareness
-
Happens naturally under concurrency
-
Ordering and proper indexing greatly reduce them
✔ Isolation Levels Matter
-
They control concurrency vs. accuracy
-
Choose based on system needs
⭐ Final Conclusion
SQL transactions are not just a feature — they are a safety system.
They ensure:
-
Accuracy
-
Consistency
-
Reliability
-
Protection from partial failures
Understanding auto-commit vs explicit, using TRY…CATCH, handling deadlocks, and choosing the right isolation level gives you complete control over data integrity.
You now have a full 360-degree understanding of SQL transactions.
0 comments:
Post a Comment