Appearance
Read Uncommitted
Read Uncommitted is the lowest isolation level for transactions. At this level, a transaction can read data that has been updated by another transaction but not yet committed. If the other transaction rolls back, the current transaction ends up reading dirty data, leading to what is known as a dirty read.
Example
Let's consider a simple example with the students
table, which contains only one record:
sql
mysql> SELECT * FROM students;
+----+-------+
| id | name |
+----+-------+
| 1 | Alice |
+----+-------+
1 row in set (0.00 sec)
Next, we open two MySQL client connections and sequentially execute Transaction A and Transaction B:
Time | Transaction A | Transaction B |
---|---|---|
1 | SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; | SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; |
2 | BEGIN; | BEGIN; |
3 | UPDATE students SET name = 'Bob' WHERE id = 1; | |
4 | SELECT * FROM students WHERE id = 1; | |
5 | ROLLBACK; | |
6 | SELECT * FROM students WHERE id = 1; | |
7 | COMMIT; |
At step 3, Transaction A updates the record with id=1
but has not yet committed. At step 4, Transaction B reads the data and sees the uncommitted change (name = 'Bob').
After Transaction A rolls back in step 5, Transaction B reads the record again in step 6. The data read this time will be consistent with the original state (name = 'Alice'), demonstrating a dirty read.
Thus, in the Read Uncommitted isolation level, a transaction can read data updated by another transaction that has not been committed, which may lead to reading dirty data.