{"id":27,"date":"2009-05-21T22:05:00","date_gmt":"2009-05-21T22:05:00","guid":{"rendered":"https:\/\/karwin.com\/blog\/index.php\/2009\/05\/21\/eav-fail\/"},"modified":"2023-12-15T18:58:27","modified_gmt":"2023-12-15T18:58:27","slug":"eav-fail","status":"publish","type":"post","link":"https:\/\/karwin.com\/blog\/index.php\/2009\/05\/21\/eav-fail\/","title":{"rendered":"EAV FAIL"},"content":{"rendered":"<div style=\"clear: both; text-align: center;\"><a href=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/7\/7e\/US-CA%2C_New_cuyama.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/7\/7e\/US-CA%2C_New_cuyama.jpg\" width=\"981\" height=\"778\" \/><\/a><i><span style=\"font-size: xx-small;\"><a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:New_cuyama.jpg\" target=\"_blank\" rel=\"noopener\">Photo by\u00a0Mike Gogulski, used in accordance with the Creative Commons BY-SA 3.0 license<\/a><\/span><\/i><\/div>\n<p>The photo above illustrates (by counter-example) an important characteristic of a normalized database: each logical &#8220;type&#8221; of attribute belongs in a separate column.<\/p>\n<p>Just because three values happen to be numeric doesn&#8217;t mean it makes sense to SUM() them together. But if dissimilar attributes are stored in the same column, it&#8217;s tempting to treat them as compatible in this way.<\/p>\n<p>This also shows a fallacy of the Entity-Attribute-Value antipattern. In this design, all attribute values are stored in a single column.<\/p>\n<pre>CREATE TABLE EntityAttributeValue (\nentity        VARCHAR(20) NOT NULL,\nattribute     VARCHAR(20) NOT NULL,\nvalue         VARCHAR(1000) NOT NULL,\nPRIMARY KEY (entity, attribute)\n);\n\nINSERT INTO EntityAttributeValue (entity, attribute, value)\nVALUES\n('New Cuyama', 'Population',          '562'),\n('New Cuyama', 'Ft. above sea level', '2150'),\n('New Cuyama', 'Established',         '1951'),\n\nSELECT SUM(value) FROM EntityAttributeValue\nWHERE entity = 'New Cuyama';<\/pre>\n<p>The Entity-Attribute-Value design does not support or conform to rules of database normalization.<\/p>\n<p>To be clear, the proper way to design a database is to put different attributes in different columns. Use column names, not strings, to identify the attributes.<\/p>\n<pre>CREATE TABLE Cities (\n city_id          SERIAL PRIMARY KEY,\n city_name        VARCHAR(100) NOT NULL,\n population       INT UNSIGNED NOT NULL,\n feet_altitude    SMALLINT UNSIGNED NOT NULL,\n year_established SMALLINT UNSIGNED NOT NULL\n);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Photo by\u00a0Mike Gogulski, used in accordance with the Creative Commons BY-SA 3.0 license The photo above illustrates (by counter-example) an important characteristic of a normalized database: each logical &#8220;type&#8221; of attribute belongs in a separate column. Just because three values happen to be numeric doesn&#8217;t mean it makes sense to SUM() them together. But if [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","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,"font":"","enabled":false},"version":2}},"categories":[19,2],"tags":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-antipattern","category-mysql"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pawgV7-r","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":1,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":355,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/27\/revisions\/355"}],"wp:attachment":[{"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/karwin.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}