A: set session transaction isolation level read uncommitted; A: begin A: select * from users where id>11; +----+-----------+-----+ | id | user_name | age | +----+-----------+-----+ | 12 | sam | 28 | +----+-----------+-----+ 1 row in set (0.01 sec) B: set session transaction isolation level read uncommitted; B: begin B: insert into users(user_name,age)values('Jovry-Lee',18); # 未提交. A: select * from users where id > 11; # 此时事务A读取到事务B尚未提交的数据,属于脏读. +----+-----------+-----+ | id | user_name | age | +----+-----------+-----+ | 12 | sam | 28 | | 13 | Jovry-Lee | 18 | +----+-----------+-----+ 2 rows in set (0.00 sec)
3 不可重复读
SQL92中对不可重复读的定义如下:
**不可重复读(nonrepeatable read)**:SQL-transaction T1 reads a row. SQL-transaction T2 then modifies or deletes that row and performs a COMMIT. If T1 then attempts to reread the row, it may receive the modified value or discover that the row has been deleted.
A: set session transaction isolation level read committed; A: begin A: select * from users where id>11; +----+-----------+-----+ | id | user_name | age | +----+-----------+-----+ | 12 | sam | 28 | +----+-----------+-----+ 1 row in set (0.01 sec) B: set session transaction isolation level read committed; B: begin B: insert into users(user_name,age)values('Jovry-Lee',18); B: commit; A: select * from users where id > 11; # 此时事务A两次读取到不同的结果,属于不可重复读. +----+-----------+-----+ | id | user_name | age | +----+-----------+-----+ | 12 | sam | 28 | | 13 | Jovry-Lee | 18 | +----+-----------+-----+ 2 rows in set (0.00 sec)
**幻读(phantom read)**:SQL-transaction T1 reads the set of rows N that satisfy some . SQL-transaction T2 then executes SQL-statements that generate one or more rows that satisfy the used by SQL-transaction T1. If SQL-transaction T1 then repeats the initial read with the same , it obtains a different collection of rows.