How to Add Comments in Simple SQL Selects
Adding comments to your SQL Select statements is a great way to document your SQL command. You can refer to the comments in the future to help you understand why you created the statement in the first place.
To learn how to add comments in simple SQL select statements, follow these steps:
- Download MySQL Community Edition 5.7 for Windows, by visiting "https://dev.mysql.com/downloads/mysql/.
- After the download, install MySQL Community Edition on your Windows computer. Be sure to take note of the "root" account password that you create. You will need that password to authenticate to MySQL in order to work through the How to demonstration.
- Now you will create a database and populate it with two simple tables.
In your text editor, create a new file and type in the following statements carefully to avoid syntax errors later:
DROP DATABASE IF EXISTS HowTo; CREATE DATABASE HowTo; USE HowTo CREATE TABLE States ( state_abbrev CHAR(2), name VARCHAR(100), CONSTRAINT pk_states PRIMARY KEY(state_abbrev) ); INSERT INTO States VALUES ('CA', 'California'); INSERT INTO States VALUES ('IL', 'Illinois'); INSERT INTO States VALUES ('NV', 'Nevada'); INSERT INTO States VALUES ('FL', 'Florida'); INSERT INTO States VALUES ('GA', 'Georgia'); INSERT INTO States VALUES ('NE', 'Nebraska'); INSERT INTO States VALUES ('NY', 'New York'); INSERT INTO States VALUES ('ME', 'Maine'); INSERT INTO States VALUES ('TX', 'Texas'); CREATE TABLE Cities ( name VARCHAR(100), state_abbrev CHAR(2), population NUMERIC(8,0), -- According to 2010 U.S. Census CONSTRAINT fk_cities_states FOREIGN KEY (state_abbrev) REFERENCES STATES (state_abbrev) ); INSERT INTO Cities VALUES ('Los Angeles', 'CA', 3792621); INSERT INTO Cities VALUES ('San Francisco', 'CA', 805235); INSERT INTO Cities VALUES ('San Diego', 'CA', 1307402); INSERT INTO Cities VALUES ('Las Vegas', 'NV', 583756); INSERT INTO Cities VALUES ('Chicago', 'IL', 2695598); INSERT INTO Cities VALUES ('Miami', 'FL', 399457); INSERT INTO Cities VALUES ('Orlando', 'FL', 238300); INSERT INTO Cities VALUES ('Atlanta', 'GA', 420003); INSERT INTO Cities VALUES ('New York City', 'NY', 8175133); INSERT INTO Cities VALUES ('Omaha', 'NE', 408958);
- Save the file as
StatesAndCities.sql. I would suggest you store the file in directory immediately under the C: drive so that you don't have to type in a long directory name when you run the SQL statements through the mysql command tool.
- Start the MySQL Server in your services panel.
- Open a command prompt and navigate (using
c:/Program Files/MySQL/MySQL Server 5.7/bin. Then type in the following command:
Hit Enter. You will be prompted for your root password. Enter your password and hit Enter again.
- You are now in the interactive mode of the mysql command line program. Our first task is to run the script you created. To do so, type in the
sourcecommand as illustrated: Hit Enter to run the script. You will see the output as shown above
(Note: Only a portion of the output is shown on the screen shot). If you experience any errors, check the syntax of your statements with the
statements I presented earlier.
- Your database is ready for action! Now we'll focus on the topic at hand, putting comments in a simple select statement.
- You may have noticed the comment I placed in the CREATE TABLE definition of the
Citiestable. The comment was preceeded by two hyphens. Let's try the comment syntax using a simple select statement:
If you hit your "Up Arrow" key you can retrieve the SQL statement. The comment can help you remember important details about the SQL select statement.
- You can also place a comment within the select statement. We will use
*/as shown in the following example:
This technique permits you to annotate individual parts of your SQL select statement.