MySQL Comments

What is the MySQL Comments?

Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements. In MySQL, a comment started with — symbol is similar to a comment starting with # symbol. When using the — symbol, the comment must be at the end of a line in your SQL statement with a line break after it. This method of commenting can only span a single line within your SQL and must be at the end of the line.

Single Line Comments

Single line comments start with --.

Any text between — and the end of the line will be ignored (will not be executed). The following example uses a single-line comment as an explanation:

Example

-- Select all:<br>SELECT * FROM Customers;

The following example uses a single-line comment to ignore the end of a line:

Example

SELECT * FROM Customers -- WHERE City='Berlin';

The following example uses a single-line comment to ignore a statement:

Example

-- SELECT * FROM Customers;<br>SELECT * FROM Products;

Multi-line Comments

Multi-line comments start with /* and end with */. Any text between /* and */ will be ignored. The following example uses a multi-line comment as an explanation:

Example

/*Select all the columns<br>of all the records<br>in the Customers table:*/<br>SELECT * FROM Customers;

The following example uses a multi-line comment to ignore many statements:

Example

/*SELECT * FROM Customers;<br>SELECT * FROM Products;<br>SELECT * FROM Orders;<br>SELECT * FROM Categories;*/<br>SELECT * FROM Suppliers;

To ignore just a part of a statement, also use the /* */ comment. The following example uses a comment to ignore part of a line:

Example

SELECT CustomerName, /*City,*/ Country FROM Customers; 

The following example uses a comment to ignore part of a statement:

Example

SELECT*;FROM ;Customers; WHERE;(Customer Name ;LIKE;'L%'<br>OR ;CustomerName;LIKE&nbsp;'R%';/*OR CustomerName LIKE 'S%'<br>OR CustomerName LIKE 'T%'*/ ;OR ;Customer Name ;LIKE&nbsp;'W%')<br>AND;Country='USA'<br>ORDER;BY&;CustomerName;

In MySQL Server, we can write the comments in mainly three ways that are given below:

  1. Using # symbol
  2. Using – symbol
  3. Using /* and */ symbol

Let us understand each in detail.https://imasdk.googleapis.com/js/core/bridge3.508.0_en.html#goog_1838502700

Using # symbol

It is used at the end of the line or SQL statements.

Syntax

  1. SELECT Statement; # comment goes here  

Example

This example gives the student details without parsing comments.

Mysql> <strong>Select</strong> * <strong>FROM</strong> student_info; # JAVATPOINT    

Output:

MySQL Comments

Using — symbol

It is placed at the end of the line. In this comment styling, we must ensure that the double-slash has at least one whitespace or control character such as tab, space, newline, etc.

Syntax

  1. SELECT Statement; – – comment goes here  

This example gives the student details without parsing comments.

mysql> <strong>SELECT</strong> * <strong>FROM</strong> orders; - - JAVATPOINT  

Output:

MySQL Comments

Using /* and */ symbol

This type of comment is similar to the C programming language that can span in multiple lines. We can use this comment to document the block of SQL statements.

Syntax

  1. /*   
  2.    comment goes here   
  3.    comment goes here  
  4. */    

Example

C-styling comment in SQL*/ <strong>SELECT</strong> * <strong>FROM</strong> orders;  cds

Output:

MySQL Comments

Executable Comments

MySQL has also used executable comments. This comment styling provides portability between different databases. It allows us to embed the SQL code that the only executable in MySQL, but other databases will ignore this extension.

Syntax

The following are the syntax of executable comments:

Syntax

/*! MySQL-specific code */  

Example

mysql> <strong>SELECT</strong> 3 /*! +2 */ <strong>AS</strong> SUM;  

Output:

MySQL Comments
MySQL Comments
Show Buttons
Hide Buttons