{"id":2059,"date":"2014-12-08T12:55:58","date_gmt":"2014-12-08T20:55:58","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=2059"},"modified":"2015-04-07T08:47:03","modified_gmt":"2015-04-07T15:47:03","slug":"backticks-in-mysql","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=2059","title":{"rendered":"Backticks in mySQL"},"content":{"rendered":"<p>When using phpMyAdmin to work on mySQL databases, you may have noticed that database and table names, as well as fields, are surrounded by backticks. If you do a query, the result may look something like this:<\/p>\n<p><pre><code class=\"preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM `product` WHERE `name` LIKE &#039;Articulation&#039;\n<\/code><\/pre><\/p>\n<p>But you can take them out and it still works.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT *&nbsp;&nbsp;FROM product WHERE name LIKE &#039;Articulation&#039;\n<\/code><\/pre><\/p>\n<p>There is a discussion of this at <a href=\"http:\/\/stackoverflow.com\/questions\/261455\/using-backticks-around-field-names\">StackOverflow<\/a> and the consensus is that it is a non-standard practice that could cause problems if you migrate to another DBMS, but it has advantages if you are sticking with mySQL.<\/p>\n<p>If you use backticks in your code, you can use reserved keywords for table names. You can also use table names with spaces.<\/p>\n<p>\u201cIt&#8217;s a lot easier to search your code-base for something in backticks. Say you have a table named event. grep -r &#8220;event&#8221; * might return hundreds of results. grep -r &#8220;\\`event\\`&#8221; * will return anything probably referencing your database.\u201d (EllisGL)<\/p>\n<p>If you use them consistently, it does make the code a bit easier to read, but harder to write. Note that if you need more than the table name, you put backticks around the database as well as the table. e.g. `myDatabase`.`myTable` NOT `myDatabase.myTable`<\/p>\n<p>If you are doing a bunch of things in a query it can get a bit messy. Here\u2019s an example with and without.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT `product`.`name`, `category`.`name` \nFROM `product`\nINNER JOIN product_category AS category\nON `product`.`category_id` = `category`.`id`\n<\/code><\/pre><\/p>\n<p>But you can take them out and it still works.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT product.name, category.name \nFROM product\nINNER JOIN product_category AS category\nON product.category_id = category.id\n<\/code><\/pre><\/p>\n<p>You can even take out just some of them and it works fine, however it is probably not good coding practice.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nSELECT product.name, `category`.name\nFROM `product`\nINNER JOIN product_category AS category\nON `product`.`category_id` = `category`.`id`\n<\/code><\/pre><\/p>\n<p>Note that you still need to use quotes for search terms (like &#8216;Articulation&#8217; above) and when inserting into  non-numeric fields.<\/p>\n<p>I haven\u2019t given this topic any thought before now, but looking at my PHP code, I noticed that I never use backticks in my queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using phpMyAdmin to work on mySQL databases, you may have noticed that database and table names, as well as fields, are surrounded by backticks. If you do a query, the result may look something like this: SELECT *&nbsp;&nbsp;FROM `product` WHERE `name` LIKE &#039;Articulation&#039; But you can take them out and it still works. SELECT &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=2059\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Backticks in mySQL<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-2059","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2059"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/2059\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}