There are two types of comments in SQL statements: single-line and multi-line. We’ll take a look at these in our article with an example of each and discuss why we might use comments
When we make comments in our code, what we do as developers is make our intentions known with our code. It’s always a good idea to illustrate or tell what our code does so the next developer who comes along to edit your code can easily read it and take the project forward without having to come find you to question you about your code is supposed to do.
In SQL statements, there are two ways to make comments – a single line comment when you only need to add a single line comment, and then a multi-line comment when you need to add more than one line.
A single line comment is illustrated by two single hyphens at the beginning of the line you would like to comment out:
CREATE TABLE names ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL ); INSERT into names VALUES (1, "Christina"); INSERT into names VALUES (2, "Ashleigh"); INSERT into names VALUES (3, "Sarah"); INSERT into names VALUES (4, "Tad"); INSERT into names VALUES (5, "Dustin"); --INSERT into names VALUES (6, "Elissa"); INSERT into names VALUES (7, "Kelly"); SELECT * from names
A multi-line comment is just like multi-line comments in JavaScript or like comments in CSS: it spans multiple lines.
CREATE TABLE names ( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(128) NOT NULL ); INSERT into names VALUES (1, "Christina"); INSERT into names VALUES (2, "Ashleigh"); INSERT into names VALUES (3, "Sarah"); INSERT into names VALUES (4, "Tad"); /*INSERT into names VALUES (5, "Dustin"); INSERT into names VALUES (6, "Elissa")*/ INSERT into names VALUES (7, "Kelly"); SELECT * from names
Here, we have commented out two lines using the familiar / * comment goes here * / syntax we are familiar with from multi-line comments in JS and comments in CSS.
If you feel comfortable with writing comments in SQL, please check out the following tutorials next!
More Resources:
SQL Create Table
SQL Where
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.