MySQL保留字冲突 关键词:保留字, 关键字
在Mysql中,当表名或字段名乃至数据库名和保留字冲突时,在sql语句里可以用撇号`(Tab键上方的按键)括起来。
注意,只有保留字需要``括起来,非保留字的关键字不需要。
MySQL 8.0 官方文档:https://dev.mysql.com/doc/refman/8.0/en/keywords.html
Keywords are words that have significance in SQL. Certain keywords, such as
SELECT
,DELETE
, orBIGINT
, are reserved and require special treatment for use as identifiers such as table and column names. This may also be true for the names of built-in functions.Nonreserved keywords are permitted as identifiers without quoting. Reserved words are permitted as identifiers if you quote them as described in Section 9.2, “Schema Object Names”:
关键词是SQL中有特定意义的词。一些关键词,例如select, delete, bigint,属于保留字,在用作例如表名和列名的时候需要特殊对待。对于内置函数的名称也是如此。
非保留字在用作标识符的时候不需要使用``包含起来。
1 mysql> CREATE TABLE interval (begin INT, end INT); 2 ERROR 1064 (42000): You have an error in your SQL syntax ... 3 near 'interval (begin INT, end INT)'
BEGIN
andEND
are keywords but not reserved, so their use as identifiers does not require quoting.INTERVAL
is a reserved keyword and must be quoted to be used as an identifier:begin和end都是关键词但不是保留字,所以使用它们用作标识符的时候不需要``引用。而interval属于保留关键词,所以在用作标识符的时候必须使用``引用:
1 mysql> CREATE TABLE `interval` (begin INT, end INT); 2 Query OK, 0 rows affected (0.01 sec)
Exception: A word that follows a period in a qualified name must be an identifier, so it need not be quoted even if it is reserved:
例外:限定名中跟在句点后面的单词一定是标识符,因此即使它是保留字,也不需要``引用:
1 mysql> CREATE TABLE mydb.interval (begin INT, end INT); 2 Query OK, 0 rows affected (0.01 sec)