SQL-Transaction-Management-in-DBMS
SQL-Transaction-Management-in-DBMS
SQL-Transaction-Management-in-DBMS
Management in DBMS
SQL Transaction Management: A Comprehensive Guide to Ensuring Data
Integrity and Consistency in Relational Databases
RR by Rajashri Rajan
Introduction to Transactions in Databases
What are Transactions? Why Transactions?
Transactions are a sequence of database operations that are Transactions provide a mechanism for maintaining data
treated as a single, indivisible unit of work. They ensure that consistency and integrity by guaranteeing that data is either
changes to the database are either fully committed or updated correctly or not at all.
completely rolled back.
ACID Properties of
Transactions
Atomicity Consistency
All operations within a Transactions preserve the
transaction are treated as a database's integrity by
single unit. Either all ensuring that any changes
operations are committed, or made by the transaction
none are. comply with the database's
predefined rules and
constraints.
Isolation Durability
Transactions run Once a transaction is
independently of each other, committed, the changes are
preventing concurrent access permanently stored in the
issues by ensuring that the database, even if the system
data seen by one transaction crashes or encounters an
is not affected by other error.
concurrent transactions.
Atomicity: All or Nothing
Example
1
Transfer Funds
2 A transaction involving a bank transfer requires both debiting the
source account and crediting the destination account.
Partial Failure
3 If one operation fails, the transaction is rolled back, ensuring
that the data remains consistent.
Consistency: Maintaining
Data Integrity
Data Integrity Example
Transactions guarantee that If a transaction attempts to
database constraints are not insert a duplicate record into a
violated, such as unique key table with a unique key
constraints or foreign key constraint, the transaction will
relationships. be rolled back to maintain data
integrity.
Isolation: Preventing
Concurrent Access Issues
Repeatable Read
Transactions can read the same
data multiple times and get the
same results, preventing phantom
reads, where new data might
appear or disappear between reads.
Durability: Permanent Storage of Committed
Transactions
Log Files
1 Transactions are written to a log file to ensure that committed changes are persisted even in
case of system crashes or failures.
Recovery Process
The database system uses the log file to restore the
2 database to a consistent state after a crash, applying any
committed transactions and rolling back any uncommitted
transactions.
Transaction States: Active, Committed, Aborted
1 2 3
COMMIT TRANSACTION
ROLLBACK TRANSACTION
Reverts all changes made within the transaction block,
3
effectively undoing the transaction and returning the
database to its state before the transaction began.
SAVEPOINT
1 2
Keep Transactions Short Optimize Concurrency
Reduce the risk of transaction failures by minimizing the Implement proper concurrency control mechanisms to prevent
duration of transactions, reducing the chance of errors or deadlocks and other issues that can arise when multiple
conflicts. transactions access the same data concurrently.
3 4
Use Savepoints Wisely Monitor Transactions
Utilize savepoints to partially roll back a transaction to a Track transaction performance and identify any bottlenecks or
specific point, allowing for greater flexibility and control during inefficiencies that might impact system performance or data
complex operations. integrity.