Course
Comments
MySQL Tutorial
This SQL tutorial is structured for beginners to guide them from the foundational concepts to advanced data manipulation and querying techniques in SQL. By the end of this tutorial, you will have developed a robust understanding of SQL, equipping you with the knowledge to efficiently manage and analyze data across various database systems. This guide sets the stage for your growth into a skilled data professional, ready to tackle complex data challenges and contribute to the field of data analysis and database management.
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 productsWHERE 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 querySELECT * FROM orders;
SELECT /* This is an inline comment */ customer_nameFROM customers;
/* This is a comment block at the beginning of a script */DELIMITER //CREATE PROCEDURE CalculateDiscount(IN product_id INT)BEGIN -- Calculate discount logic hereEND //DELIMITER ;
Comments Using a Client Program
We can also comment any value using the client program.
Syntax
PHPNodeJSJavaPython
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);
Example
Following are the programs
PHPNodeJSJavaPython
$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