{"id":29109,"date":"2024-04-29T01:44:19","date_gmt":"2024-04-29T00:44:19","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/mejora-tu-codigo-sql-usando-comentarios-en-mysql\/"},"modified":"2024-06-03T17:44:29","modified_gmt":"2024-06-03T16:44:29","slug":"mejora-tu-codigo-sql-usando-comentarios-en-mysql","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/","title":{"rendered":"Improve your SQL Code Using Comments in MySQL"},"content":{"rendered":"<p>Comments in MySQL not only allow you to leave notes for yourself and other developers who may work on your database in the future, but they also give you a way to experiment with segments of your code without having to permanently delete them. This article will guide you through the essential practices of how to use comments in MySQL to make your SQL statements more understandable and maintainable.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/#%C2%BFQue_son_los_Comentarios_en_MySQL\" >What are Comments in MySQL?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/#Comentarios_de_Una_Sola_Linea\" >One Line Comments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/#Comentarios_de_Varias_Lineas\" >Multiple Line Comments<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/#Buenas_Practicas_al_Comentar\" >Good Commenting Practices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/#Ejemplos_de_Comentarios_Utiles\" >Examples of Helpful Comments<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/#Documentando_un_Procedimiento_Almacenado\" >Documenting a Stored Procedure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/improve-your-sql-code-using-comments-in-mysql\/#Anadiendo_Contexto_a_una_Decision_de_Diseno\" >Adding Context to a Design Decision<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_son_los_Comentarios_en_MySQL\"><\/span>What are Comments in MySQL?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A comment in MySQL is a piece of text within an SQL statement that is not executed by the database server. It is primarily used to document code, explaining what a certain part of the code does or why it was written that way. Comments are crucial for maintaining the code and making it easier for other developers or even yourself to understand in the future.<\/p>\n<p>There are two types of comments in MySQL: single-line comments and multi-line comments. Below, we explore how to use each type.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Comentarios_de_Una_Sola_Linea\"><\/span>One Line Comments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In MySQL, you can comment a single line of your code using two scripts (<code>--<\/code>) followed by a space. Any text after the dashes on that line will be ignored by the server.<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">-- This is a single line comment SELECT * FROM users; -- This comment follows an SQL statement<\/code><\/pre>\n<p>Using single-line comments is ideal for short explanations or side notes.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Comentarios_de_Varias_Lineas\"><\/span>Multiple Line Comments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>For longer comments, you can use multi-line comments starting with <code>\/*<\/code> and end with <code>*\/<\/code>. This type of comment can extend over several lines and is useful for describing the next block of code in detail.<\/p>\n<pre><code class=\"&quot;language-sql&quot;\">\/* This is a multi-line comment. It can be used to further explain the logic behind the following SQL block *\/ SELECT * FROM orders WHERE date &gt; &#039;2023-01-01&#039;;<\/code><\/pre>\n<p>Multi-line comments are especially useful in environments where you need to explain complex algorithms or important design decisions.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Buenas_Practicas_al_Comentar\"><\/span>Good Commenting Practices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Commenting your code can be as much art as science. Here are some good practices:<\/p>\n<ol>\n<li><strong>Clarity Above All:<\/strong> Make sure your comments are clear and easy to understand. Avoid complex or undocumented jargon.<\/li>\n<li><strong>Keep it Relevant:<\/strong> Only comment on things that have an impact on the understanding of the code. Avoid redundant or irrelevant comments.<\/li>\n<li><strong>Update Comments:<\/strong> If you update the code, be sure to update the accompanying comments. An incorrect comment can be more damaging than no comment at all.<\/li>\n<li><strong>Avoid Commenting Obsolete Code:<\/strong> Instead of commenting out old code, consider deleting it. Older versions of your code should be controlled by version systems like Git.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Ejemplos_de_Comentarios_Utiles\"><\/span>Examples of Helpful Comments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Here are some examples of how feedback can be useful in different situations:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Documentando_un_Procedimiento_Almacenado\"><\/span>Documenting a Stored Procedure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"&quot;language-sql&quot;\">\/* Name: RetrieveOrder Description: Selects all order details based on the order ID. Parameters: - @OrderID: ID of the order being searched Returns: Order details Notes: Make sure the orderID is validated before using this procedure. *\/ DELIMITER \/\/ CREATE PROCEDURE RetrieveOrder(IN OrderID INT) BEGIN SELECT * FROM orders WHERE id = OrderID; END \/\/ DELIMITER ;<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Anadiendo_Contexto_a_una_Decision_de_Diseno\"><\/span>Adding Context to a Design Decision<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre><code class=\"&quot;language-sql&quot;\">\/* We change the query to include a JOIN with the users table since we need the user&#039;s name for the new report required by the marketing team. *\/ SELECT orders.id, users.name FROM orders JOIN users ON orders.user_id = users.id WHERE orders.date &gt;= CURDATE();<\/code><\/pre>\n<p>Using comments effectively not only improves the readability of your code, but also acts as live documentation that can be extremely useful for collaboration and long-term maintenance of applications. If you want to explore more about how to improve your SQL skills or learn about other programming topics, visit <a href=\"https:\/\/nelkodev.com\/en\/\">my blog<\/a> o <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact me<\/a> if you have any questions or need help with your projects.<\/p>","protected":false},"excerpt":{"rendered":"<p>Comments in MySQL not only allow you to leave notes for yourself and other developers who may work on your database in the future, but they also give you a way to experiment with segments of your code without having to permanently delete them. This article will walk you through the [\u2026]<\/p>","protected":false},"author":1,"featured_media":29110,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2010,2185],"tags":[2193,205,525,1080,2189,49,352,504,2192,1273],"class_list":["post-29109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-advanced-techniques","category-curso-mysql","tag-advanced","tag-blog","tag-codigo","tag-comentarios","tag-curso","tag-mejora","tag-mysql","tag-sql","tag-techniques","tag-usando"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=29109"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/29109\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/29110"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=29109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=29109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=29109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}