Transactions in SQL
Posted on Oct 25, 2022
Transactions are a fundamental concept in database management that ensure data integrity and consistency.
What is a Transaction?
A transaction is a sequence of operations performed as a single logical unit of work.
ACID Properties
Atomicity
All operations in a transaction succeed or all fail together.
Consistency
A transaction brings the database from one valid state to another.
Isolation
Concurrent transactions don’t interfere with each other.
Durability
Once committed, changes are permanent.
Transaction Syntax
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
If something goes wrong:
ROLLBACK;
Understanding transactions is essential for building reliable applications!