totn MariaDB

MariaDB: Comments within SQL

This MariaDB tutorial explains how to use comments within your SQL statements in MariaDB with syntax and examples.

Description

Did you know that you can place comments within your SQL statements in MariaDB? These comments can appear on a single line or span across multiple lines. Let's look at how to do this.

Syntax

There are three syntaxes that you can use to create a comment within your SQL statement in MariaDB.

Syntax Using # symbol

The syntax for creating a SQL comment in MariaDB using # symbol is:

# comment goes here

In MariaDB, a comment started with # symbol must be at the end of a line in your SQL statement with a line break after it. This method of commenting can only span a single line within your SQL and must be at the end of the line.

Syntax Using -- symbol

The syntax for creating a SQL comment in MariaDB using -- symbol is:

-- comment goes here

In MariaDB, a comment started with -- symbol is similar to a comment starting with # symbol. When using the -- symbol, the comment must be at the end of a line in your SQL statement with a line break after it. This method of commenting can only span a single line within your SQL and must be at the end of the line.

Syntax Using /* and */ symbols

The syntax for creating a SQL comment in MariaDB using /* and */ symbols is:

/* comment goes here */

In MariaDB, a comment that starts with /* symbol and ends with */ and can be anywhere in your SQL statement. This method of commenting can span several lines within your SQL.

Example - Comment on a Single Line

You can create a SQL comment on a single line in your SQL statement in MariaDB.

Let's look at a SQL comment example that shows a SQL comment that is on a single line and does not span multiple lines.

SELECT website_id, website_name, host_name
/* Author: TechOnTheNet.com */
FROM websites;

Here is a SQL comment that appears in the middle of the line:

SELECT  /* Author: TechOnTheNet.com */  website_id, website_name, host_name
FROM websites;

Here is a SQL comment that appears at the end of the line:

SELECT website_id, website_name, host_name  /* Author: TechOnTheNet.com */
FROM websites;

or

SELECT website_id, website_name, host_name  -- Author: TechOnTheNet.com
FROM websites;

or

SELECT website_id, website_name, host_name  # Author: TechOnTheNet.com
FROM websites;

Example - Comment on Multiple Lines

In MariaDB, you can create a SQL comment that spans multiple lines in your SQL statement. For example:

SELECT website_id, website_name, host_name
/*
 * Author: TechOnTheNet.com
 * Purpose: To show a comment that spans multiple lines in your SQL
 * statement in MariaDB.
 */
FROM websites;

This SQL comment spans across multiple lines in MariaDB - in this example, it spans across 5 lines.

In MariaDB, you can also create a SQL comment that spans multiple lines using this syntax:

SELECT website_id, website_name, host_name /* Author: TechOnTheNet.com
Purpose: To show a comment that spans multiple lines in your SQL statement
in MariaDB */
FROM websites;

MariaDB will assume that everything after the /* symbol is a comment until it reaches the */ symbol, even if it spans multiple lines within the SQL statement. So in this example, the SQL comment will span across 3 lines within the SQL statement.