Mysql, comment types

4

I had already realized that mysql may not even be the only case, but I would like an explanation on it to be more objective.

I have several types of comments that are accepted:

1) -- ESTA LINHA ESTA COMENTADA ?

2) # ESTA LINHA ESTA COMENTADA ?

3) /*ESTA LINHA ESTA COMENTATA 
   E CONTINUA COMENTADA*/

4) /*!40101 SET character_set_client = utf8 */;

I know I can use both option 1 and 2 to make a comment on a single line and 3 and 4 for multiple lines but I still have to understand other details.

My question is / are:

  • Use - or # make any difference, do you have any recommendations on when to use one or the other?
  • Are the instructions in 4) executed even if they are commented?
asked by anonymous 16.06.2015 / 14:04

2 answers

4

As @PauloRoberto mentioned and referenced in his reply , the four types of commentaries work, but only the comment with -- is ANSI SQL; other DBMSs do not accept other types of comments.

I recommend, therefore, that you avoid using the other code comment formats you write yourself: the day you want to migrate to something more parrudo (and you going ) need to migrate to something else gritty on the day you want to do something more complicated), you'll need to fix less code.

Regarding comments with !40101 , they are conditional comments : they are processed only by versions of MySQL greater than or equal to the number provided (in the example you gave, SET will only be executed by MySQL versions ≥ 4.1.1); previous versions will ignore the content of the comment (and, I assume, mess up all the accented characters in the entry contained in the rest of mysql_dump ).

They have an important catch: they do not work inside stored procedures , since the parser discards comments; if you want to run commands conditioned to the MySQL version, you will need to do something smarter, probably involving dynamic SQL.

    
16.06.2015 / 14:26
0

The MySql documentation has an entire chapter dedicated to this and is in Portuguese, has specific sections and shows how, why and where to use each type of comment.

  

The MySQL server supports the # comment styles at the end of the line,   - at the end of the line and / * on the line or on multiple lines * /

     

mysql > select 1 + 1; # This comment continues to the end of the line   mysql > select 1 + 1; - This account continues to the end of the line   mysql > select 1 / * This is a line comment / + 1; mysql > select   1+ / This is a multiple line comment   */ 1; Note that the comment style - requires at least one space after the code -!

     

Although the server understands the comment syntaxes described here,   there are some limitations in the way that the mysql client parses the   comment / * ... * /:

     

Single-quote and double-quote characters are used to indicate   the beginning of a string with quotation marks, even within a comment. If the   quotes do not match a second quotation mark within the comment, the   analyzer does not realize that the comment has an end. If you are   running mysql interactively, you may notice the confusion   occurred because of the mysql prompt change > for '> or ">.

     

A semicolon is used to indicate the end of an SQL statement   and anything that comes after it indicates the beginning of the next   instruction.

     

These limitations apply to both when you run mysql   interactively when placing commands in a file and   asks that mysql read the entries in this file with the mysql command   < some-file.

     

MySQL supports the SQL-99 '-' comment style only if the second   dash is followed by space See more information about this in Section   1.8.4.7, "'-' as Start of Comment".

6.1.6. Syntax of Comments 1.8.4.7. '-' as Comment Start

    
16.06.2015 / 14:11