如何在MySQL中解決使用保留字作為表名或列名時發生的錯誤?
當您嘗試使用保留字作為表名或列名時,會發生此錯誤。這可能是由於:
情況1:當您使用保留字作為表名時:
mysql> create table insert −> ( −> Id int −> );
錯誤如下:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert ( Id int )' at line 1
上述錯誤是因為“insert”在MySQL中是關鍵字。
情況2:當您在MySQL中使用保留字作為列名時。
mysql> create table Customers −> ( −> Add int −> );
錯誤如下:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Add int )' at line 3
上述錯誤是因為列名“Add”是MySQL中的保留字。
為了避免上述錯誤,您需要了解所有MySQL保留字。
一些MySQL保留字如下:
Insert Add Is Key Like etc.
MySQL保留關鍵字的完整列表如下。這是MySQL的官方網站:https://dev.mysql.com/doc/refman/5.7/en/keywords.html
使用反引號括起保留關鍵字來解決此問題。
注意 - 您不能將保留關鍵字用作表名或列名。但是,使用反引號括起來會被認為是正確的。
例如:
create table `insert`
表名和列名都使用反引號的演示。
mysql> create table `Insert` −> ( −> `Add` int −> ); Query OK, 0 rows affected (0.59 sec)
藉助反引號,您將不會收到任何錯誤。
廣告