MySQL Comments

MySQL Tutorial

MySQL Comments

In MySQL, comments are used to explain SQL code, disable statements temporarily, and improve readability.
They are ignored by the MySQL engine during execution.

 Why Use Comments?

  •  Make SQL easier to understand
  •  Document complex queries
  •  Temporarily disable code for testing/debugging
  •  Helpful in teamwork & maintenance

 Types of Comments

It supports three types of comments:

  1. Single-line comment using --

  2. Single-line comment using #

  3. Multi-line comment using /* ... */

 Single-Line Comment (--)

  • Important rule: There must be a space after --

Inline comment

Single-Line Comment (#)

Mostly used in MySQL, less portable to other databases.

  •  Common in MySQL scripts, not ANSI standard.

 Multi-Line Comment (/* ... */)

Used for long explanations or commenting multiple lines.

Inline block comment

 Commenting Out SQL Code (Debugging)

  •  Useful for testing without running risky queries

 Special MySQL Version-Specific Comments (Advanced)

Used to run code only for specific MySQL versions.

/*!40101 SET NAMES utf8 */;
  •  Executes only if MySQL version ≥ 4.01.01

 Common Mistakes

  •  Forgetting space after --
  •  Using # in other DBs (not portable)
  •  Nesting multi-line comments (not allowed)
  •  Leaving commented DELETE/DROP in production scripts

 MySQL Comments vs Other Databases (Interview)

FeatureMySQL
-- Yes
# Yes (MySQL-specific)
/* */Yes

Interview Questions & MCQs (Very Important)

Q1. How many types of comments does MySQL support?

A) One
B) Two
C) Three
D) Four

Answer: C

Q2. Which symbol is MySQL-specific for comments?

A) --
B) /* */
C) #
D) //

Answer: C

Q3. Which comment is used for multiple lines?

A) --
B) #
C) /* */
D) //

Answer: C

Q4. What is mandatory after --?

A) Semicolon
B) Space
C) New line
D) Tab

Answer: B

Q5. Are comments executed by MySQL?

A) Yes
B) No

Answer: B

Q6. Which comment is used for version-specific execution?

A) #
B) --
C) /* */
D) /*! ... */

Answer: D

 Exam & Interview Tips

  •  Remember 3 comment types
  •  Space after -- is mandatory
  • # is MySQL-specific
  •  Use comments wisely in production

 Summary

  • MySQL supports 3 types of comment

  • Comments improve readability & safety

  • -- and # → single-line

  • /* */ → multi-line

  • /*! ... */ → version-specific

  • Important for SQL exams & interviews

You may also like...