After testing, I found that the following situations will cause table locks
1.like
2.update, delete unconditional operations
3.update,delete are conditional operations but not primary key operations
Select will not lock the table whether it is conditional or unconditional, whether the condition is primary key or index, Is this correct? Is there something wrong with my test?
select can specify various levels of locks such as shared locks, exclusive locks, etc., such as
select ... FOR UPDATE
.As for why locks are needed, here is a simple example. For example, if you have a document that inherits from the previous document, you can choose to add a read lock and lock the previous document to prevent others from modifying the previous document before you submit it. , causing data inconsistency.
As long as the condition does not contain the primary key, or contains the primary key but is not an equal sign or IN, the entire table will be locked, which includes all three situations in the question.
Whether it is locking the entire table or locking a few rows, select adds read locks, and update and delete add write locks. As for what read locks and write locks are, you can Google them. As for the details, you can also look at theTransaction Isolation Level, which I won’t go into here.