What is a Transaction in DBMS?
A transaction in the context of a Database Management System (DBMS) is a logical unit of work that performs one or more operations (such as INSERT
, UPDATE
, DELETE
, or SELECT
) on a database. These operations are grouped together to ensure that they are executed as a single, indivisible unit, which either completes fully (commit) or fails entirely (rollback). This ensures data consistency, integrity, and reliability.
Key Properties of Transactions (ACID Properties)
- Atomicity:
- A transaction is treated as a single unit.
- Either all operations within the transaction are executed successfully or none are.
- Example: In a bank transfer, both debit from one account and credit to another must occur together; otherwise, neither occurs.
- Consistency:
- Ensures that the database transitions from one valid state to another.
- All integrity constraints and rules must remain valid after a transaction.
- Example: After a stock purchase, the total available shares remain constant.
- Isolation:
- Transactions are executed independently of one another.
- Concurrent transactions do not interfere with each other.
- Example: While one transaction updates a customer's balance, another cannot see the intermediate state.
- Durability:
- Once a transaction is committed, the changes are permanent, even in case of a system crash.
- Example: After transferring funds, the database reflects the transaction even after a power failure.
States of a Transaction
A transaction progresses through the following states:
- Active: The transaction is in progress.
- Partially Committed: All operations have been executed, but changes are not yet permanent.
- Committed: The transaction successfully completes, and changes are saved permanently.
- Failed: An error occurs, and the transaction cannot proceed further.
- Aborted: Changes made during the transaction are rolled back, restoring the database to its original state.