The lock is a mechanism in MySQL to restrict unauthorized access to a personal table. MYSQL allows us to incorporate table locks and are of many types for various types of locks. MYSQL also prevents unauthorized modifications to the table when in a locked state.
- This type of lock allows users to only read the data from a table and prevent any other activities outside of it.
- MYSQL allows multiple numbers of sessions to acquire a READ lock for the table, and all the other sessions can read the table without even gaining the lock.
- If a session has a READ lock on a table, then a user cannot perform any write operation over the table during that session.
- As the name suggests, READ can only allow us to read the table.
- All the other sessions that do not acquire the READ lock are also not able to write data onto the table without releasing the READ lock.
- Write operations are in a hold state until the lock has been released.
Let us use this rec table and lock the table.
Here is the table. Now let us lock it.
LOCK TABLE rec READ;
Now that the table is locked, it should not let us write anything on it.
Let us try to insert data onto it.
INSERT INTO rec(ID, Name, Email) VALUES (1, 'mas', 'firstname.lastname@example.org')
We can see that it won't allow us to write anything onto it.
UNLOCK TABLES are used to unlock locked tables and revert to the old state.