MySQL
SQL

Commenting in MySQL: Definitive Guide

intro

This definitive guide will walk you through everything you need to know when working with commenting in MySQL and related database management systems.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT
mariadb MARIADB
THE MARIADB DATABASE
MySQL logo MySQL
THE MYSQL DATABASE

Many software engineers and DBAs working on specific projects often come across the necessity to comment on their code. Here’s why MySQL commenting is such a popular topic.

No matter if we’re talking about commenting something out of the code or adding specific comments to clarify how a specific piece of code may be executed during different phases, comments are a necessary part of every programming language.

Let’s dive in!

Comments in SQL

Comments have always been a part of the SQL world. When it comes to relational databases, there are three ways of commenting stuff out:

  1. Use -- to write one-line comments that provide quick references.
  2. Use the # character to write comments that span a single line.
  3. Use /* and */ to write comments spanning multiple lines.

On a high level, comments are used to explain SQL statement functionality.

How Does Commenting in MySQL Look Like?

Commenting in MySQL usually looks like so:

MySQL comments in DbVisualizer
MySQL comments in DbVisualizer

Nothing very complex, yeah? We think so, too. That’s what you have to keep in mind:

  • Comments using -- are designed to cover one line, while comments starting with /* are designed to wrap multiple lines.
  • There’s nothing obstructing you from using both -- and /* at the same time, as you can see in the example above. The same goes for -- and #, # and /* , and so on and so forth.
  • Comments have no impact on the execution time of your queries and vice versa. Your queries do not “look” at the comments at all, neither through optimizers, neither through other mediums.
  • Everything that’s commented out is simply ignored by the DBMS.
  • You can have as many comments as you want and comment on your code as often as you want, too—there’s no hard limit on this either.

Note: Comments using --, the # character, and /* are supported in all relational database management systems but are not supported in non-relational and Microsoft Access-based databases.

Comments in MySQL: Syntax and First Examples

When it comes to commenting in MySQL, things are simple. As already mentioned before, we have three ways of commenting stuff out:

  • -- or # for single-line comments.
  • /*...*/ or comments spanning multiple lines.

Comments are not all the same for all database management systems, the same can be said about MySQL.

Many users of MySQL will find this type of commenting familiar:

Copy
        
1 /*!80000 2 Comment here! 3 */

Here, the number 80000 in the MySQL comment targets MySQL 8.0.00 or above and the same can be said about other numbers too. In regards to versioning, MySQL uses this format:

Copy
        
1 MMmmrr

Where:

  • MM is a two-digit major version
  • mm are the two-digit minor version numbers
  • rr are the two-digit minor version and release numbers

Keep in mind that such “version-based” commenting is only applicable to multiple-line comments.

MySQL supports comments that are different from C-style comments. These include:

  1. /*!STRAIGHT_JOIN*/
  2. /*!KEY_BLOCK_SIZE=XXXXX */
  3. SELECT… /*!+ COMMENT */ FROM …

The first comment is only applicable to MySQL Server, but not to Percona Server or MariaDB. Find out more in our guide on MariaDB vs MySQL.

In the third comment, + specifies a hint for the query optimizer. Invoking MySQL in the binary mode (using the --binary-mode option) prevents the query optimizer from storing such comments in the object definitions.

Comments using the version numbers are not stored on the server. MySQL does not support “short-form” comments supported by some CLI appliances either (here we’re talking about commenting using C to precede and end comment blocks.)

Comments in Other Database Management Systems

As already mentioned before, different database management systems come with different commenting conventions, too, and not all comments are the same across the database world. For example, NoSQL databases such as MongoDB precede and end comments with // or “embed” them in a JSON document like so:

Copy
        
1 { 2 "_id": ObjectId(123), 3 "username": "Username", 4 "about": "Some text...", 5 "comments": [ 6 { 7 "name": "Demo", 8 "body": "Comment!" 9 }, 10 { 11 "name": "Some comment!", 12 "body": "Comment here too!" 13 }, 14 { 15 "name": "Demo for DbVisualizer", 16 "body": "This is a comment as well." 17 } 18 ] 19 }

Using another database management system? Make sure to refer to its documentation for comment examples and use cases.

Summary

Commenting in MySQL is not hard, but it’s not like eating a piece of cake either. One can approach comments in MySQL in one of three ways:

  1. Preceding comments using -- for one-line comments.
  2. Preceding comments using the # character for one-line comments.
  3. Preceding comments using /* and ending them using */ for comments wrapping multiple lines.

It may seem simple and straightforward, but as with everything, comments in MySQL come with a bunch of things necessary for you to know, such as the versioning numbers at the beginning of the comment block or certain C-style comments.

There are many other problems you should be wary about, too — these include problems with your database performance, availability, or security implications and for that, you should consider using a state-of-the-art database client, such as DbVisualizer.

DbVisualizer not only has a powerful SQL editor, but it also allows you to optimize your database instances and explore and visualize the data within them.

We hope that this blog helped you better understand commenting in MySQL, and until next time!

FAQ

How does commenting in MySQL work?

One can approach commenting in MySQL in one of three ways: using # or -- for one-line comments, or using /* and */ for multiple-line comments.

What do the numbers after a multiple-line comment mean?

The numbers after the /*! code block denote the version of MySQL. Such numbers frequently appear in MySQL dump files from phpMyAdmin.

Why aren’t comments the same in all DBMS?

Comments in MySQL aren’t the same as comments in MongoDB or other database management systems. That is because of the way we interact with different database management systems differs from MySQL, and as such, comments are implemented differently too.

Where can I learn more about databases?

Feel free to binge our blog TheTable, or watch educational database-based YouTube videos through our friends Database Dive on YouTube. See you there!

Dbvis download link img
About the author
LukasVileikisPhoto
Lukas Vileikis
Lukas Vileikis is an ethical hacker and a frequent conference speaker. He runs one of the biggest & fastest data breach search engines in the world - BreachDirectory.com, frequently speaks at conferences and blogs in multiple places including his blog over at lukasvileikis.com.
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

How to Deal With a Swollen SQL Transaction Log

author Antonello Zanini tags SQL SQL SERVER 7 min 2025-03-12
title

When, How, and Why to Use the Truncate SQL Statement

author Lukas Vileikis tags DbVisualizer MySQL SQL 6 min 2025-03-11
title

SQL PIVOT: How to Use a Pivot Table In SQL

author Leslie S. Gyamfi tags MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-03-10
title

MySQL ALTER TABLE Statement: Definitive Guide

author Antonello Zanini tags ALTER TABLE MySQL 11 min 2025-03-06
title

A Complete Guide to the SQL CREATE INDEX Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-03-05
title

A Complete Guide to the Order of Execution in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 9 min 2025-03-03
title

MySQL NOW Function: Get the Current Date and Time

author Antonello Zanini tags DATETIME MySQL 5 min 2025-02-27
title

A Complete Guide to the MySQL CTE Mechanism

author Antonello Zanini tags MySQL 10 min 2025-02-26
title

SQL Operators: A Complete Walkthrough

author Lukas Vileikis tags DbVisualizer MySQL SQL 4 min 2025-02-25
title

How to Truncate Many Tables In SQL at Once

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-02-24

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.