What is a transaction in Database?

Database transaction interview questions covered in this section:
What is a Transaction?
What are the different states of Transaction?
What are the main properties of Transaction?
What are the advantages of a Transaction?
Do all storage engines support transaction?
What are the ACID properties of a database system?

Transaction

An independent and logical unit of work that is performed to complete read and write operations on a database and in an RDBMS is known as Transaction. A Transaction may contain several sub-tasks and after a complete transaction, the overall data remain consistent.

Let’s take an example of a Banking System where Money is transferred from one account to another.

Steps :
Step 1: Account A has amount = a
Step 2: Account B has an amount = b
Step 3: The total amount that A and B have combined = (a+b)
Step 4: Now A transfers 1000 rupees to B.
Step 5: So After transaction A has amount = a-1000
Step 6: and After transaction B has amount = b+1000
and after transaction combined amount that A and B have is
= (a-1000) + (b+1000)
= (a+b) and this the amount total that A and B have before the transaction.

States of Transaction

A transaction in its life cycle may have in different states at different time. There are the following states that a transaction may have:

  1. Active
  2. Partially Committed
  3. Failed
  4. Aborted
  5. Committed

states-of-transactions

Active

When a transaction starts it is in Active state. The very initial stage of a transaction.

Partially committed

After initialization operation starts and the state is known as partially committed. The partial word is added because the operation is not yet completed and the transaction does not reflect the data manipulation results.

Failed

A transaction is failed by many reasons like any checks made by the database system, fails OR Database systems fails or system crashed. In such cases transaction is called failed and can not proceed further.

Aborted

When any checks fail then It is necessary to abort the transaction and kill it. In this phase, the changes made by write queries undone and database is backed to initial state.

Committed

Committed is a state when all the operations in a transaction completed and unit of the task done completely. After commit, changes can not be undone and can be reflected in the result.

ACID Properties

A single independent logical unit in a database or a transaction need to maintain consistency during processing. So there are some standard and norms that need to follow by these transactions and these are the mandatory features of any transactions. These features are known as ACID properties. ACID means a system must maintain Atomicity, Consistency, Isolation, and Durability.

Atomicity

There is no intermediate state between all and none. A transaction is an atomic unit and either it’s executed all the operations or none of them executed that means either the whole transactions take place or none of them happened. If some of them executed and the transaction fails then in partially committed condition then Roll Back occurs and all the parts done operations, undone.

Consistency

Let’s say that the amount of data must be consistent before and after any transaction. This is very important to maintain the integrity of data during a transaction. An example given above can explain the transaction.

Isolation

A transaction is an independent thread of all the other transactions. Failure of success of any transaction does not affect the presence of any other transaction. There may be many transactions are executing at a time. While multiple transactions are executing on a system the integrity of data is maintained.

Durability

After successful completion of any transaction, the data is saved to disk and should be persistent. The changes become permanent after a transaction committed. Even after system failures and several restarts.

Dhananjay

Dhananjay

I’m Dhananjay — a free-spirit with a passion for technologies and blogging.

View all posts by Dhananjay →

4 thoughts on “What is a transaction in Database?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.