search
HomeDatabaseMysql TutorialMySQL syntax analysis table locking

This article brings you relevant knowledge about mysql, which mainly introduces the relevant syntax analysis about table locking. MySQL allows client sessions to explicitly acquire table locks to prevent other sessions To access the same table within a specific time period, let’s take a look at it. I hope it will be helpful to everyone.

MySQL syntax analysis table locking

Recommended learning: mysql video tutorial

MySQL table lock

A lock is a sign associated with a table. MySQL allows client sessions to explicitly acquire table locks to prevent other sessions from accessing the same table for a specific period of time. A client session can acquire or release table locks only for itself. It cannot acquire or release table locks for other sessions.

CREATE TABLE tbl (
  id INT NOT NULL AUTO_INCREMENT,
  col INT NOT NULL,
  PRIMARY KEY (id)) Engine = InnoDB;

LOCK and UNLOCK TABLES syntax

The following statements explicitly acquire table locks:

LOCK TABLES table_name [READ | WRITE]

To lock a table, specify its name after the LOCK TABLES keyword. Additionally, you can specify the type of lock, which can be READ or WRITE.

To release the table lock, please use the following statement:

UNLOCK TABLES;

Read lock

READ lock has the following characteristics:

  1. READ can be passed Multiple sessions acquire table locks at the same time. Additionally, other sessions can read data from the table without acquiring a lock.
  2. A session holding a READ lock can only read data in the table, but cannot write. Additionally, other sessions cannot write data to the table until READ releases the lock. Write operations from another session will enter a wait state until the READ lock is released.
  3. If the session terminates normally or abnormally, MySQL will implicitly release all locks. This feature is also related to WRITE lock.

Example:
In the first session, first, connect to the mysqldemo database and use the CONNECTION_ID() function to get the current connection ID, as shown below:

SELECT CONNECTION_ID();

MySQL syntax analysis table locking
Then, insert a new row in the tbl table.

INSERT INTO tbl(col) VALUES(10);

Next, query the data in the tbl table.

SELECT * FROM tbl;

MySQL syntax analysis table locking
Afterwards, to obtain the lock, use the LOCK TABLE statement.

LOCK TABLE tbl READ;

Finally, within the same session, if you try to insert a new row in the tbl table, you will get an error message.

INSERT INTO tbl(col) VALUES(11);

MySQL syntax analysis table locking
So once READ acquires the lock, it cannot write data to the table in the same session.

Check the lock from another session.
First, connect to mysqldemo and check the connection ID:

SELECT CONNECTION_ID();

MySQL syntax analysis table locking
Then, retrieve the data from the tbl table:

SELECT * FROM tbl;

MySQL syntax analysis table locking
Next , insert a new row in the tbl table:

INSERT INTO tbl(col) VALUES(20);

MySQL syntax analysis table locking
The insert operation from the second session is waiting because the first session READ has acquired the lock on the tbl table, But it has not been released yet.

You can view detailed information from SHOW PROCESSLIST.

SHOW PROCESSLIST;

MySQL syntax analysis table locking
After that, return to the first session and use the UNLOCK TABLES statement to release the lock. After READ releases the lock from the first session, INSERT performs the operation in the second session.

unlock tables;

Finally, check the data of the tbl table to see if the INSERT operation of the second session is actually executed.

SELECT * FROM tbl;

MySQL syntax analysis table locking

Write lock

WRITE lock has the following characteristics:

  1. The only session holding the table lock can read from the table Fetch and write data.
  2. Other sessions cannot read data from and write data to the table until the WRITE lock is released.

First, WRITE acquires the lock from the first session.

LOCK TABLE tbl WRITE;

Then, inserting a new row in the tbl table

INSERT INTO tbl(col) VALUES(11);

is useful.

Next, read data from the tbl table.

SELECT * FROM tbl;

MySQL syntax analysis table locking

It works too.

Afterwards, starting from the second session, try to write and read data:

INSERT INTO tbl(col) VALUES(21);
 SELECT * FROM tbl;

MySQL syntax analysis table locking
MySQL syntax analysis table locking

最后,从第一个会话中释放锁定。

UNLOCK TABLES;

看到第二个会话中的所有待处理操作都已执行,下图说明了结果:
MySQL syntax analysis table locking

读锁与写锁

  1. 读锁是“共享”锁,它可以防止正在获取写锁,但不能锁定其他读锁。
  2. 写锁是“独占”锁,可以防止任何其他类型的锁。

推荐学习:mysql视频教程

The above is the detailed content of MySQL syntax analysis table locking. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft