如何解决在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等等。
MySQL保留关键字的完整列表如下。这是MySQL的官方网站− ** https://dev.mysql.com/doc/refman/5.7/en/keywords.html **
用反引号(backtick)与保留关键字一起解决此问题。
注意 - 您不能使用保留关键字作为表名或列名。但是,使用反引号将被视为正确。
例如 −
create table `insert
用表名和列名的反引号的演示。
mysql> create table `Insert`
−> (
−> `Add` int
−> );
Query OK, 0 rows affected (0.59 sec)
借助反引号,您将不会遇到任何错误。
阅读更多:MySQL 教程
极客教程