MySql Comment Syntax - what is the difference between "#" and "-"

According to the documentation, there are two ways to comment out the end of a line in MySql:

  • "#"
  • "-- "

Is there a difference between the two?

If so, when should I use one against the other?

If not, does anyone know why both are supported?

It seems strange to me, especially when the wrapped version is still slightly different from the standard SQL Syntax .

+4
source share
3 answers

This is really important if you want your SQL to be portable. For example, comments in Sqlite and Postegresql are fine, but # is not. Best used -- with a space. (As far as I remember, I almost never saw anything)

+12
source

Because the link you provided clearly explains that -- is the standard SQL comment separator. When MySQL departs from the standard, a space after -- required, which will be recognized as a comment. "Standard" SQL does not require this.

To provide an example, in the following code it -- recognized as a comment token:

 mysql> CREATE TABLE T(C int); -- This is my new table Query OK, 0 rows affected (0.18 sec) 

But note that after -- interactive interpreter erroneously works:

 mysql> CREATE TABLE T(C int); --This is my new table Query OK, 0 rows affected (0.24 sec) -> ; 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 '--This is my new table' at line 1 

MySQL supports some other comment format to adapt to the habits of various programmers: # like many script languages ​​and /* ... */ like C. It is very amazing that // is not part of them yet.

 mysql> CREATE TABLE T(C int); /* This is my new table */ Query OK, 0 rows affected (0.22 sec) mysql> CREATE TABLE T(C int); # This is my new table Query OK, 0 rows affected (0.24 sec) 
+4
source

Both Oracle and SQL Server (as well as DB2 and others) support the comment standard - and / * * / (multi-line). Therefore, it is a good habit to use the same style in MySQL, as you are likely to encounter situations when you are programming in multiple environments.

The requirement for a space after is MySQL's weirdness, but hardly a big deal, as it helps clarity.

If, of course, you use a lot of perl and MySQL, then you can select # comments for the same reasons ...

0
source

Source: https://habr.com/ru/post/1490536/


All Articles