MySQL Comments

MySQL Comments


In MySQL, comments are used to add notes or explanations within your SQL code. Comments are ignored by the database engine and are solely for developers to make the code easier to read and understand.


Types of Comments in MySQL


MySQL supports three types of comments:


  1. Single-line Comments using --
  2. Single-line Comments using #
  3. Multi-line Comments using /* ... */

Single Line Comments


  • Use -- (two hyphens) followed by a space for a single-line comment.
  • It is often used for in-line documentation.


Example
-- This query selects all students
SELECT * FROM students;

SELECT name, age -- Fetching only name and age
FROM students;

Try it yourself

Single-line Comments Using #


  • # is another way to add single-line comments in MySQL.
  • This style is common in MySQL scripts.
Example
# Select students who scored above 80
SELECT * FROM students WHERE score > 80;

Try it yourself

Note

# comments are not part of the SQL standard, but they work in MySQL.

Multi-line Comments


  • Use /* to start the comment and */ to end it.
  • Suitable for large comments or when commenting out blocks of SQL code.
Example
/*
This query selects the student name and score
from the students table where the score is above 75.
*/
SELECT name, score
FROM students
WHERE score > 75;

Try it yourself

Example - Commenting Out a Block of Code
/*
SELECT name FROM students;
SELECT age FROM students;
*/
SELECT name, age FROM students;

Try it yourself

When to Use Comments


  • To explain the purpose of a query.
  • To add notes about changes or updates.
  • To temporarily disable parts of a query for debugging.
  • To describe complex joins, subqueries, or calculations.

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.