Home > Database > Mysql Tutorial > MySQL中select * for update锁表的问题

MySQL中select * for update锁表的问题

WBOY
Release: 2016-06-07 16:52:33
Original
1070 people have browsed it

先前介绍过SELECT ... FOR UPDATE的用法,不过锁定(Lock)的资料是判别就得要注意一下了。由于InnoDB预设是Row-Level Lock,所以只

先前介绍过SELECT ... FOR UPDATE的用法,不过锁定(Lock)的资料是判别就得要注意一下了。由于InnoDB预设是Row-Level Lock,所以只有「明确」的指定主键,MySQL才会执行Row lock (只锁住被选取的资料例) ,否则MySQL将会执行Table Lock (将整个资料表单给锁住)。

举个例子:

假设有个表单products ,里面有id跟name二个栏位,id是主键。

例1: (明确指定主键,并且有此笔资料,row lock)

SELECT * FROM products WHERE FOR UPDATE;

例2: (明确指定主键,若查无此笔资料,无lock)

SELECT * FROM products WHERE FOR UPDATE;

例2: (无主键,table lock)

SELECT * FROM products WHERE FOR UPDATE;

例3: (主键不明确,table lock)

SELECT * FROM products WHERE id'3' FOR UPDATE;

例4: (主键不明确,,table lock)

SELECT * FROM products WHERE id LIKE '3' FOR UPDATE;

注1: FOR UPDATE仅适用于InnoDB,且必须在交易区块(BEGIN/COMMIT)中才能生效。

注2: 要测试锁定的状况,可以利用MySQL的Command Mode ,开二个视窗来做测试。 

linux

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template