Saturday 16 September 2023

Dirty read in SQL

A "dirty read" in SQL refers to a situation where one transaction reads data that has been modified by another transaction but has not yet been committed. In other words, it allows a transaction to read uncommitted changes made by another concurrent transaction. This concept is associated with the lowest level of isolation in the SQL transaction isolation levels, known as "Read Uncommitted."

 Let’s see this example.

Here in the transaction we are updating, inserting and deleting some records but not yet committed this transaction.  

When we are updating this table in transaction this table is locked and when we are trying to fetch data from the table we will not get the data because it is waiting for the commit the transaction.  

This query is still executing. Because SQL is by default reading the committed data.

Let’s use no lock. We will get the uncommitted data. It means we will get the old data.   

Here we got the data. Even though our transaction is not yet commented.  This data is known as Dirty Read or Dirty data.

Let’s  commit the transaction.   

 Transaction committed successfully. Now we can see the updated data.  


No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts