The storage engine tables supported by MySQL database perform table-level locking. Use lock tables to lock for the current thread, and unlock tables to release any locks obtained by the current thread. The following uses a specific example to illustrate the usage of lock and unlock. The specific operations are as follows:
1. The first step is to create the database table writer and view the table structure. Use SQL statement:
create table writer( wid int(10), wno int(10), wname varchar(20), wsex varchar(2), wage int(2) ); desc writer;
As shown below:
2. In the second step, insert five pieces of data into the database table writer, After inserting, check the data in the table, as shown in the following figure:
3. The third step is to use the lock statement to lock the database table writer and use the SQL statement :
lock table writer read;
Make the database table read-only and not write
As shown in the figure below:
4. The fourth step, in order to verify the locking effect, you can view the database table data and use the SQL statement:
select * from writer;
As shown below:
5. The fifth step is to use the update statement to update id=5. The SQL statement is:
update writer set wname = '胡思思' where id = 5;
As shown in the figure below:
6. The sixth step is to use unlock to unlock. The SQL statement is:
unlock tables;
As shown in the figure below:
Notes
Proficient in using the lock and unlock commands
Understand the MySQL database locking and unlocking threads
The above is the detailed content of How to lock and unlock database tables in MySQL database?. For more information, please follow other related articles on the PHP Chinese website!