How to make comments in MySQL statements (3+ approaches)

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

Understanding how to comment in MySQL is crucial for creating clear and maintainable SQL scripts. Just like any other programming language, MySQL provides facilities for adding comments to your code. Comments are negligible for MySQL server, but they are invaluable for people reading your code, sharing scripts, or even for reminding yourself of what a certain section is supposed to do.

In this guide, we’ll explore the different ways to add comments to MySQL statements, offer a variety of examples from basic to advanced, and show the implications that comments might have on your code and its execution. Let’s dive in!

Single-Line Comments

Single-line comments are used for brief explanations or for code deactivation on a single line. In MySQL, you can create a single-line comment using either the ‘#’ character or two dashes followed by a whitespace ‘– ‘. Here is an example:

-- This is a single-line comment
SELECT * FROM users;

# This is also a single-line comment
SELECT * FROM products;

Commenting Out Code

Single-line comments are also handy when you want to temporarily disable parts of your SQL code. Here’s how you can comment out a portion of a statement:

SELECT first_name, -- last_name, 
       email FROM users;

With the ‘last_name’ field commented out, the query will only return ‘first_name’ and ’email’.

Multi-Line Comments

For longer descriptions or blocks of code that shouldn’t be executed, multi-line comments are appropriate. They start with ‘/*’ and end with ‘*/’. Everything within those symbols is considered a comment:

/* This is a multi-line comment
   which spans multiple lines. */
SELECT * FROM orders;

Multi-line comments can be placed on a single line or extend across multiple lines. They can also be nested within a SQL statement:

SELECT first_name, /* last_name, */ email 
FROM users;

Nesting Comments

In MySQL, nesting multi-line comments isn’t supported. Attempting to nest multi-line comments could lead to unexpected behavior or errors. See this incorrect example illustrating the issue:

/* Outer comment
   /* Inner comment */
   Unreachable code? */
SELECT * FROM products;  -- The code may result in an error

Conditional Comments

MySQL supports a unique type of comment known as a ‘conditional comment.’ These comments are actually executed by the MySQL server when it runs on a version that matches the specified condition. This feature allows authors to write version-specific code. Here’s how you do it:

/*!50500 SELECT * FROM features */;

This statement is executed only if the MySQL version is 5.5.0 or higher. For lower versions, it’s treated as a comment.

Advanced Use of Conditional Comments

Conditional comments can hide more complex code and even regular SQL statements. It’s also possible to include multiple SQL statements within a conditional comment, for example:

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/* More SQL statements can follow here. */

The MySQL server processes these statements if the version is 4.1.1 or higher. Tip: You can use space inside conditional comments.

Commenting Best Practices

While commenting is beneficial, it’s important to strike the right balance to keep your SQL scripts readable. Here are some best practices for commenting in MySQL:

  • Use comments to explain why you’re doing something, not what you’re doing (unless it’s not obvious).
  • Keep your comments up-to-date with your code changes.
  • Place comments on a separate line, unless they’re brief and fit naturally at the end of a line of code.
  • Avoid commenting out large blocks of code that you’re no longer using. Remove it from your production scripts for clarity.

Remember, good comments don’t replace the need for writing clear, self-explanatory SQL statements.

Common Pitfalls When Commenting

Here are a few common mistakes to avoid with MySQL comments:

  • Putting comments inside string literals.
  • Excessively verbose or irrelevant comments.
  • Nesting multi-line comments, which can introduce errors.

Avoid these pitfalls to ensure your comments contribute positively to your code.

Conclusion

Effective use of comments can greatly enhance the readability and maintainability of your MySQL scripts. Whether you’re annotating your code for the benefit of your future self or for others, make sure your comments are insightful and to the point. Now that you know how to use comments, apply these practices to your own SQL scripts and observe how they lead to better collaboration and understanding among your team.