Understanding Transactions in Databases

Understanding Transactions in Databases

Transactions in Databases

A transaction in the context of databases is a collection of logically connected actions that cooperate to accomplish a particular goal. These operations are grouped in such a way that they function as a single unit of work.

In simple terms, a transaction is a series of actions performed by a single user or process to access or modify data in a database.

Machine Learning Tutorial:–Click Here
Data Science Tutorial:-Click Here
Complete Advance AI topics:- CLICK HERE
Deep Learning Tutorial:- Click Here

Real-World Example

Consider a scenario where a bank employee transfers ₹800 from Account X to Account Y. Although this might seem like a small action, it involves several low-level steps behind the scenes:

For X’s Account:

  1. Open_Account(X)
  2. Old_Balance = X.balance
  3. New_Balance = Old_Balance - 800
  4. X.balance = New_Balance
  5. Close_Account(X)

For Y’s Account:

  1. Open_Account(Y)
  2. Old_Balance = Y.balance
  3. New_Balance = Old_Balance + 800
  4. Y.balance = New_Balance
  5. Close_Account(Y)

Core Operations in a Transaction

A transaction is built on two fundamental operations:

  • Read(X) – briefly saves the value of X in the main memory buffer after retrieving it from the database.
  • Write(X) – adds the value of X from the buffer to the database.

Example: Debit Operation

Let’s say we need to deduct ₹500 from an account with an initial balance of ₹4,000. The operations would look like this:

  1. R(X) → Read X’s value (4,000) from the database into a buffer.
  2. X = X - 500 → Update buffer value to 3,500.
  3. W(X) → Write updated value (3,500) back to the database.

At the end of the transaction, the account balance becomes ₹3,500.

What If a Transaction Fails?

In reality, transactions can fail due to hardware issues, software errors, or power outages. If the above example failed after step 2, the database would still show ₹4,000—making the operation incomplete and invalid from a business perspective.

Ensuring Reliability: Commit & Rollback

To maintain data integrity, databases provide two crucial operations:

  • Commit –records every modification made throughout the transaction for all time.
  • Rollback – Reverses any changes made during the transaction, restoring the data to its previous state.

By ensuring that transactions adhere to the ACID properties (Atomicity, Consistency, Isolation, Durability), these procedures maintain the accuracy and dependability of databases.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

Download New Real Time Projects :–Click here


what is transaction in database with example
properties of transaction in dbms
transaction management in dbms
acid properties in dbms
transaction states in dbms
database transaction sql
acid properties of transaction
database transaction vs query
understanding transactions in databases geeksforgeeks

    Share this content:

    Post Comment