{"id":667,"date":"2010-03-11T12:57:24","date_gmt":"2010-03-11T12:57:24","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=667"},"modified":"2023-09-05T07:47:10","modified_gmt":"2023-09-05T07:47:10","slug":"sql-syntax-with-c-style-comments-in-mysqldump","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/sql-syntax-with-c-style-comments-in-mysqldump","title":{"rendered":"SQL syntax with \/*! c-style comments in MySQLdump"},"content":{"rendered":"\n<p>In mysql we have &#8212; , \/* and \/*! comments. &nbsp;This post is mainly about very basic c-style comments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\/*! : C-Style comments in MySQL<\/h2>\n\n\n\n<p>We normally see comments in MySQLdump as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/*!40000 ALTER TABLE `a` DISABLE KEYS *\/;<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*!50013 DEFINER=`nitty_witty_com`@`%` SQL SECURITY DEFINER *\/<\/code><\/pre>\n\n\n\n<p>These are actually C-Style comments which has embeded sql and treated specially by MySQL server, but ignored by other database engines.<br>This helps in writing portable comments, it&#8217;s treated as a part of surrounding SQL by MySQL while ignored by other databases.<\/p>\n\n\n\n<p>But still you may wonder what are these numbers after \/*! &#8211;&nbsp;\/*!40000 or \/*!50013 !!<\/p>\n\n\n\n<p><strong>There are two ways you can write the C-Style comments.<br><\/strong> 1. \/*! SQL *\/<\/p>\n\n\n\n<p>When SQL is embeded with comments [\/*! and *\/] it&#8217;s executed by MySQL server along with surrounding SQL.<\/p>\n\n\n\n<p>2. \/*!version-number SQL *\/<\/p>\n\n\n\n<p>When SQL is embeded with comment that begins with \/*! follwed by a version number, the SQL becomes version specific.<br>MySQL executes SQL body only if it server&#8217;s version is atleast as recent as version-number.<\/p>\n\n\n\n<p>So if your MySQL version is 5.x, following syntax will give error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>SHOW \/*!60000 status *\/;<\/em><\/pre>\n\n\n\n<p>but following will work fine!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>SHOW \/*!50000 status *\/;<\/em><\/pre>\n\n\n\n<p id=\"_mcePaste\"><span style=\"font-weight: normal;\"><span style=\"font-weight: normal;\"><span style=\"font-size: 13px;\">Similarly see below example:<\/span><\/span><\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"font-weight: normal;\"><span style=\"font-weight: normal;\"><span style=\"font-size: 13px;\"><em>mysql&gt; show \/*!50077 full*\/ tables;\n+----------------+------------+\n| Tables_in_test | Table_type |\n+----------------+------------+\n| comments &nbsp; &nbsp; &nbsp; | BASE TABLE |\n+----------------+------------+\n1 row in set (0.00 sec)<\/em><\/span><\/span><\/span><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>mysql&gt; show \/*!50078 full*\/ tables;<br>+----------------+<br>| Tables_in_test |<br>+----------------+<br>| comments &nbsp; &nbsp; &nbsp; |<br>+----------------+<br>1 row in set (0.00 sec)<\/em><\/pre>\n\n\n\n<p><em>Above code works at first attempt as MySQL Version was 5.0.77 &nbsp;but for version specific comment \/*!50078 it don&#8217;t.<\/em><\/p>\n\n\n\n<p>Further this can be used for adjusting the syntax or keywords change in latest releases. For eg MySQL 8.0.26 has replaced the word &#8220;slave&#8221; with &#8220;replica&#8221; in variables and you can use your query to hint the execution as:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT @@global.version as version, @@global.hostname as service \\\n\/*!80026 , @@global.replica_exec_mode as replica_exec_mode *\/ \\\n\/*<em>!80026 , @@global.replica_skip_errors as replica_skip_errors<\/em> *\/\\G<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Other comments in MySQL:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>&#8212; : double slash and a space or # : Hash<\/strong><\/li>\n<\/ul>\n\n\n\n<p>This is spanned till end of the line.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\/* :<\/strong><\/li>\n<\/ul>\n\n\n\n<p>This is a normal multi-line comment.<\/p>\n","protected":false},"excerpt":{"rendered":"In mysql we have &#8212; , \/* and \/*! comments. &nbsp;This post is mainly about very basic c-style comments. \/*! : C-Style comments in MySQL We normally see comments in&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,377],"tags":[547,549,238,681,27,427],"class_list":{"0":"post-667","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-articles","8":"tag-547","9":"tag-80001-2","10":"tag-c-style-comments-in-mysqldump","11":"tag-comment-in-sql","12":"tag-comments","13":"tag-mysql"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=667"}],"version-history":[{"count":5,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/667\/revisions"}],"predecessor-version":[{"id":3027,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/667\/revisions\/3027"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}