Using Comments
As you have seen, SQL statements are instructions that are processed by your DBMS. But what if you wanted to include text that you’d not want processed and executed? Why would you ever want to do this? Here are a few reasons:
- The SQL statements we’ve been using here are all very short and very simple. But, as your SQL statement grow (in length and complexity), you’ll want to include descriptive comments (for your own future reference or for whoever has to work on the project next). These comments need to be embedded in the SQL scripts, but they are obviously not intended for actual DBMS processing. (For an example of this, see the create.sql and populate.sql files used in Appendix B).
- The same is true for headers at the top of SQL file, perhaps containing the programmer contact information and a description and notes. (This use case is also seen in the Appendix B .sql files.).
- Another important use for comments is to temporarily stop SQL code from being executed. If you were working with a long SQL statement, and wanted to test just part of it, you could comment out some of the code so that MariaDB saw it as comments and ignored it.
Most DBMSs supports several forms of comment syntax. We’ll Start with inline comments:
Input
SELECT prod_name -- this is a comment FROM Products;
Analysis
Comments may be embedded inline using -- (two hyphens). Anything after the -- is considered comment text, making this a good option for describing columns in a CREATE TABLE statement, for example.
Here is another form of inline comment (although less commonly supported):
Input
# This is a comment SELECT prod_name FROM Products;
Analysis
A # at the start of a line makes the entire line a comment. You can see this format comment used in the accompanying create.sql and populate.sql scripts.
You can also create multi line comments, and comments that stop and start anywhere within the script:
Input
/* SELECT prod_name, vend_id FROM Products; */ SELECT prod_name FROM Products;
Analysis
/* starts a comments, and */ ends it. Anything between /* and */ is comment text. This type of comment is often used to comment out code, as seen in this example. Here, two SELECT statements are defined, but the first won’t execute because it has been commented out.