{"id":549,"date":"2010-03-24T07:35:36","date_gmt":"2010-03-24T14:35:36","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=549"},"modified":"2018-02-23T11:01:15","modified_gmt":"2018-02-23T16:01:15","slug":"naming-database-columns-when-to-violate-dry","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/","title":{"rendered":"Naming Database Columns, When to Violate DRY"},"content":{"rendered":"<p>DRY = Don&#8217;t Repeat Yourself.\\n\\nFor the most part this is a good principle to follow. \u00a0But, there is one particular instance where I feel repeating myself makes things more clear.\\n\\nIt is very important for us to remember that principles are important, but ultimately they are very strong guidelines, not absolutes. \u00a0(It is hard to imagine that I just said that, since I am usually arguing on the merit of\u00a0principles, but I digress.)\\n\\n<\/p>\n<h2>Traditions<\/h2>\n<p>\\n\\nIn particular, when creating database tables and naming database columns, most people will create a table structure that looks like:\\n\\n<a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-550\" title=\"databasewithid\" alt=\"\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg\" width=\"470\" height=\"191\" srcset=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg 470w, https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid-300x122.jpg 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a>\\n\\nLooks perfectly normal and good. \u00a0But, I don&#8217;t like it. \u00a0Imagine you have to join these tables together to create a query. \u00a0(A pretty common occurence.)\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/b82732fb97243a205d6705418d7c997c.js\"><\/script>\\n\\nDid you catch my mistake above? \u00a0Probably not. \u00a0Most people would overlook it. \u00a0With the ER diagram above, and not very many columns on the tables, it is a little bit easier to see.\\n\\nIt is very easy to mix up which table is the parent and which is the child. \u00a0It is very easy to put the .id on the wrong side of the join clause. \u00a0The correct SQL should have been:\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/9315992e029d73b60e91f40849676312.js\"><\/script>\\n\\n<\/p>\n<h2>Bucking the trend<\/h2>\n<p>\\n\\nNow take a look at this ER diagram:\\n\\n<a href=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithfullname.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-552\" title=\"databasewithfullname\" alt=\"database columns\" src=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithfullname.jpg\" width=\"474\" height=\"193\" \/><\/a>\\n\\nThe corresponding SQL for selecting from this database is:\\n\\n<script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/607cba2b17b3d486054967d55b516647.js\"><\/script>\\n\\nIt is a small change, but it makes a huge difference. \u00a0Writing SQL to join the tables is much easier and much more difficult to get wrong. \u00a0Yes, I am repeating myself here, but I am doing it for a very good reason. \u00a0I am making the SQL much more clear and I am making it much easier to talk about these tables.\\n\\nThink about the last time you had a conversation about database tables. \u00a0Did you say <em>id<\/em>? \u00a0Did you then have to qualify which <em>id<\/em> you were talking about? \u00a0If you never have <em>&#8220;id&#8221;<\/em>, you eliminate a large amount of\u00a0ambiguity.\\n\\nThe major argument against what I am suggesting is that you don&#8217;t need to name the <em>id<\/em> for the <em>book<\/em>, <em>book_id<\/em> because it is a column on book, so you know that id refers to the <em>id <\/em>of the book. \u00a0I do not disagree with this. \u00a0For the most part that principle is sound. \u00a0The question though is &#8220;which one is easier to read, write and talk about?&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DRY = Don&#8217;t Repeat Yourself.\\n\\nFor the most part this is a good principle to follow. \u00a0But, there is one particular instance where I feel repeating myself makes things more clear.\\n\\nIt is very important for us to remember that principles are important, but ultimately they are very strong guidelines, not absolutes. \u00a0(It is hard to imagine&#8230;<\/p>\n","protected":false},"author":2,"featured_media":550,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[475,148],"tags":[],"class_list":["post-549","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-database","category-design"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Naming Database Columns, When to Violate DRY<\/title>\n<meta name=\"description\" content=\"A recommendation that when naming database columns it may be a good idea to violate the &quot;Don&#039;t Repeat Yourself&quot; (DRY) principle. Code examples are included.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Naming Database Columns, When to Violate DRY\" \/>\n<meta property=\"og:description\" content=\"A recommendation that when naming database columns it may be a good idea to violate the &quot;Don&#039;t Repeat Yourself&quot; (DRY) principle. Code examples are included.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-03-24T14:35:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-23T16:01:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"470\" \/>\n\t<meta property=\"og:image:height\" content=\"191\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Naming Database Columns, When to Violate DRY\",\"datePublished\":\"2010-03-24T14:35:36+00:00\",\"dateModified\":\"2018-02-23T16:01:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/\"},\"wordCount\":419,\"commentCount\":9,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/databasewithid.jpg\",\"articleSection\":[\"Database\",\"Design\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/\",\"name\":\"Naming Database Columns, When to Violate DRY\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/databasewithid.jpg\",\"datePublished\":\"2010-03-24T14:35:36+00:00\",\"dateModified\":\"2018-02-23T16:01:15+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"A recommendation that when naming database columns it may be a good idea to violate the \\\"Don't Repeat Yourself\\\" (DRY) principle. Code examples are included.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#primaryimage\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/databasewithid.jpg\",\"contentUrl\":\"https:\\\/\\\/simpleprogrammer.com\\\/wp-content\\\/uploads\\\/2010\\\/03\\\/databasewithid.jpg\",\"width\":470,\"height\":191,\"caption\":\"database columns\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/naming-database-columns-when-to-violate-dry\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Naming Database Columns, When to Violate DRY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Naming Database Columns, When to Violate DRY","description":"A recommendation that when naming database columns it may be a good idea to violate the \"Don't Repeat Yourself\" (DRY) principle. Code examples are included.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"Naming Database Columns, When to Violate DRY","og_description":"A recommendation that when naming database columns it may be a good idea to violate the \"Don't Repeat Yourself\" (DRY) principle. Code examples are included.","og_url":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/","og_site_name":"Simple Programmer","article_published_time":"2010-03-24T14:35:36+00:00","article_modified_time":"2018-02-23T16:01:15+00:00","og_image":[{"width":470,"height":191,"url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg","type":"image\/jpeg"}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Naming Database Columns, When to Violate DRY","datePublished":"2010-03-24T14:35:36+00:00","dateModified":"2018-02-23T16:01:15+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/"},"wordCount":419,"commentCount":9,"image":{"@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg","articleSection":["Database","Design"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/","url":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/","name":"Naming Database Columns, When to Violate DRY","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#primaryimage"},"thumbnailUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg","datePublished":"2010-03-24T14:35:36+00:00","dateModified":"2018-02-23T16:01:15+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"A recommendation that when naming database columns it may be a good idea to violate the \"Don't Repeat Yourself\" (DRY) principle. Code examples are included.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#primaryimage","url":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg","contentUrl":"https:\/\/simpleprogrammer.com\/wp-content\/uploads\/2010\/03\/databasewithid.jpg","width":470,"height":191,"caption":"database columns"},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/naming-database-columns-when-to-violate-dry\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Naming Database Columns, When to Violate DRY"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=549"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/549\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media\/550"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}