MySQL 錯誤 ERROR 1099 (HY000): 表被加上了讀鎖,無法更新


要消除 LOCK TABLES 查詢,您需要使用 UNLOCK TABLES。

讓我們建立一個表 -

mysql> create table demo6
−> (
−> country_name varchar(100
−> )
−> );
Query OK, 0 rows affected (1.51 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into demo6 values('US');
Query OK, 1 row affected (0.11 sec)

mysql> insert into demo6 values('UK');
Query OK, 1 row affected (0.08 sec)

mysql> insert into demo6 values('AUS');
Query OK, 1 row affected (0.11 sec)

使用 select 語句顯示錶中的記錄 -

mysql> select *from demo6;

這將產生以下輸出 -

+--------------+
| country_name |
+--------------+
| US           |
| UK           |
| AUS          |
+--------------+
3 rows in set (0.00 sec)

這裡,我只對上述表加上了讀取操作的鎖。以下是查詢 -

mysql> lock tables demo6 read;
Query OK, 0 rows affected (0.00 sec)

當您嘗試在上述表中插入資料時,會出現以下錯誤 -

mysql> insert into demo6 values('IND');
ERROR 1099 (HY000): Table 'demo6' was locked with a READ lock and can't be updated

如果您使用 UNLOCK TABLES,則可以在同一個表中插入記錄 -

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

使用 insert 命令在表中插入一些記錄 -

mysql> insert into demo6 values('IND');
Query OK, 1 row affected (0.09 sec)

使用 select 語句顯示錶中的記錄 -

mysql> select *from demo6;

這將產生以下輸出 -

+--------------+
| country_name |
+--------------+
| US           |
| UK           |
| AUS          |
| IND          |
+--------------+
4 rows in set (0.00 sec)

更新於: 2020-11-19

601 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.