MySQL Comments

In MySQL, comments are used to explain sections of SQL code, making the code easier to understand, or to temporarily disable part of the code (prevent execution). MySQL supports both single-line and multi-line comments.


Single Line Comments

  • Syntax: -- comment
  • Any text following -- on the same line will be treated as a comment and ignored by the MySQL engine.

Example 1: Basic comment

-- Select all records from the Customers table:
SELECT * FROM Customers;

Example 2: Comment part of the statement

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

Example 3: Comment out a full statement

-- SELECT * FROM Customers;
SELECT * FROM Products;

Multi-line Comments

  • Syntax: /* comment */
  • Anything between /* and */ is treated as a comment, even if it spans multiple lines.

Example 1: Multi-line comment as explanation

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

Example 2: Comment multiple statements

/*SELECT * FROM Customers;
  SELECT * FROM Products;
  SELECT * FROM Orders;
  SELECT * FROM Categories;*/
SELECT * FROM Suppliers;

Example 3: Comment part of a statement

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

Example 4: Complex comment within a statement

SELECT * FROM Customers WHERE (CustomerName LIKE 'L%'
OR CustomerName LIKE 'R%' /*OR CustomerName LIKE 'S%'
OR CustomerName LIKE 'T%'*/ OR CustomerName LIKE 'W%')
AND Country='USA'
ORDER BY CustomerName;

Summary

  • Single-line comments are useful for brief notes or disabling a single line of code.
  • Multi-line comments are ideal for longer explanations or temporarily disabling large sections of code.

Was this article helpful?