由SQL注释触发的Mysql错误1064(“您的SQL语法中有错误”)

时间:2022-04-24 14:27:09

I have a series of scripts for creating a schema, with a comment like the following before each instruction:

我有一系列用于创建模式的脚本,在每条指令之前都有如下注释:

--------------------------------------------------------
--  Table TABLE_NAME
--------------------------------------------------------

When I execute the script from mysql on the command line, I get a bunch of errors like the following:

当我在命令行上从mysql执行脚本时,我得到了一堆如下错误:

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 '------------------------------------------------------
------------------------' at line 1

(actually, I get one error for each comment, in spite of the message always referring to line 1).

(实际上,每个评论都会出现一个错误,尽管消息总是引用第1行)。

To quickly solve my problem I simply removed the comments and the script ran without problems, but I was surprised to see such a behaviour and to be unable to find a relevant question here on *. Does anyone have an explanation? Did anyone ever observe such an odd behaviour?

为了快速解决我的问题,我只是删除了注释,脚本运行没有问题,但我很惊讶地看到这样的行为,并且无法在*上找到相关的问题。有没有人有解释?有没有人观察到这种奇怪的行为?

I am running mysql 5.6.30, the default for 5.6 on ubuntu at this time.

我正在运行mysql 5.6.30,这是ubuntu上5.6的默认值。

4 个解决方案

#1


9  

From the MySQL Manual:

从MySQL手册:

From a “-- ” sequence to the end of the line. In MySQL, the “-- ” (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.8.2.4, “'--' as the Start of a Comment”.

从“ - ”序列到行尾。在MySQL中,“ - ”(双破折号)注释样式要求第二个破折号后跟至少一个空格或控制字符(例如空格,制表符,换行符等)。此语法与标准SQL注释语法略有不同,如第1.8.2.4节“' - '作为注释的开头”中所述。

(Emphasis Mine)

tl;DR Your -- indicating a comment must be followed by at least one whitespace or control character.

tl; DR你的 - 表示注释必须至少跟一个空格或控制字符。

Fixed code of yours:

修正了你的代码:

-- -----------------------------------------------------
--  Table TABLE_NAME
-- -----------------------------------------------------

In MySQL You can also use this syntax:

在MySQL中您也可以使用以下语法:

/* 
*    Table TABLE_NAME
*/ 

Or even this:

甚至这个:

# -----------------------------------------------------
#   Table TABLE_NAME
# -----------------------------------------------------

#2


3  

You need a space after two dashes to indicate a comment. Without it it is just a string:

两次破折号后需要一个空格来表示评论。没有它它只是一个字符串:

-- ------------------------------------------------------
--  Table TABLE_NAME
-- ------------------------------------------------------

#3


1  

Personally, I only use the two dashes -- when commenting a single line. When working with block comments, I tend to use the following format:

就个人而言,我只使用两个破折号 - 评论一行时。使用块注释时,我倾向于使用以下格式:

/**
 * Table TABLE_NAME
 *
 */

#4


0  

From: http://dev.mysql.com/doc/refman/5.7/en/comments.html The space after double dash i.e. "-- " is part of the comment identification! Rationale behind MySQL decision: is here http://dev.mysql.com/doc/refman/5.7/en/comments.html

来自:http://dev.mysql.com/doc/refman/5.7/en/comments.html双击之后的空格即“ - ”是评论标识的一部分! MySQL决策的基本原理:在这里http://dev.mysql.com/doc/refman/5.7/en/comments.html

The space is required to prevent problems with automatically generated SQL queries that use constructs such as the following, where we automatically insert the value of the payment for payment:

需要该空间来防止自动生成的SQL查询出现问题,这些查询使用如下所示的结构,其中我们自动插入付款的付款值:

UPDATE account SET credit=credit-payment
UPDATE account SET credit=credit--1

#1


9  

From the MySQL Manual:

从MySQL手册:

From a “-- ” sequence to the end of the line. In MySQL, the “-- ” (double-dash) comment style requires the second dash to be followed by at least one whitespace or control character (such as a space, tab, newline, and so on). This syntax differs slightly from standard SQL comment syntax, as discussed in Section 1.8.2.4, “'--' as the Start of a Comment”.

从“ - ”序列到行尾。在MySQL中,“ - ”(双破折号)注释样式要求第二个破折号后跟至少一个空格或控制字符(例如空格,制表符,换行符等)。此语法与标准SQL注释语法略有不同,如第1.8.2.4节“' - '作为注释的开头”中所述。

(Emphasis Mine)

tl;DR Your -- indicating a comment must be followed by at least one whitespace or control character.

tl; DR你的 - 表示注释必须至少跟一个空格或控制字符。

Fixed code of yours:

修正了你的代码:

-- -----------------------------------------------------
--  Table TABLE_NAME
-- -----------------------------------------------------

In MySQL You can also use this syntax:

在MySQL中您也可以使用以下语法:

/* 
*    Table TABLE_NAME
*/ 

Or even this:

甚至这个:

# -----------------------------------------------------
#   Table TABLE_NAME
# -----------------------------------------------------

#2


3  

You need a space after two dashes to indicate a comment. Without it it is just a string:

两次破折号后需要一个空格来表示评论。没有它它只是一个字符串:

-- ------------------------------------------------------
--  Table TABLE_NAME
-- ------------------------------------------------------

#3


1  

Personally, I only use the two dashes -- when commenting a single line. When working with block comments, I tend to use the following format:

就个人而言,我只使用两个破折号 - 评论一行时。使用块注释时,我倾向于使用以下格式:

/**
 * Table TABLE_NAME
 *
 */

#4


0  

From: http://dev.mysql.com/doc/refman/5.7/en/comments.html The space after double dash i.e. "-- " is part of the comment identification! Rationale behind MySQL decision: is here http://dev.mysql.com/doc/refman/5.7/en/comments.html

来自:http://dev.mysql.com/doc/refman/5.7/en/comments.html双击之后的空格即“ - ”是评论标识的一部分! MySQL决策的基本原理:在这里http://dev.mysql.com/doc/refman/5.7/en/comments.html

The space is required to prevent problems with automatically generated SQL queries that use constructs such as the following, where we automatically insert the value of the payment for payment:

需要该空间来防止自动生成的SQL查询出现问题,这些查询使用如下所示的结构,其中我们自动插入付款的付款值:

UPDATE account SET credit=credit-payment
UPDATE account SET credit=credit--1