How Transactions Work in SQL (ACID)

πŸ’‘ Concept Name

SQL Transactions are logical units of work made up of one or more SQL statements that must all succeed or fail as a group, governed by ACID properties.

πŸ“˜ Quick Intro

Transactions ensure that database operations are performed reliably. SQL uses ACID propertiesβ€”Atomicity, Consistency, Isolation, and Durabilityβ€”to maintain integrity during operations like updates, inserts, or deletes.

🧠 Analogy / Short Story

Think of a bank transfer: you withdraw money from Account A and deposit it into Account B. If one step fails, the transaction is rolled back. SQL transactions ensure both steps happen together or not at all, just like undoing a failed money transfer.

πŸ”§ Technical Explanation

  • BEGIN TRANSACTION marks the start of a transaction block.
  • COMMIT saves all changes permanently to the database.
  • ROLLBACK undoes all changes since the last BEGIN TRANSACTION.
  • Atomicity: All or nothing execution.
  • Consistency: Maintains data integrity before and after.
  • Isolation: Prevents interference from other concurrent transactions.
  • Durability: Changes remain even after crashes once committed.

🎯 Purpose & Use Case

  • βœ… Group multiple related changes into one atomic operation.
  • βœ… Prevent partial updates that can corrupt data.
  • βœ… Ensure consistent state in concurrent user environments.
  • βœ… Handle errors gracefully with rollback support.

πŸ’» Real Code Example

BEGIN TRANSACTION;

UPDATE Accounts SET Balance = Balance - 500 WHERE AccountID = 1;
UPDATE Accounts SET Balance = Balance + 500 WHERE AccountID = 2;

IF @ERROR <> 0
    ROLLBACK;
ELSE
    COMMIT;

❓ Interview Q&A

Q1: What is a transaction in SQL?
A: A group of SQL statements executed as a single unit of work that must either all succeed or all fail.

Q2: What are ACID properties?
A: Atomicity, Consistency, Isolation, and Durability β€” the key principles that ensure safe transaction processing.

Q3: What is the use of ROLLBACK?
A: It undoes all changes made during the transaction if any part fails.

Q4: What ensures data integrity across transactions?
A: The Consistency property of ACID ensures valid data before and after the transaction.

Q5: How does Isolation help in transactions?
A: It prevents one transaction’s intermediate results from being visible to others.

Q6: Can transactions be nested in SQL Server?
A: Yes, but only the outermost COMMIT completes the transaction.

Q7: What is the default isolation level in SQL Server?
A: READ COMMITTED.

Q8: What happens if a COMMIT is missed?
A: The transaction remains open, locking resources, or may be rolled back on disconnect.

Q9: What is the role of @ERROR?
A: It checks for errors after SQL statements to conditionally commit or rollback.

Q10: Is durability guaranteed during a crash?
A: Yes, once COMMIT is executed, changes persist even after a crash, thanks to logging.

πŸ“ MCQs

Q1. What does BEGIN TRANSACTION do?

  • Commits changes
  • Starts a new transaction block
  • Ends a session
  • Creates a backup

Q2. Which ACID property ensures all steps succeed or none?

  • Consistency
  • Atomicity
  • Durability
  • Concurrency

Q3. What does ROLLBACK do?

  • Saves changes
  • Validates schema
  • Undoes all changes in the current transaction
  • Refreshes indexes

Q4. Which SQL statement makes a transaction permanent?

  • ROLLBACK
  • BEGIN
  • SAVEPOINT
  • COMMIT

Q5. Which is not part of ACID?

  • Atomicity
  • Durability
  • Flexibility
  • Isolation

Q6. Which property ensures data is valid before and after transaction?

  • Isolation
  • Atomicity
  • Consistency
  • Rollback

Q7. What prevents dirty reads in SQL Server?

  • NOLOCK
  • AUTO COMMIT
  • READ COMMITTED isolation level
  • UNCOMMITTED

Q8. What ensures committed changes survive a crash?

  • Atomicity
  • Durability
  • Isolation
  • Logging

Q9. Which T-SQL keyword ends a transaction successfully?

  • END
  • ROLLBACK
  • COMMIT
  • RELEASE

Q10. Which of the following supports concurrent access safely?

  • Atomicity
  • Commit
  • Isolation
  • DROP

πŸ’‘ Bonus Insight

Using transactions strategically helps prevent data corruption, especially during batch operations or complex workflows. Always handle exceptions and log errors to support reliable rollbacks and audits in production systems.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

Learn More on SQL Topics

SQL Joins Explained: INNER, LEFT, RIGHT, FULL πŸ‘‰ Explained
SQL Subqueries and Nested Queries Explained πŸ‘‰ Explained
SQL Aggregate Functions: COUNT, SUM, AVG, MIN, MAX πŸ‘‰ Explained
Understanding SQL GROUP BY and ORDER BY πŸ‘‰ Explained
Difference Between DELETE, TRUNCATE and DROP πŸ‘‰ Explained
SQL Views: Use Cases and Performance Tips πŸ‘‰ Explained
Common Table Expressions (CTE) in SQL πŸ‘‰ Explained
SQL Window Functions and OVER Clause πŸ‘‰ Explained
SQL Indexes: Clustered vs Non-Clustered πŸ‘‰ Explained
SQL Normalization and Denormalization πŸ‘‰ Explained
SQL Constraints: NOT NULL, UNIQUE, CHECK, DEFAULT πŸ‘‰ Explained
SQL Injection and How to Prevent It πŸ‘‰ Explained
Primary Key vs Foreign Key in SQL πŸ‘‰ Explained
Data Types in SQL: INT, VARCHAR, DATE and more πŸ‘‰ Explained
How to Use CASE Statement in SQL πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup