Transactions in DBMS
β± Estimated reading time: 3 min
A Transaction in DBMS is a sequence of one or more SQL operations that are executed as a single logical unit of work. A transaction must either be fully completed or not executed at all. It ensures that the database remains consistent even in the presence of errors, system failures, or concurrent access.
Typical operations inside a transaction include:
-
INSERT
-
UPDATE
-
DELETE
For example, transferring money between two bank accounts involves:
-
Deducting amount from Account A
-
Adding amount to Account B
These steps must be treated as a single transaction.
Properties of a Transaction β ACID Properties
The four ACID properties guarantee reliability and consistency.
1οΈβ£ Atomicity
-
All or nothing property
-
Ensures entire transaction is completed.
-
If any part fails, the whole transaction is rolled back.
Example:
If money is deducted from Account A but not added to B, entire transaction must be cancelled.
2οΈβ£ Consistency
-
Transaction must take the database from one valid state to another valid state.
-
Ensures rules, constraints, and triggers are not violated.
3οΈβ£ Isolation
-
Multiple transactions can occur at the same time without interfering with each other.
-
Intermediate results of one transaction should be hidden from others.
Example:
Two users withdrawing money simultaneously should not see inconsistent balance.
4οΈβ£ Durability
-
Once a transaction is committed, its changes are permanentβeven if system crashes.
-
DBMS stores changes to non-volatile memory.
Transaction States
A transaction passes through several states:
-
Active β Execution in progress
-
Partially Committed β Final statement executed
-
Committed β Successful completion, changes saved
-
Failed β Error or system failure
-
Aborted β Transaction rolled back
-
Terminated β Transaction completed (either committed or aborted)
Β Control Commands Used in Transactions
1οΈβ£ COMMIT
Saves all changes permanently.
2οΈβ£ ROLLBACK
Undo changes made in the current transaction.
3οΈβ£ SAVEPOINT
Creates a checkpoint inside a transaction.
Problems Prevented by Transactions
Transactions help avoid anomalies in concurrent environments:
β Dirty Read
Reading uncommitted data from another transaction.
β Lost Update
Two transactions overwrite each other's data.
β Non-repeatable Read
Data changes during a transaction when read multiple times.
β Phantom Read
New rows appear in the result of a repeated query.
Schedules in Transactions
β Serial Schedule
Transactions execute one after another.
Safe but slow.
β Non-Serial Schedule
Transactions interleave.
Efficient but require checks for consistency.
Why Transactions Are Important?
-
Maintain data consistency
-
Provide fault tolerance
-
Handle system crashes safely
-
Ensure reliable multi-user access
-
Prevent data corruption
Conclusion
Transactions are a fundamental concept in DBMS to ensure data reliability and integrity. Using ACID properties, transactions make sure that even in the presence of system failures, power outages, or concurrent users, the database remains stable, consistent, and correct. They are essential for applications such as banking, e-commerce, reservation systems, and any real-time multi-user environment.
Register Now
Share this Post
β Back to Tutorials