如何解决在 MySQL 中使用保留字作为表名或列名时发生的错误?
mysqlmysqli database更新于 2024/3/13 21:10:00
当您尝试使用保留字作为表名或列名时,会发生此错误。它可能由于 − 而发生
情况 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)
借助反引号,您将不会收到任何错误。