{"id":128,"date":"2011-07-02T21:55:25","date_gmt":"2011-07-02T21:55:25","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=128"},"modified":"2019-06-02T18:38:37","modified_gmt":"2019-06-02T18:38:37","slug":"normalization-1nf","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/normalization-1nf\/","title":{"rendered":"Normalization, 1NF"},"content":{"rendered":"<p>I want to normalize a database schema for efficient transactions. I want to make sure all tables in the schema are in First Normal Form (1NF).<\/p>\n<p>There is no universal definition of 1NF; some definitions require only atomicity such that it may be impossible to violate 1NF if using a relational database such as MySQL (where there is no direct concept of tables within tables). Other definitions of 1NF restrict any attribute from containing a NULL value.<\/p>\n<p>Practical examples make this much easier to understand. Let&#8217;s consider the follow table:<\/p>\n<pre class=\"sh_sql\">mysql&gt; SELECT username, domain, another_domain FROM foobar_users;\n+-----------------+--------+----------------+\n| username        | domain | another_domain |\n+-----------------+--------+----------------+\n| bjones          | XYZ    |                |\n| jsmith          | XYZ    |                |\n| bjones, hmiller | ABC    | XYZ            |\n| gwashington     | ABC    |                |\n+-----------------+--------+----------------+\n4 rows in set (0.00 sec)\n\n<\/pre>\n<p>This table violates what a reasonable data architect refers to as 1NF; namely, a table in 1NF has no repeating columns and no repeating groups.<\/p>\n<p>The third row overloads the username column with two users (comma separated), and there are repeating columns <em>domain<\/em> and <em>another_domain<\/em>. The column order (domain, another_domain) may need to match the overloaded username field.<\/p>\n<p>Putting this in 1NF should look like this:<\/p>\n<pre class=\"sh_sql\">mysql&gt; SELECT username, domain, role FROM foobar_users;\n+-------------+--------+-------+\n| username    | domain | role  |\n+-------------+--------+-------+\n| bjones      | ABC    | admin |\n| bjones      | XYZ    | admin |\n| gwashington | ABC    | user  |\n| hmiller     | ABC    | user  |\n| jsmith      | XYZ    | user  |\n+-------------+--------+-------+\n5 rows in set (0.00 sec)\n<\/pre>\n<p>Now let&#8217;s consider the following DDL:<\/p>\n<pre class=\"sh_sql\">CREATE TABLE foobar_users (\n    username VARCHAR(20) NOT NULL,\n    domain VARCHAR(20) NOT NULL,\n    dept VARCHAR(20),\n    status VARCHAR(12),\n    login_id VARCHAR(20),\n    login_pw VARCHAR(32),\n    role VARCHAR(12) NOT NULL,\n    website VARCHAR(255),\n    partner VARCHAR(20),\n    PRIMARY KEY(username, domain, role)\n);\n<\/pre>\n<p>The tables in the schema are now normalized to 1NF, next, I&#8217;d like to normalize to <a href=\"\/q\/normalization-2nf\/\">Second Normal Form (2NF)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I want to normalize a database schema for efficient transactions. I want to make sure all tables in the schema are in First Normal Form (1NF). There is no universal definition of 1NF; some definitions require only atomicity such that it may be impossible to violate 1NF if using a relational database such as MySQL [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,15],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/128"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":10,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":1003,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/128\/revisions\/1003"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}