How can I add comments in MySQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Comments in MySQL help explain intent, migration risk, and schema meaning when they are used carefully. MySQL supports both SQL text comments and persistent metadata comments on tables and columns. Good comment strategy improves maintainability without turning SQL scripts into stale documentation.
SQL Text Comment Syntax in MySQL
MySQL supports three common inline styles:
- Double-hyphen single-line comment.
- Hash single-line comment.
- Block comment between slash and star markers.
The double-hyphen form should include a following space for parser clarity and portability.
Write Comments for Intent, Not Obvious Syntax
Useful comments explain business constraints or non-obvious choices.
A comment that repeats SQL syntax adds noise and quickly becomes stale.
Persistent Schema Comments
If documentation should live with schema objects, use COMMENT clauses in DDL.
These comments are queryable through metadata views and survive beyond one migration script.
Update column comments later with alter statement:
Version-Specific Comments
MySQL also supports version-gated execution comments for compatibility.
Only compatible MySQL versions execute the embedded SQL. Use this sparingly, because heavy use can reduce script readability.
Stored Procedure and Migration Script Practices
Procedural code benefits from concise phase markers.
Migration headers are also useful:
Short and structured headers improve review speed.
Toolchain and Formatting Considerations
Some SQL formatters or migration tools modify or remove comments. Before treating comments as critical documentation, verify your pipeline preserves them.
Practical checks:
- Run formatter and inspect output.
- Validate migration artifact in CI.
- Confirm schema comments persist after deployment.
If comments are stripped, keep critical operational documentation in dedicated runbooks.
Security and Compliance Notes
Do not place secrets, credentials, or sensitive incident details in SQL comments. Scripts often move across repositories, logs, or deployment artifacts.
Safer approach:
- Keep comments technical and non-sensitive.
- Put confidential context in secured documentation systems.
- Include comment review in migration code review checklist.
Team Commenting Guidelines
A simple convention keeps quality high:
- Comment only non-obvious intent.
- Remove comments when logic changes.
- Prefer schema comments for durable data dictionary notes.
- Avoid leaving commented-out dead SQL in long-lived scripts.
Comment hygiene should be maintained continuously, not during occasional cleanup drives.
Common Pitfalls
- Using double-hyphen comments without required spacing.
- Leaving outdated comments after query refactors.
- Using comments as substitute for proper migration history.
- Storing sensitive details in widely distributed SQL files.
- Assuming tooling preserves comments without verification.
Summary
- MySQL supports line and block comments for SQL text annotation.
- Use comments to explain intent and constraints, not obvious syntax.
- Use
COMMENTclauses for persistent schema documentation. - Apply version-gated comments only where compatibility requires them.
- Keep comments current, concise, and safe for broad visibility.

