如何在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.mysqlserver.cn/doc/refman/5.7/en/keywords.html
使用反引号括起保留关键字来解决此问题。
注意 - 您不能将保留关键字用作表名或列名。但是,使用反引号括起来会被认为是正确的。
例如:
create table `insert`
表名和列名都使用反引号的演示。
mysql> create table `Insert` −> ( −> `Add` int −> ); Query OK, 0 rows affected (0.59 sec)
借助反引号,您将不会收到任何错误。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP