This article will discuss several ways to make comments in MySQL. These methods will work if you are using the MySQL shell or editing a .sql file which you will then import into MySQL.
Method 1: Using a ‘#’ to comment until the end of a line
Using a ‘#’ (without the quotes), is a way to comment in a line until the end of the line. This is similar to how you could comment a line in Python.
select * from bank_table where acc_num='abcd1234'; #bank information
For this method, remember that anything after the ‘#’ won’t be read. You can continue your code on the next line.
Method 2: Using ‘–‘ to comment until the end of a line
Using ‘–‘ (without the quotes), is a way to comment in a line until the end of the line. This is very similar to method 1.
select name, age from students where id='S1803393'; -- a student's info
Note: This method might not work in MySQL workbench, but should work in the command line with MySQL.
Method 3: In-line comment
This is using a comment C-style. You can actually write a comment within a line and still continue SQL code after the comment on the same line as opposed to methods 1 and 2. With this method, you start the comment with /* and then end your comment with */
select species /*should be a dog*/ from pet_table where name = 'Cali';
Method 4: Multi-line comments
This is very similar to how you can make multi-line comments in the C language. This is similar to method 3, except you continue it for multiple lines. You start the comment with /*, and then you end the comment with */, the catch is that you could run the comment for as many lines as you want.
select * from my_table;
/* We should figure out something
more clever to query. This comment is still being continued
This line is where the comment will end. Notice that this comment went on for 3 lines. */
What did you think of this article? Did you have anything to add? Let’s discuss it in the comments below.