Open In App

Dirty Read in SQL

Pre-Requisite – Types of Schedules, Transaction Isolation Levels in DBMS

A Dirty Read in SQL occurs when a transaction reads data that has been modified by another transaction, but not yet committed. In other words, a transaction reads uncommitted data from another transaction, which can lead to incorrect or inconsistent results.



This situation can occur when a transaction modifies a data item and then fails to commit the changes due to a system failure, network error, or other issue. If another transaction reads the modified data before the first transaction has a chance to commit, it can lead to a dirty read.

For example, consider two transactions T1 and T2. T1 modifies a row in a table and starts a transaction, but does not commit it. Meanwhile, T2 tries to read the same row before T1 commits its changes. If T2 is allowed to read the uncommitted data, it will result in a dirty read, potentially leading to incorrect or inconsistent results.



To prevent dirty reads, SQL provides transaction isolation levels, which specify how transactions should be isolated from one another. The isolation levels include:

Example: Table Record

ID

Cus_Name

Balance

1

S Adam

100

2

Zee Young

150

Transaction: Transfer 10 from S Adam’s account to Zee Young’s account.

Input:
BEGIN TRY
BEGIN TRANSACTION
UPDATE Table SET Balance = Balance - 10 WHERE ID=1;
UPDATE Table SET Balance = Balance + 10 WHERE ID='C';
COMMIT TRANSACTION
PRINT 'Committed'
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
PRINT 'Not Committed'
END CATCH

Output:
Not committed

By executing the above query the output will be “Not committed” because there is an error, there is no ID=C. So at that time if we want to execute another transaction with that row time Dirty Reads occur. There is no partial commitment if both the UPDATE query succeeds only then the output will be “Committed”. Before Execution.

Table Record

ID

Cus_Name

Balance

1

S Adam

100

2

Zee Young

150

After Execution:

Input:
BEGIN TRY
BEGIN TRANSACTION
UPDATE Table SET Balance = Balance - 10 WHERE ID=1;
UPDATE Table SET Balance = Balance + 10 WHERE ID='C';
COMMIT TRANSACTION
PRINT 'Committed'
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
PRINT 'Not Committed'
END CATCH
Output:
(1row affected)
(0row affected)
Not Committed

Note that if we put a valid ID first transaction result will come out as committed or 1 row affected but the 2nd one will not be affected.

Explanation: If we have a ticket booking system and One Customer is trying to book a ticket at that time an available number of the ticket is 10, before completing the payment, the Second Customer wants to book a ticket at that time this 2nd transaction will show the second customer that the number of the available tickets is 9. The twist is here if the first customer does not have sufficient funds in his debit card or in his wallet then the 1st transaction will Rollback, At that time 9 seats are available which is read by the 2nd transaction is Dirty Read

Example: Available ticket: For 1st customer

Step 1:

Input: Transaction 1
Select *from Bus_ticket;

Output:

ID

Bus_Name

Available_Seat

1

KA0017

10

Step 2: Booking time for 1st customer

Input:  
--Transaction 1
BEING Transaction
UPDATE Bus_Ticket set Available_Seat=9
WHERE ID=1

--Payment for Transaction 1
Waitfor Delay '00.00.30'
Rollback transaction

Available ticket: For the 2nd customer while the 1st customer is paying for the ticket.

Step 3: Input

Transaction 1

set transaction isolation level read uncommitted
Select *from Bus_ticket where ID=1;

Output:

ID

Bus_Name

Available_Seat

1

KA0017

9

Note that during the payment of the 1st customer’s 2nd transaction read 9 seat is available if somehow the 1st transaction Rollback then available seat 9 is Dirty read data. After the rollback of the 1st transaction available seat is 10 again. 2nd and 3rd steps happening at the same time. Actually available seat after rollback of transaction 1.

ID

Bus_Name

Available_Seat

1

KA0017

10

Advantages of Dirty Read

Disadvantages of Dirty Read

FAQs on Dirty Read in SQL

1. What is Dirty Read?

Answer:

If a transaction reads any modified data before the first transaction has been committed, then it can lead to Dirty Read.

2. How Dirty Read can be prevented?

Answer:

Dirty Read can be prevented by making the schedule serializable.


Article Tags :