GATE CSEIT/Database/. Transaction in Database ( With basic introduction).
Contents
Transaction in Database Management System (DBMS)
Introduction to Transactions
A transaction in a Database Management System (DBMS) is a sequence of one or more database operations that are executed as a single logical unit of work. Transactions are used to maintain data integrity, consistency, and reliability in a database.
For example, in a banking system, transferring money from one account to another involves multiple operations:
-
Deducting money from Account A.
-
Adding money to Account B.
If any of these operations fail, the entire transaction must be rolled back to maintain consistency.
Properties of Transactions (ACID Properties)
Transactions in a database follow ACID properties:
-
Atomicity – A transaction is either fully completed or fully rolled back.
-
Consistency – The database remains in a valid state before and after the transaction.
-
Isolation – Transactions are executed independently without interfering with each other.
-
Durability – Once a transaction is committed, the changes are permanent, even if a system failure occurs.
States of a Transaction
A transaction goes through different states during execution:
-
Active – The transaction starts execution.
-
Partially Committed – All operations are performed, but changes are not yet saved.
-
Committed – Changes are permanently stored in the database.
-
Failed – Transaction execution is interrupted due to an error.
-
Aborted – Transaction is rolled back to maintain consistency.
Types of Transactions
-
Read-Only Transaction – Retrieves data without modifying it.
-
Read-Write Transaction – Reads data, modifies it, and stores the changes.
Transaction Control Commands in SQL
-
COMMIT – Saves all changes made by a transaction permanently.
-
ROLLBACK – Undoes all changes made by a transaction.
-
SAVEPOINT – Creates a checkpoint within a transaction to rollback to a specific point.
-
SET TRANSACTION – Defines the properties of a transaction (e.g., isolation level).
Conclusion
Transactions play a crucial role in maintaining database integrity and consistency. By following ACID properties and using transaction control commands, databases ensure that operations are reliable and error-free.