借助 LOCK TABLES 命令,您可以實(shí)現(xiàn)多個表鎖。語法如下 –
LOCK TABLES yourTableName1 WRITE; LOCK TABLES yourTableName2 WRITE; LOCK TABLES yourTableName3 WRITE; LOCK TABLES yourTableName4 WRITE; . . . N;
登錄后復(fù)制
表鎖不是事務(wù)安全的,它在嘗試鎖定第二個表之前首先隱式提交活動事務(wù)。
假設(shè)我有一個表 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)
登錄后復(fù)制
這里是鎖定表 OrderDemo 和 utfdemo 的查詢。 utfdemo 已存在于示例數(shù)據(jù)庫中。查詢?nèi)缦?–
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)
登錄后復(fù)制
現(xiàn)在它鎖定會話的表。如果您嘗試創(chuàng)建表格,則會收到錯誤。
錯誤如下 –
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
登錄后復(fù)制
要解決此問題,您需要重新啟動 MySQL。
以上就是MySQL如何鎖定多個表?的詳細(xì)內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!