MySQL 如何锁定多个表
您可以使用LOCK TABLES命令来实现多表锁定。该语法如下−
LOCK TABLES yourTableName1 WRITE;
LOCK TABLES yourTableName2.WRITE;
LOCK TABLES yourTableName3 WRITE;
LOCK TABLES yourTableName4 WRITE;
.
.
.
N;
表锁不是事务安全的,因此在尝试锁定第二个表之前,隐式地首先提交活动事务。
假设我有一个名为OrderDemo的表 −
mysql> create table OrderDemo
-> (
-> OrderId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> OrderPrice int,
-> OrderDatetime datetime
-> );
Query OK, 0 rows affected (0.66 sec)
以下是锁定OrderDemo和utfdemo表的查询。 utfdemo已经在示例数据库中。 查询如下−
mysql> LOCK TABLES OrderDemo WRITE;
Query OK, 0 rows affected (0.03 sec)
mysql> LOCK TABLES utfdemo WRITE;
Query OK, 0 rows affected (0.07 sec)
现在它为会话锁定表。 如果您尝试创建表,则会收到错误。
该错误如下−
mysql> create table LockTableDemo
-> (
-> UserId int,
-> UserName varchar(10)
-> );
ERROR 1100 (HY000): Table 'LockTableDemo' was not locked with LOCK TABLES
mysql> create table UserIformation
-> (
-> UserId int,
-> UserName varchar(10)
-> );
ERROR 1100 (HY000): Table 'UserIformation' was not locked with LOCK TABLES
要修复此问题,您需要重新启动MySQL。
阅读更多:MySQL 教程
极客教程