MySQL - Comments



The MySQL Comments

The MySQL Comment is a textual explanation added to a piece of code to provide additional information about the code. Comments are not meant to be executed as part of the code. It serve as notes for readers, including developers, to understand the purpose of the code, functionality, or any other relevant details.

There are two types of comments in MySQL: Single-line comments and Multi-line comments

The MySQL Single Line Comments

Single-line comments are used for brief explanations on a single line. To create a single-line comment in MySQL, use two hyphens (--) followed by your comment text.

Example

In the following query, we are using a single line comment to write a text.

SELECT * FROM customers; -- This is a comment

The MySQL Multi-line Comments

Multi-line comments in MySQL are used for longer explanations or to comment out multiple lines of code. These comments start with /* and end with */. Everything between them is considered a comment.

Example

The following example uses multi-line comment as an explanation of the query −

/*
This is a multi-line comment.
You can use it to explain complex queries or comment out multiple lines of code.

SELECT *
FROM products
WHERE price > 50;
*/

Where to Place Comments

You can place comments almost anywhere in your SQL code. Common places include −

  • Before or after a SQL statement.

  • Within a SQL statement to explain a specific part of it.

  • At the beginning of a script or stored procedure to describe its purpose.

-- This is a comment before a query
SELECT * FROM orders;

SELECT /* This is an inline comment */ customer_name
FROM customers;

/* This is a comment block at the beginning of a script */
DELIMITER //
CREATE PROCEDURE CalculateDiscount(IN product_id INT)
BEGIN
    -- Calculate discount logic here
END //
DELIMITER ;

Comments Using a Client Program

We can also comment any value using the client program.

Syntax

To comment any value or query through a PHP program, we need to execute the following comment methods using the mysqli function query() as follows −

single line comment
--
multiline comment
/**/

(using Query)

$sql = "SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'";
$mysqli->query($sql);

To comment any value or query through a JavaScript program, we need to execute the following comment methods using the query() function of mysql2 library as follows −

single line comment
--
multiline comment
/**/

(using Query)

sql = "SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'";
con.query(sql);

To comment any value or query through a Java program, we need to execute the following comment methods using the JDBC function executeQuery() as follows −

single line comment
--
multiline comment
/**/

(using Query)

String sql = "SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'";
statement.executeQuery(sql);

To comment any value or query through a Python program, we need to execute the following comment methods using the execute() function of the MySQL Connector/Python as follows −

single line comment
--
multiline comment
/**/

(using Query)

comments_query = "SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'"
cursorObj.execute(comments_query)

Example

Following are the programs −

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = 'TUTORIALS';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $db);
if ($mysqli->connect_errno) {
    printf("Connect failed: %s
", $mysqli->connect_error); exit(); } //printf('Connected successfully.
'); $sql = "SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'"; if($mysqli->query($sql)){ printf("Select query executed successfully...!\n"); } printf("Table records: \n"); if($result = $mysqli->query($sql)){ while($row = mysqli_fetch_array($result)){ printf("Id: %d", $row['ID']); printf("\n"); } } if($mysqli->error){ printf("Error message: ", $mysqli->error); } $mysqli->close();

Output

The output obtained is as shown below −

Select query executed successfully...!
Table records:
Id: 4    
var mysql = require('mysql2');
var con = mysql.createConnection({
host:"localhost",
user:"root",
password:"password"
});

 //Connecting to MySQL
 con.connect(function(err) {
 if (err) throw err;
//   console.log("Connected successfully...!");
//   console.log("--------------------------");
 sql = "USE TUTORIALS";
 con.query(sql);
 //create table
 sql = "SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'";
 con.query(sql, function(err, result){
    console.log("Select query executed successfully(where we commented the name and address column)...!");
    console.log("Table records: ")
    if (err) throw err;
    console.log(result);
    });
});   

Output

The output obtained is as shown below −

Select query executed successfully(where we commented the name and address column)...!
Table records:
[ { ID: 4 } ]
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Comments {
  public static void main(String[] args) {
    String url = "jdbc:mysql://localhost:3306/TUTORIALS";
    String user = "root";
    String password = "password";
    ResultSet rs;
    try {
      Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user, password);
            Statement st = con.createStatement();
            //System.out.println("Database connected successfully...!");
            //create table
            String sql = "SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'";
            rs = st.executeQuery(sql);
            System.out.println("Table records: ");
            while(rs.next()) {
              String id = rs.getString("id");
              System.out.println("Id: " + id);
            }
    }catch(Exception e) {
      e.printStackTrace();
    }
  }
}     

Output

The output obtained is as shown below −

Table records: 
Id: 4
import mysql.connector
# Establishing the connection
connection = mysql.connector.connect(
    host='localhost',
    user='root',
    password='password',
    database='tut'
)
cursorObj = connection.cursor()
# Query with comments
comments_query = """SELECT ID /*NAME, ADDRESS*/ FROM CUSTOMERS WHERE ADDRESS = 'Mumbai'"""
cursorObj.execute(comments_query)
# Fetching all the rows that meet the criteria
filtered_rows = cursorObj.fetchall()
# Printing the result
print("IDs of customers from Mumbai:")
for row in filtered_rows:
    print(row[0])
cursorObj.close()
connection.close()  

Output

The output obtained is as shown below −

IDs of customers from Mumbai:
4
Advertisements