Home >Database >Mysql Tutorial >What is the difference between phantom read and non-repeatable read?

What is the difference between phantom read and non-repeatable read?

王林
王林Original
2020-06-05 15:28:019918browse

What is the difference between phantom read and non-repeatable read?

Difference:

Non-repeatable read: Under the same conditions, the value of the read data will change when we read it again Variety.

Phantom reading: Under the same conditions, the number of records read out for the first and second times is different.

Detailed analysis:

1. Non-repeatable reading

Under the same conditions, the value of the read data will be the same when we read it again Something has changed.

Example:

In transaction 1, A reads his salary as 1000, but at this time the operation of transaction 1 has not been completed, and there is another identical read later. operate.

con1 = getConnection();
select salary from employee where employeeName ="A";

In transaction 2, the financial staff modified A’s salary to 2000 and submitted the transaction.

con2 = getConnection();    
update employee set salary = 2000 where employeeName = "A";    
con2.commit();

In transaction 1, when A reads his salary again, the salary becomes 2000.

select salary from employee where employeeName ="A";

The results of two reads in a transaction are inconsistent, resulting in non-repeatable reads.

2. Phantom reading

Under the same conditions, the number of records read out for the first and second times is different.

Example:

There are currently 5 employees with a salary of 1,000.

Transaction 1, read all employees with a salary of 1,000, and read a total of 10 records.

con1 = getConnection();    
Select * from employee where salary =1000;

At this time, another transaction inserted an employee record into the employee table, and the salary was also 1,000

con2 = getConnection();    
Insert into employee(employeeName,salary) values("B",1000);    
con2.commit();

Transaction 1 read all employees with a salary of 1,000 again, and a total of 6 records were read Record, which creates phantom reading.

//con1    
select * from employee where salary =1000;

Recommended tutorial: mysql tutorial

The above is the detailed content of What is the difference between phantom read and non-repeatable read?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn