MySQL Comments

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:
Single-line comment using
--Single-line comment using
#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.
- 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)
| Feature | MySQL |
|---|---|
-- | 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-specificImportant for SQL exams & interviews
