MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL - Comments



The Comments are added in a MySQL statement with the purpose of making the statement easier to understand. It makes the MySQL statement more readable and hence easier to update it later. Comments are ignored when the statement is executed. In MySQL, there are two ways of putting a comment.

  • Single line comment
  • Multi-line comment

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

Single line Comment

In MySQL, a single line comment can be done by two ways:

  • using # character : Starts with # and ends with the end of that line.
  • using --  : Starts with -- followed by at least one whitespace or control character (such as a space, tab and so on) and ends with the end of that line.

These single line comments will be ignored when the statement is executed.

Example:

In the example below, two line comments are used. Comments are ignored when the query is executed.

-- first line comment
#second line comment
SELECT Name, Salary FROM Employee; -- third line comment

This will produce the result as shown below:

NameSalary
John3000
Marry2750
Jo2800
Kim3100
Ramesh3000
Huang2800

Multi-line Comment

It starts with /* and ends with */. Anything between /* and */ is a block comment and will be ignored when query is executed.

Example:

In the below MySQL statement, multi-line comments are used. When this query is executed these block of comments will be ignored.

/* comment line 1
comment line 2 */
SELECT EmpID, Name, City  /* more comments */  FROM Employee; 

This result of the above MySQL statement will be:

EmpIDNameCity
1JohnLondon
2MarryNew York
3JoParis
4KimAmsterdam
5RameshNew Delhi
6HuangBeijing

5