Monday 9 January 2012

Comments in MySQL

You can include comments in MySQL in three ways:
  1. You can begin them with two hyphens followed by a blank space. These comments continue until the next line so a semi-colon at the end is ignored.
  2. You can begin them with a hash (#). These comments continue until the next line so a semi-colon at the end is ignored.
  3. You can begin them with a /* as in C. Comments like this continue until a */ and can span more than one line.
You can see what I mean in the examples below:
 
mysql> select 1-- valid comment;
    -> ;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
 
mysql> select 2--invalid comment;
ERROR 1054 (42S22): Unknown column 'invalid' in 'field list'
mysql> select 3#another valid comment;
    -> ;
+---+
| 3 |
+---+
| 3 |
+---+
1 row in set (0.00 sec)
 
mysql> select 4/*in-line comment*/;
+---+
| 4 |
+---+
| 4 |
+---+
1 row in set (0.00 sec)
 
mysql> select 5/*multi-
   /*> line comment*/;
+---+
| 5 |
+---+
| 5 |
+---+
1 row in set (0.00 sec)
 
mysql>

No comments: