SQL – Comments
”;
A comment is a piece of text that is used in programming languages to provide additional information. These comments are ignored by the compiler and do not affect the program”s functionality. They are not visible in the output after the execution of code. Their purpose is to make the source code easier for human to understand more clearly.
SQL Comments
In SQL, comments can be used to explain a particular section of a query; or to skip the execution of statement. So, whenever a line of code is marked as a comment in a program, it is not executed.
There are two types of comments used in MySQL database, they are as follows −
- Single-line comments
- Multi-line comments
Single Line Comments
The SQL single line comments starts with two consecutive hyphens (i.e. –) and extends to the end of the line. The text after the hyphens will not be executed.
Syntax
Following is the syntax of SQL single line comment −
-- This is a single-line comment
Example
In the following query, we are using a single line comment to write a text −
-- Will fetch all the table records SELECT * from table;
Example
Here, we have a SELECT statement that retrieves data from a table named CUSTOMERS. Though we have an ORDER BY clause in this statement, since we have commented that part, this query just retrieves the records in the CUSTOMERS table without sorting the result −
SELECT * FROM CUSTOMERS -- ORDER BY NAME ASC;
Example
Now, we are using the single line comment to ignore the last statement −
SELECT * FROM CUSTOMERS; SELECT * FROM EMPLOYEES; -- SELECT * FROM ORDERS WHERE ID = 6;
Multi-Line Comments
The SQL multi line comments are used to comment out multiple lines or a block of SQL code. It starts with /* and ends with */. Entire text between these delimiters (/*…*/) will be ignored and considered as a comment.
Syntax
Following is the syntax of SQL multi line comments −
/* This is a multi-line comment */
Example
The following example uses multi-line comment as an explanation of the query −
/*following query will fetch all the table records./* SELECT * from CUSTOMERS;
Example
Here, we are using the multi-line comments (/*….*/) to ignore a part of the query, making it as a comment −
SELECT ID /*AGE, SALARY*/ FROM CUSTOMERS WHERE SALARY = 1500.00;
Example
In the following query, we are ignoring multiple statements using a multi-line comment −
/*SELECT * FROM CUSTOMERS; SELECT * FROM EMPLOYEE;*/ SELECT * FROM ORDERS WHERE ID = 6;
”;