SQL Comments :
In my previous articles i have given the idea about different SQL concepts as well as multiple unix concepts. In this article i would like to give the information about SQL Comments.SQL Comments is vital part of any SQL languages. SQL comments indicates the purpose of the code and coder will get idea about why this code has been written and will get idea about the purpose of the code.I thought that writing the article on SQL comments will definitely help the users of this site for understanding the purpose of the code and how the comments are written in the code.
How to Write SQL Comments?
The first question in mind that what will be use of comments in the code? The answer of this question is user needs to know the functionality of the SQL command. To know the functionality of SQL commands programmer needs to add the comments which gives the brief description about what is written and what is use of the SQL command? The SQL comments are not the part of SQL statement it just explains the user about that SQL statement.
Through the code you are communicating with the machine and Through the code you are communicating with the human audience. After writing the code user will get idea about what we have written in code after time span.So SQL Comments are used to understand the functionality of the program without looking in to it. The comments will give you the idea about what is written in the given SQL statement and how it works.The comments can make application or code easier to read as well as maintain.So it is really important to developer to comment in to the SQL statements.
There are two types of SQL Comments or way to commenting in the SQL :
- Single line Comment
- Multi line Comment
Single Line Comments :
The SQL Comments are of two type one is single line comments and other is multi-line comment.User can explain the functionality of SQL statement using those comments.
Syntax :
— Comment to be written
The two dash lines indicates the single line comment in SQL statements. These single line comments are basically used to show the comments in start of program and end of program. User can easily use this comment type to explain the flow of program.
Examples of Single line SQL Comments :
I would like to explain different examples and how the single line comments is used in SQL statements. I will explain each and every example with its importance.
Example 1 : Single line comment at the start of the program or query
–PL SQL block starts here
Declare
Num number(10);
Begin
— This statement will count the number of customers and place it to the Num variable
Select count(*) in to Num from Customer;
–PL SQL block ends here
End;
The Single line comments are used to indicate the start of PL SQL block as well as its end. In above PL SQL block i show the comments in the bold letters which indicates the start of PL SQL block as well as end of PL SQL block.
Example 2 : To avoid some conditions in the SQL statement
The SQL comments are also useful in avoiding some conditions in the SQL statement.Lets say you have written the Employees with role sales in the SQL statement; but now if you want to know about all the Employees then just comment the condition in the SQL statement.
Select * from Employee ; –where role=’Sales’
The above statement will give you the information about all the employees. The statement where role=’Sales’ is commented by single line comment so that this statement is not the part of the query.
Example 3 : Single line comments used to ignore all SQL Statements
Sometimes in real life scenarios user needs to ignore all SQL statements while doing programming. The single line comments are also used to ignore all the SQL statements.
Lets say we dont want to see all employees from employee table and we just want to see the departments from department table.These are situations where single line comments are used to avoid the SQL statement of Employees table
–Select * from Employees;
Select * from Departments;
The above statement avoids the Employees table statement and displays the department table data.
These are above multiple usages of single line comments in SQL.
Multi-Line Comments :
In previous section i have given brief idea about the single line comments.In this section i will try to give the idea about Multi-Line Comments with different real life examples.The basic use of multi-line SQL comments is to comment the multiple statements in SQL.The multi-line comments are not only used to comment single lines but also used to comment multiple lines.
Syntax :
/* Comment to be written */
The backslash with star as well as star with backslash will be written to comment the line.
Example 1 : Commenting multiple SQL statement
The basic use of multi-line comments are commenting multiple lines.
/*
select * from Employee;
Select * from department;
*/
Select * from S_order;
In above statement the Employee and department tables are commented by multi-line comments in SQL.The multi-line comments can ignore more than one SQL statements.
Example 2 : Writing Multi-line comment in the program
User can write any multiple line explanation with using multi-line comments in SQL.
/*
PL SQL block starts here.
The block will give you the number of customers in variable named Num
*/
Declare
Num number(10);
Begin
— This statement will count the number of customers and place it to the Num variable
Select count(*) in to Num from Customer;
–PL SQL block ends here
End;
In above PL SQL statements at the start of the program we have given the multi-line comment.
Example 3 : Multi-line comments to avoid the specific part of the query
Sometimes there are situations where user needs to avoid the specific part of the query in SQL.The single line comments will comment all the statement so these single line comments are not important in that case.User needs to go for multi-line comments to avoid the specific part of the query.
Select name,/*salary,*/department from Employee;
The above statement will give you the name and department of Employee from Employee table.The multi-line comment is used to avoid the salary column from Employee table.
Example 4 : To avoid multiple statements in PL SQL block.
The multi-line comments are used to avoid the multiple statements in PL SQL blog.
Declare
Num number(10);
Begin
/*
— This statement will count the number of customers and place it to the Num variable
Select count(*) in to Num from Customer;
*/
select count(*) in to Num from Department;
–PL SQL block ends here
End;
In above PL SQL block we have avoided multiple PL SQL statements.
These are above some important examples of SQL comments. Hope you like this article on SQL comments. If you like this article on SQL comments or if you have any questions or concerns regarding the same kindly comment in to comments section.