SQL is a defacto language used to manage, query, select, filter data in a relational database. Similar to the programming languages SQL provides comments in order to make the SQL query or clause more readable and put some note.
We will start with single-line comments. Double dash
-- is used to create a single line comment. We can put the double dash at the start of the line which we want to comment on.
-- This is single full line comment. SELECT * FROM Users; -- This is a comment too.
We can also use the double dash in order to create half-line or inline comments. We will put a double dash where we want to comment to start. In this example after the SELECT query, we will put a double dash and the next part of the line will become a comment.
-- This is single full line comment. SELECT * FROM Users; -- This is inline comment.
We can also use
*/ in order to create inline comments.
/* will specify the start and
*/ will specify the end of the inline comment.
SELECT * FROM Users; /* This is inline comment. */
We can also comment on some SQL parts like below.
SELECT Name, Surname /*, City */ From Users;
We can see that
City is commented and it will not returned y the SQL query.
Another useful comment type is a multi-line comment where we will use
/* in order to set the start of the comment and
*/ for the end of the comment. Lines between
*/ will be assumed as a comment to like below.
/* This the the start and first line of the comment. This line is comment too. This line too. This line is the end and last line of the comment.*/ SELECT * FROM Users;
Here is another example of the multi-line comment.
/* This the the start and first line of the comment. This line is comment too. This line too. This line is the end and last line of the comment. */ SELECT * FROM Users;
Comments can be nested. Nesting comments can be useful when we try some different SQL sentences and enable or disable them by commenting and uncommenting.
/* This is a comment. /* This is a nested comment. */ This is the end line of the comment.*/
Execute Comment In MySQL
MySQL database provides some extensions to the SQL language. We can use executable comments in order to run SQL for specific MySQL versions. e will use
/*! for the start and
*/ for the end of the executable comment. In the following example, we will sum the two values which will return 4.
SELECT 2 /*! +2 */
Execute Comment For Specific MySQL Version
We can also execute comments for specific MySQL version. We will specify the MySQL version only numbers without dots. and then provide the SQL like below. In the following example, we will run the comment the MySQL version 5.2.10 .
SELECT * FROM Users; /*!5210 KEY_BLOCK_SIZE=1024; */