How can I add comments in MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
Related reading
- How can I append a string to an existing field in MySQL?
- How can I avoid concurrency problems when using SQLite on Android?
- How can I browse or query live MongoDB data?
- How can I cancel a database query in ASP.NET when the user's browser disconnects?
- How can I change the Database Name in AWS RDS for Postgresql?
- How can I check for average concurrent events in a SQL table based on the date, time and duration of the events?
- How can I check MySQL engine type for a specific table?
- How can I check what is stored in my Core Data Database?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.