{"componentChunkName":"component---src-templates-post-template-js","path":"/211205-database-normalization/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"518f0f85-782f-5028-81b3-86094269a82d","html":"<blockquote>\n<p>Normalization(정규화)는 관계를 취하고 그 관계가 normal form(정규형)을 만족하는지 확인하기 위해 일련의 테스트를 실행한다. 불만족스러운 “bad” 관계의 속성을 “smaller” 관계로 분해하는 과정이다.</p>\n</blockquote>\n<p><strong>결과 설계의 품질이 우수하고 <u>(1) 중복 최소화</u> 및 <u>(2) 업데이트 이상 현상 최소화</u>를 충족하도록 실제로 수행한다.</strong></p>\n<p><strong>Denormalization(비정규화)</strong> 는 상위 정규형 관계의 join을 하위 정규형의 기본 관계로 저장하는 과정이다.</p>\n<br />\n<h1>Normal Form (NF)</h1>\n<hr />\n<blockquote>\n<p>관계의 스키마가 특정 정규 형식인지 여부를 인증하기 위해 해당 관계의 키와 FD를 사용하는 관계의 조건을 의미한다.</p>\n</blockquote>\n<ul>\n<li>\n<p>관계가 충족하는 가장 높은(정규 형식) 조건을 나타낸다.</p>\n</li>\n<li>\n<p>관계가 “정규화”된 정도를 나타낸다.</p>\n</li>\n</ul>\n<br />\n<p><strong>The level of NF</strong></p>\n<ul>\n<li>\n<p>1NF, 2NF, 3NF 및 Boyce-Codd NF(BCNF)(3NF의 더 강력한 정의): 관계 스키마의 키 및 FD 기반</p>\n</li>\n<li>\n<p>(4NF, 5NF도 있지만 생략하겠다.)</p>\n</li>\n</ul>\n<br />\n<p>데이터베이스 설계자는 <strong>가능한 가장 높은 정규화 형식으로 정규화할 필요가 없다</strong>(성능으로 인해).</p>\n<ul>\n<li>실제로는 일반적으로 최대 3NF 및 BCNF가 사용되고, 4(5)NF는 거의 사용되지 않는다.</li>\n</ul>\n<br />\n<h1>First Normal Form (1NF)</h1>\n<hr />\n<blockquote>\n<p>기본(평면) 관계형 모델에서 관계의 형식적 정의의 일부로 간주된다. Primary Key를 이용하여 관련 데이터의 각 집합을 고유하게 식별할 수 있어야 한다.</p>\n</blockquote>\n<p>다음을 금지한다.</p>\n<ul>\n<li>\n<p><strong>Composite attributes</strong></p>\n</li>\n<li>\n<p><strong>Multi-valued attributes (MVAs)</strong></p>\n</li>\n<li>\n<p><strong>Nested relations (NRs)</strong></p>\n<ul>\n<li>\n<p>개별 튜플의 값이 <strong>non-atomic</strong>인 속성: 단일 튜플에 대한 값 집합, 값 튜플 또는 둘의 조합</p>\n</li>\n<li>\n<p>관계 내의 관계 또는 튜플 내의 속성 값으로서의 관계</p>\n</li>\n</ul>\n</li>\n</ul>\n<p><strong>대부분의 RDBMS는 1NF에 있는 관계만 정의할 수 있다.</strong></p>\n<br />\n<h2>Normalization into 1NF for (1) an MVA - Example</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144735154-5605f25b-f325-488c-939d-1d6c9354df73.png\" width=\"800px\">\n</div>\n<br />\n<p><strong>1. 1NF를 위반하는 속성 Dlocations를 제거하고 별도의 관계에 배치한다.</strong></p>\n<br />\n<div>\n  <img src=\"https://user-images.githubusercontent.com/33220404/144735312-78f18396-008f-451e-b1c4-dd881c71ba10.png\" width=\"200px\">\n</div>\n<br />\n<ul>\n<li>가장 좋은 것으로 간주된다. ➡️ <strong>튜플들 간 중복 최소화</strong></li>\n</ul>\n<br />\n<p><strong>2. DEPARTMENT의 각 위치에 대해 원래 DEPARTMENT 관계에 별도의 튜플이 있도록 키를 확장한다.</strong></p>\n<ul>\n<li>\n<p>{Dnumber, Dlocation}: 기본 키 역할을 한다.</p>\n</li>\n<li>\n<p>권장되지는 않는다.</p>\n</li>\n</ul>\n<br />\n<p><strong>3. 속성에 대해 최대 값(예: m)이 알려진 경우 속성을 m 속성으로 대체한다.</strong></p>\n<ul>\n<li>\n<p>ex) At most 3 locations -> Dloc1, Dloc2, Dloc3 attributes</p>\n</li>\n<li>\n<p>권장되지는 않는다.</p>\n</li>\n</ul>\n<br />\n<h2>Normalization for (2) Nested Relations - Example</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144735360-ae444eb7-6dcf-485a-9edc-7e9546d71312.png\" width=\"800px\">\n</div>\n<br />\n<h2>(Non)Prime Attributes - 2NF를 알아보기 전 참고</h2>\n<p>관계 스키마에 하나 이상의 (<strong>super</strong>) <strong>key</strong>가 있는 경우 각각을 <strong>candidate key</strong>라고 한다.</p>\n<ul>\n<li><strong>Key</strong>: r(R)에서 t1[SK] ≠ t2[SK]와 같은 <strong>minimal superkey</strong>(SK)</li>\n</ul>\n<p><strong>Primary Key</strong>: candidate 키 중에서 임의로 지정</p>\n<ul>\n<li><strong>Secondary keys</strong>: 다른 candidate 키</li>\n</ul>\n<br />\n<p><strong>Prime Attribute</strong>: 일부 <strong>candidate key의 구성원</strong>이어야 한다.</p>\n<p><strong>Nonprime Attribute</strong>: Prime Attribute이 아니다. 즉, candidate key의 구성원이 아니다.</p>\n<br />\n<h1>Second Normal Form (2NF)</h1>\n<hr />\n<blockquote>\n<p>full functional dependency의 개념을 기반으로 한다. 모든 속성이 full functional dependency을 만족해야 한다.</p>\n</blockquote>\n<p><strong>FD X ➡️ Y는 X에서 속성 A를 제거하는 것이 종속성이 더 이상 유지되지 않는다는 것을 의미하는 경우 full functional dependency(완전 기능 종속성)이다.</strong></p>\n<ul>\n<li>\n<p><strong>Full dependency(완전 종속성)</strong> : 즉, X는 (X – {A})인 경우 기능적으로 Y를 결정하지 않는다.</p>\n</li>\n<li>\n<p><strong>Partial dependency(부분 종속성)</strong> : X에서 일부 속성을 제거할 수 있는 경우에도 종속성이 여전히 유지되는 경우이다.</p>\n</li>\n</ul>\n<br />\n<p><strong><u>relation R의 모든 nonprime 속성 A가 R의 기본 키에 완전히 기능적으로 종속되어 있으면 관계 R은 2NF에 있다.</u></strong></p>\n<br />\n<h2>Normalizing into 2NF</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144736443-1b9debf9-dfdc-4e2f-80c2-0c81d5f70537.png\" width=\"700px\">\n</div>\n<br />\n<p>“<strong>Normalization of 2NF”: decomposed into 2NF relations</strong></p>\n<br />\n<h1>Third Normal Form (3NF)</h1>\n<hr />\n<blockquote>\n<p>transitive functional dependency 개념을 기반으로 한다. Primary Key가 아닌 속성들은 Primary Key에만 의존해야 한다.</p>\n</blockquote>\n<p>R의 FD X ➡️ Z는 X ➡️ Y, Y ➡️ Z의 두 FD에서 파생될 수 있다.</p>\n<ul>\n<li>Y(속성 집합): 후보 키도 아니고 R 키의 하위 집합도 아니다.</li>\n</ul>\n<br />\n<p><strong><u>관계 R은 2NF를 만족하고 R의 nonprime 속성이 R의 기본 키에 이행적으로(transitively) 종속되지 않는 경우 3NF이다.</u></strong></p>\n<br />\n<h2>Normalizing into 3NF</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144736714-670b36a0-7b05-4ae2-b487-f0103cd03ee5.png\" width=\"700px\">\n</div>\n<br />\n<p><strong>“Normalization of 3NF”: decomposed into 3NF relations</strong></p>\n<br />\n<h1>Normal Forms Defined Informally</h1>\n<hr />\n<p><strong>1NF</strong></p>\n<ul>\n<li>\n<p>키 일부 또는 전체에 대해서 의존한다.</p>\n</li>\n<li>\n<p>FD가 없다.</p>\n</li>\n</ul>\n<br />\n<p><strong>2NF</strong></p>\n<ul>\n<li>\n<p>키 전체에 대해서 의존한다.</p>\n</li>\n<li>\n<p>단, transitive property에 의해, 다른 nonprime attribute에 대한 의존성 있을 수 있다.</p>\n</li>\n</ul>\n<br />\n<p><strong>3NF</strong></p>\n<ul>\n<li>모든 속성이 키 외에 의존하는 것이 없다.</li>\n</ul>\n<br/>\n<p>*좌변이 PK의 일부이거나 키가 아닌 속성인 모든 FD는 문제가 된다.</p>\n<p><strong>➡️ 이 문제는 원래 관계를 새로운 관계로 분해하여 2NF와 3NF의 정규화에서 해결할 수 있다.</strong></p>\n<br />\n<h1>Boyce-Codd Normal Form(BCNF)</h1>\n<hr />\n<blockquote>\n<p>FD X ➡️ Y에서 Y가 X의 부분 집합이거나, X는 관계의 SuperKey일 때 BCNF를 만족한다.</p>\n</blockquote>\n<p>각 normal form은 이전 형식보다 엄격하게 강하다.</p>\n<ul>\n<li>모든 2NF 관계는 1NF에 있다.</li>\n<li>모든 3NF 관계는 2NF에 있다.</li>\n<li>모든 BCNF 관계는 3NF에 있다.</li>\n</ul>\n<br />\n<p><strong>관계 디자인의 목표는 BCNF(또는 3NF)에서 각 관계를 갖는 것이다.</strong></p>\n<br />\n<h2>Example of BCNF Normalization</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144737465-e06b329c-c7ab-4498-a60c-c5e7ae1d0ed7.png\" width=\"700px\">\n</div>\n<br />\n<p><strong>BCNF 정규화를 통해</strong> LOTS1A의 여러 튜플에서 동일한 정보(from … Country_name|…|Area)를 <strong>반복하는 중복성을 줄일 수 있다.</strong></p>\n<br />\n<h2>Another Example of a Relation in 3NF But Not in BCNF</h2>\n<br />\n<div style=\"text-align:center;\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144737733-68c9873e-c326-4656-af0b-a4a63bab1885.png\" width=\"350px\">\n</div>\n<br />\n<ul>\n<li>\n<p>TEACH에는 2개의 FD가 있다.</p>\n<ul>\n<li>FD1: {Student, Course} ➡️ {Instructor}</li>\n<li>FD2: {Instructor} ➡️ {Course}</li>\n</ul>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p>{Student, Course}: <strong>TEACH의 후보 키(수퍼키)</strong> 이다.</p>\n<ul>\n<li>\n<p>Instructor: <strong>non-prime</strong> 속성.</p>\n</li>\n<li>\n<p>그래서 transitive(전이) FD가 존재한다.</p>\n</li>\n</ul>\n</li>\n</ul>\n<br />\n<ul>\n<li><strong>BCNF에 속하지 않는 관계는 BCNF의 속성에 맞도록 분해되어야 한다.</strong></li>\n</ul>\n<br />\n<p><strong>How to Achieve BCNF? Decomposition!</strong></p>\n<ul>\n<li>\n<p>관계 TEACH에 대한 세 가지 가능한 분해:</p>\n<ul>\n<li>D1) R1(Student, Instructor) and R2(Student, Course)</li>\n<li>D2) R1(Course, Instructor) and R2(Course, Student)</li>\n<li>D3) R1(Instructor, Course) and R2(Instructor, Student)</li>\n</ul>\n<p>=> 그러나 세 FD 모두 FD1을 잃었다. <strong>BCNF에서 만들기 위해 여러 FD가 있는 경우 FD 보존을 희생해야 한다.</strong></p>\n<ul>\n<li>\n<p>그러면 어떤 분해를 선택해야 할까?</p>\n<ul>\n<li>\n<p>정규화 과정에서 두 가지 분해 속성을 보존하기 위해 하나를 선택한다.</p>\n<p>=> non-additive (lossless) join property (희생할 수 없음)과 기능적 종속성 보존 속성</p>\n</li>\n</ul>\n</li>\n<li>\n<p>D3) <strong>NJB(Nonadditive Join Test for Binary Decomposition)를 통과하면 원하는 분해가 된다.</strong></p>\n</li>\n</ul>\n</li>\n</ul>\n<br />\n<h2>NJB (Non-additive Join Test for Binary Decompositions)</h2>\n<blockquote>\n<p>R의 분해 D = {R1, R2}는 R에서 FD의 집합 F에 대해 lossless(non-additive) join 속성을 가진다.</p>\n</blockquote>\n<ul>\n<li>\n<p>The FD ((R1 ∩ R2) ➡️ (R1 – R2)) is in F+, or</p>\n</li>\n<li>\n<p>The FD ((R1 ∩ R2) ➡️ (R2 – R1)) is in F+,</p>\n</li>\n<li>\n<p>여기서 F+는 F가 암시하는 모든 FD를 포함한다.</p>\n</li>\n</ul>\n<br />\n<p>D1) <strong>R1(Student, Instructor)</strong> and <strong>R2(Student, Course)</strong></p>\n<ul>\n<li>Both <code class=\"language-text\">Student ➡️ Instructor</code> and <code class=\"language-text\">Student ➡️ Course</code> are <strong>false</strong>.</li>\n</ul>\n<p>D2) <strong>R1(Course, Instructor)</strong> and <strong>R2(Course, Student)</strong></p>\n<ul>\n<li>Both <code class=\"language-text\">Course ➡️ Instructor</code> and <code class=\"language-text\">Course ➡️ Student</code> are <strong>false</strong>.</li>\n</ul>\n<p>D3) <strong>R1(Instructor, Course)</strong> and <strong>R2(Instructor, Student)</strong></p>\n<ul>\n<li>\n<p><code class=\"language-text\">Instructor ➡️ Course</code> (= FD2) ⊆ F+</p>\n</li>\n<li>\n<p><strong>NJB 속성이 만족된다. 따라서 D3는 non-additive decomposition으로 선택된다.</strong></p>\n</li>\n</ul>\n<br />\n<h2>BCNF Decomposition Algorithm</h2>\n<ol>\n<li>\n<p>Let R be the relation not in BCNF.</p>\n</li>\n<li>\n<p>Let X ⊆ R.</p>\n</li>\n<li>\n<p>Let X ➡️ A be the FD, <strong>causing a violation of BCNF.</strong></p>\n</li>\n<li>\n<p>그러면 R은 두 가지 관계로 분해될 수 있다.</p>\n</li>\n</ol>\n<div>\n  <img src=\"https://user-images.githubusercontent.com/33220404/144738726-d7d25224-3b76-4d55-9eb3-478370e27e2c.png\" width=\"350px\">\n</div>\n<ol start=\"5\">\n<li>(1) 또는 (2)가 <strong>BCNF에 없으면 분해 과정을 반복</strong>한다. 그렇지 않으면 알고리즘을 종료한다.</li>\n</ol>\n<br />\n<p>ex) In <code class=\"language-text\">TEACH</code>(=R), FD2(<code class=\"language-text\">Instructor</code>(=X) ➡️ <code class=\"language-text\">Course</code>(=A)) violated BCNF</p>\n<ul>\n<li>\n<p>(<code class=\"language-text\">TEACH</code> - <code class=\"language-text\">Course</code>) and (<code class=\"language-text\">Instructor</code> ∪ <code class=\"language-text\">Course</code>)</p>\n</li>\n<li>\n<p>BCNF relations: (<code class=\"language-text\">Instructor</code>, <code class=\"language-text\">Student</code>) and (<code class=\"language-text\">Instructor</code> and <code class=\"language-text\">Course</code>)</p>\n</li>\n</ul>\n<h1>The Hierarchy of Normal Forms</h1>\n<hr />\n<div style=\"text-align:center\">\n  <img src=\"https://user-images.githubusercontent.com/33220404/144739037-8f9192d1-fb36-4bcc-adbe-9a6e5f46d369.png\" width=\"700px\">\n</div>\n<br />\n<p><strong>Denormalization은 성능 여부 때문에 이용될 수 있다.</strong></p>","frontmatter":{"title":"[Database] Nomalization of Relations","date":"2021.12.05.","category":"Data Science","categoryColor":"seagreen","tags":["DB"]}}}]}},"pageContext":{"slug":"/211205-database-normalization/","next":{"frontmatter":{"title":"[Database] Functional Dependencies"},"fields":{"slug":"/211204-database-functional-dependencies/"}},"previous":{"frontmatter":{"title":"[Database] Physical Database Design"},"fields":{"slug":"/211206-database-physical-design/"}}}},"staticQueryHashes":[]}