【问题标题】:Why do I have to use BACKTICKS when identifier is not a reserved word or with spaces?为什么标识符不是保留字或空格时必须使用 BACKTICKS?
【发布时间】:2017-01-28 08:36:27
【问题描述】:

反引号没有错误。

DB [XXX]> create temporary table `123456e6` (`id` char (8));
Query OK, 0 rows affected (0.03 sec)

不使用反引号时出错。

DB [XXX]> create temporary table 123451e6 (`id` char (8));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '123451e6 (`id` char (8))' at line 1

我的标识符 123451e6 根据页面 here 有效,并且不是保留字、空格或包含任何特殊字符。

【问题讨论】:

    标签: mysql sql syntax-error mariadb ddl


    【解决方案1】:

    您遇到了“仅数字”规则的一个微妙案例:

    标识符可以以数字开头,但除非被引用,否则标识符可能不完全由数字组成。

    e 字符用于数字的科学记数法,MySQL 将 123451e6 解释为“123451 * 106”或 123451000000。除“e”之外的任何其他字母都会导致字符串坚持到“仅数字”规则,可以不加引号使用。例如:

    MariaDB [db]> create temporary table 123451f6 (`id` char (8));
    Query OK, 0 rows affected (0.02 sec)
    

    【讨论】:

    • 感谢您的回答。
    【解决方案2】:

    这是无效的,因为 12345e6 形式的某些东西会计算为一个数字,在本例中特别是 12345 * 106。因此,您必须将其显式标记为字符串。

    【讨论】:

    • 你的意思是一个标识符而不是一个字符串。
    • @TheMagician 在这种情况下可以互换。标识符是字符串,而不是数字,这是最初的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 2012-02-10
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多