Definition of Transaction in Database


A transaction consists of a series of commands executed in the database. Each command in a transaction is atomic i.e it cannot be split further into sub commands. Any command in the transaction may or may not change the structure of the database.Also, the changes required by the transaction must be made together. If this rule is not implemented, data might be lost if the system fails, power is lost or for some other reason.

A simple example of a transaction is as follows −

Harry needs to transfer Rs 100 from his account to Sally’s. This is done as a transaction. First, Harry’s account details are read and his balance is reduced by 100. This new data is saved back in Harry’s account. Next, Sally’s account details are read and her balance is increased by 100. This new data is saved back in Sally’s account.

However, it is complicated to implement a transaction in the database. In the above example, if the system crashes after the money is withdrawn from Harry’s account, it will never be added to Sally’s account. So, information is lost and Sally’s account is never updated.

Process of a Transaction

A transaction consists of a series of read and write operations. These are used to read the current value of any object, and to write back the updated value obtained after some calculations.

Read Operation

To read any database object, first it is brought into the main memory from the disk. After that, its value is copied into the required variable.

Write operation

To write any database object, the value in the memory is saved and then stored back into the disk.

Updated on 20-Jun-2020 08:47:21