{"id":2581,"date":"2010-07-01T09:36:32","date_gmt":"2010-07-01T07:36:32","guid":{"rendered":"http:\/\/code.openark.org\/blog\/?p=2581"},"modified":"2010-07-01T09:36:32","modified_gmt":"2010-07-01T07:36:32","slug":"sql-good-comments-conventions","status":"publish","type":"post","link":"https:\/\/code.openark.org\/blog\/mysql\/sql-good-comments-conventions","title":{"rendered":"SQL: good comments conventions"},"content":{"rendered":"<p>I happened upon a customer who left me in awe and admiration. The reason: excellent comments for their SQL code.<\/p>\n<p>I list four major places where SQL comments are helpful. I&#8217;ll use the <a href=\"http:\/\/dev.mysql.com\/doc\/sakila\/en\/sakila.html\">sakila<\/a> database. It is originally scarcely commented; I&#8217;ll present it now enhanced with comments, to illustrate.<\/p>\n<h4>Table definitions<\/h4>\n<p>The <strong>CREATE TABLE<\/strong> statement allows for a comment, intended to describe the nature of the table:<\/p>\n<blockquote>\n<pre>CREATE TABLE `film_text` (\r\n `film_id` smallint(6) NOT NULL,\r\n `title` varchar(255) NOT NULL,\r\n `description` text,\r\n PRIMARY KEY (`film_id`),\r\n FULLTEXT KEY `idx_title_description` (`title`,`description`)\r\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 <strong>COMMENT='Reflection of `film`, used for FULLTEXT search.'<\/strong>\r\n<\/pre>\n<\/blockquote>\n<p>It&#8217;s too bad the comment&#8217;s max length is 60 characters, though. However, it&#8217;s a very powerful field.<\/p>\n<h4>Column definitions<\/h4>\n<p>One may comment particular columns:<!--more--><\/p>\n<blockquote>\n<pre>CREATE TABLE `film` (\r\n `film_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,\r\n `title` varchar(255) NOT NULL,\r\n `description` text,\r\n `release_year` year(4) DEFAULT NULL,\r\n `language_id` tinyint(3) unsigned NOT NULL <strong>COMMENT 'Soundtrack spoken language'<\/strong>,\r\n `original_language_id` tinyint(3) unsigned DEFAULT NULL <strong>COMMENT 'Filmed spoken language'<\/strong>,\r\n `rental_duration` tinyint(3) unsigned NOT NULL DEFAULT '3',\r\n `rental_rate` decimal(4,2) NOT NULL DEFAULT '4.99',\r\n `length` smallint(5) unsigned DEFAULT NULL,\r\n `replacement_cost` decimal(5,2) NOT NULL DEFAULT '19.99',\r\n  ...\r\n) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8\r\n<\/pre>\n<\/blockquote>\n<h4>Stored routines definitions<\/h4>\n<p>Here&#8217;s an original <strong>sakila<\/strong> procedure, untouched. It is already commented:<\/p>\n<blockquote>\n<pre>CREATE DEFINER=`root`@`localhost` PROCEDURE `rewards_report`(\r\n IN min_monthly_purchases TINYINT UNSIGNED\r\n , IN min_dollar_amount_purchased DECIMAL(10,2) UNSIGNED\r\n , OUT count_rewardees INT\r\n)\r\n READS SQL DATA\r\n <strong>COMMENT 'Provides a customizable report on best customers'<\/strong>\r\nBEGIN\r\n\r\n DECLARE last_month_start DATE;\r\n DECLARE last_month_end DATE;\r\n ...\r\n<\/pre>\n<\/blockquote>\n<h4>SQL queries<\/h4>\n<p>Last but not least, while not part of the schema, SQL queries define the use of the schema. That is, the schema exists for the sole reason of being able to query it.<\/p>\n<p>Where did <em>that<\/em> query come from? Which piece of code issued it? Why? What&#8217;s its purpose?<\/p>\n<p>Looking at the <strong>PROCESSLIST<\/strong>, the slow log, etc., it is easier when the queries are commented:<\/p>\n<blockquote>\n<pre>SELECT\r\n <strong>\/* List film details along with participating actors *\/<\/strong>\r\n <strong>\/* Issued by analytics module *\/<\/strong>\r\n film.*,\r\n COUNT(*) AS count_actors,\r\n GROUP_CONCAT(CONCAT(actor.first_name, ' ', actor.last_name))\r\nFROM\r\n film\r\n JOIN film_actor USING(film_id)\r\n JOIN actor USING(actor_id)\r\nGROUP BY film.film_id;\r\n<\/pre>\n<\/blockquote>\n<h4>Conclusion<\/h4>\n<p>Source code commenting is an important practice, and usually watched out for. SQL &amp; table definitions commenting are often scarce or non-existent. I urge DBAs to adopt a comments coding convention for SQL, and apply it whenever they can.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I happened upon a customer who left me in awe and admiration. The reason: excellent comments for their SQL code. I list four major places where SQL comments are helpful. I&#8217;ll use the sakila database. It is originally scarcely commented; I&#8217;ll present it now enhanced with comments, to illustrate. Table definitions The CREATE TABLE statement [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[53,5],"tags":[60,21,20],"class_list":["post-2581","post","type-post","status-publish","format-standard","hentry","category-development","category-mysql","tag-coding","tag-sql","tag-syntax"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bZZp-FD","_links":{"self":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/2581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/comments?post=2581"}],"version-history":[{"count":22,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/2581\/revisions"}],"predecessor-version":[{"id":2649,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/posts\/2581\/revisions\/2649"}],"wp:attachment":[{"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/media?parent=2581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/categories?post=2581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code.openark.org\/blog\/wp-json\/wp\/v2\/tags?post=2581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}