As discussed in the previous post, a transaction in DBMS can be defined as a program which groups various operations to be performed on the database and executes this collection as a single unit.
The execution of the transaction may or may not change the contents of the database. The primary goals of a transaction must be to ensure the consistency of the database and maintaining the integrity constraints as well.
Categories of a Transaction in DBMS
A transaction in DBMS may fall under any of these three categories. These categories are:
- Retrieval Transactions: The transactions which are used only for retrieving data from a database for either viewing of data or for creating some sort of reports are called Retrieval Transactions.
- Update Transactions:All transactions that are used to add new data into the database or are used to update existing data of the database are known as Update Transactions.
- Mixed Transactions:The transactions which do some amount of retrieval as well as some sort of update in the database lie in the category of Mixed Transactions. These types of transactions are used in complex applications.
Transaction – An Example
To understand transactions in a better way, let us take an example of a transaction we usually do in our day to day lives. Say you pay your electricity bill online from your account to XYZ Power Corporation.
If you take a close look here, when you would have made the payment then a number of steps would have taken place before the amount was deducted from your account and was settled in the account of the power house. Let us have a look at those operations that would have taken place.
Operations in Transaction
The following operations will be performed in the above example:
- Subtracting the amount of electricity bill (say Rs 10,000) from your account. This would be an UPDATE operation on your account.
- Adding the amount to the power house’s account. This will initiate another UPDATE operation but on the account of the associated power house.
Both these UPDATE operations form the part of a single transaction. So, the success or failure of the entire transaction will depend on the success or failure of these individual operations of the transaction.
If both the operations are done successfully, then the transaction becomes a Committed transaction and all the changes made to the data in the database are reflected successfully. But, if either one of these fails, then the entire transaction fails and the data has to be rolled back to the original consistent state.
Hence the success or failure of the entire transaction depends on the set of operations the transaction is performing.
READ / WRITE Operations in Transaction in DBMS
Whenever the data in the database is to be changed, the following operations need to be performed:
- Fetch the data from the database stored on the RAM. This will require a READ operation to be performed.
- Make the necessary changes to the data in the database and save it back on the RAM. This step requires a WRITE operation to be performed.
Why READ / WRITE operations are needed?
Database’s store useful data on disks i.e., RAM. Whenever any transaction in DBMS takes place on the stored data, then in order to reduce the access time the data on the disk is read into a small portion of the memory called the buffers. This is done with the help of READ operation.
Here, all the changes to the data are made by performing various operations of the transaction. Once all the changes have been made successfully, then this data is to be recorded back on the disks. This is done with the help of the WRITE operation. The WRITE operation transfers the data to the disk either immediately or at some later point in time.
Access to the database is made using two basic operations. These are:
- READ (X)
- This operation transfers X data from storage device to buffer.
- WRITE (X)
- This operation transfers X data from the buffer to the database.