- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
Writing clean SQL is about more than just correctness—it's about clarity. SQL comments help you communicate with others (or your future self) by adding context, notes, or temporarily disabling code. Let’s explore how to use comments effectively.
Using Comments in SQL
Two Main Types:
Helpful When:
Avoid:
How do I add comments in SQL?
Use -- for quick notes or /* */ for multiple lines or blocks.
Can I use comments to remove lines during testing?
—wrap code in /* */ and it won’t run.
Are comments part of SQL standards?
, both styles are widely supported with small engine-specific features.
Can comments be dangerous?
Only if they include secrets or mislead developers. Keep them clean and relevant.
Conclusion
SQL comments don’t take much effort but make a big difference. They’re your best tool for writing collaborative, maintainable SQL scripts. Read for more insights.
Using Comments in SQL
Two Main Types:
Single-line
-- Pull users created in the last week
SELECT * FROM users WHERE created_at >= CURRENT_DATE - INTERVAL '7 days';
Multi-line
/*
Use this version for reporting
Includes only verified accounts
*/
SELECT * FROM accounts WHERE verified = TRUE;
Helpful When:
- Writing complex logic
- Sharing scripts with a team
- Disabling old queries during testing
Avoid:
- Leaving sensitive info
- Explaining simple queries
- Letting comments go stale
- MySQL: Supports versioned comments /*! */.
- PostgreSQL: COMMENT ON lets you label schema elements.
- SQLite: Keeps it basic and portable.
- SQL Server & Oracle: Great for documenting stored code and schema.
How do I add comments in SQL?
Use -- for quick notes or /* */ for multiple lines or blocks.
Can I use comments to remove lines during testing?
—wrap code in /* */ and it won’t run.Are comments part of SQL standards?
, both styles are widely supported with small engine-specific features.Can comments be dangerous?
Only if they include secrets or mislead developers. Keep them clean and relevant.
Conclusion
SQL comments don’t take much effort but make a big difference. They’re your best tool for writing collaborative, maintainable SQL scripts. Read for more insights.