{"componentChunkName":"component---src-templates-post-template-js","path":"/211016-database-relational-model/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"454d2ee7-2713-5957-9bc5-7146e34d0345","html":"<h1>Relational Model 개념</h1>\n<blockquote>\n<p>Relational Model은 실제 세계의 데이터를 ‘관계’ 라는 개념을 사용해 표현한 데이터 모델이다.</p>\n</blockquote>\n<h2>Informal</h2>\n<br />\n<table\n    border=\"1\"\n    width=\"50%\"\n    height=\"200\"\n    style=\"margin: auto\"\n>\n    <caption><strong>STUDENT</strong></caption>\n    <thead>\n        <tr align=\"center\">\n            <th style=\"background-color: lightgray;\">Name</th>\n            <th style=\"background-color: lightgray;\">Student_number</th>\n            <th style=\"background-color: lightgray;\">Class</th>\n            <th style=\"background-color: lightgray;\">Major</th>\n        </tr>\n    </thead>\n    <tbody>\n        <tr align=\"center\">\n            <td>Smith</td>\n            <td>17</td>\n            <td>1</td>\n            <td>CS</td>\n        </tr>\n        <tr align=\"center\">\n            <td>Brown</td>\n            <td>8</td>\n            <td>2</td>\n            <td>CS</td>\n        </tr>\n    </tbody>\n</table>\n<br />\n<ul>\n<li>\n<p><strong>Relation</strong></p>\n<p>“sets (element들의 집합\u001d)“에 기반한 수학적 개념.</p>\n<p>Relational Model에서 데이터베이스는 <strong>Relation의 모음</strong>이다.</p>\n<p>Relation은 값의 <strong>Table</strong> (informal) 처럼 보인다.</p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>rows</strong></p>\n<p>Relation은 일련의 <strong>rows</strong>를 포함한다.</p>\n<p>각 row의 데이터 요소는 실제 Entity 또는 Relation에 해당하는 특정 <strong>facts</strong>를 나타낸다.</p>\n<p>formal model에서 row를 <strong>tuple</strong>이라 한다.</p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>column</strong></p>\n<p>각 <strong>column</strong>에는 데이터 항목의 의미를 전달하는 <strong>column header</strong> (informal) 가 있다.</p>\n<p>formal model에서 column를 <strong>attribute</strong>라 한다.</p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>Key of a Relation</strong></p>\n<p>각 <strong>row</strong>에는 테이블에서 해당 행을 고유하게 식별하는 <strong>Key</strong>라고 하는 데이터 elment 값이 있다.</p>\n<p>경우에 따라 <strong>row id</strong> 또는 <strong>sequential number</strong>를 Key로 사용할 수 있으며, 단순히 Table의 row를 식별하기 위해 <strong>surrogate(or artificail) Key</strong>라고 한다.</p>\n</li>\n</ul>\n<br />\n<h2>Formal</h2>\n<br />\n<div style=\"text-align: center\">\n    <img src=\"https://platanus.org/stack/wp-content/uploads/2021/02/250576c5660c4eb393c679bff5b8274e-1024x369.png\" width=\"700\">\n</div>\n<br />\n<ul>\n<li>\n<p><strong>Schema</strong></p>\n<ul>\n<li>\n<p><strong>Relation의 Schema</strong> : <em>R(A1, A2, … , An)</em></p>\n<p><em>R</em> : Relation name</p>\n<p><em>A1, A2, … , An</em> : List of Attribute</p>\n</li>\n</ul>\n<p>Relation을 설명하는 데 사용한다.</p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>Domain</strong></p>\n<p>분할할 수 없는 값의 집합이다. <strong>Data Type</strong> 또는 <strong>Data Format</strong>이 정의되어 있다.</p>\n<p><strong>ex.</strong> Korea_cell_phone_numbers”는 01로 시작하는 유효한 11자리 전화번호 set을 나타낸다.</p>\n<p><strong>ex.</strong> Date는 yyyy-mm-dd 또는 dd/mm/yy/ 등의 format을 가진다.</p>\n<ul>\n<li>\n<p><strong>Attribute(cloumn)은 Relation(Table)에서 Domain 역할을 한다.</strong></p>\n<p>해당 Attribute에 해당하는 데이터 요소의 의미를 해석하는데 사용된다.</p>\n<p><strong>ex.</strong> Domain Date는 “Invoice-date” 또는 “Payment-date”라는 두 가지 Attribute을 서로 다른 의미로 정의하는 데 사용할 수 있다.</p>\n</li>\n</ul>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>Tuple</strong></p>\n<p><strong>정렬된 집합</strong>이다. ’&#x3C; … >’ 로 표현된다.</p>\n<p>Tuple의 각 값은 적절한 <strong>Domain</strong>에서 파생된다.</p>\n<p><strong>ex.</strong> &#x3C;632895, “홍길동”, “대구 북구 대학로 80 IT-5 41566”, “053-950-6372”></p>\n<p><strong>Relation(Table)는 이러한 Tuples(rows)의 집합이다.</strong></p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>State</strong></p>\n<ul>\n<li><strong>Relation State</strong> : Attribute Domain의 Cartesian product1(데카르트 곱1)의 subset이다.</li>\n</ul>\n<p>각 Domain에는 Attribute가 가져올 수 있는 모든 가능한 값 set이 포함되어 있다.</p>\n</li>\n</ul>\n<br />\n<ul>\n<li>\n<p><strong>Summary</strong></p>\n<ul>\n<li>\n<p><strong>Relation R(A1, A2, … , An)</strong> : r(R) ⊆ dom(A1) x dom(A2) x … x dom(An)</p>\n</li>\n<li>\n<p><strong>R(A1, A2, … , An)</strong>: the schema of the relation</p>\n<p>• <strong>R</strong>: the name of the relation</p>\n<p>• <strong>A1, A2, … , An</strong>: the attributes of the relation</p>\n</li>\n<li>\n<p><strong>r(R)</strong>: a specific state (or “value” or “population”) of the relation R</p>\n<p>• A set of tuples (rows) in R.</p>\n<p>• <strong>r(R) = {t1, t2, …, tm}</strong>, where ti is an n-tuple.</p>\n<p>• <strong>ti = &#x3C;v1, v2, …, vn></strong>, where each vj is an element of dom(Aj).</p>\n</li>\n</ul>\n</li>\n</ul>\n<br />\n<br />\n<table\n    border=\"1\"\n    width=\"70%\"\n    height=\"200\"\n    style=\"margin: auto\"\n>\n    <caption><strong>Definition Summary</strong></caption>\n    <thead>\n        <tr>\n            <th style=\"background-color: aliceblue;\">Informal Terms</th>\n            <th style=\"background-color: aliceblue;\">Formal Terms</th>\n        </tr>\n    </thead>\n    <tbody>\n        <tr>\n            <td>Table</td>\n            <td>Relation</td>\n        </tr>\n        <tr>\n            <td>Column Headerrown</td>\n            <td>Attribute</td>\n        </tr>\n         <tr>\n            <td>All possible values in a column</td>\n            <td>Domain</td>\n        </tr>\n         <tr>\n            <td>Row</td>\n            <td>Tuple</td>\n        </tr>\n         <tr>\n            <td>Table Definition</td>\n            <td>Schema (intension) of a Relation</td>\n        </tr>\n         <tr>\n            <td>Populated (Loaded) Table</td>\n            <td>State (extension) of the Relation</td>\n        </tr>\n    </tbody>\n</table>\n<br />\n<br />\n<h1>Relation Model Constraints (제약조건)</h1>\n<blockquote>\n<p>Constraints (제약조건)은 허용되는 값과 데이터베이스에 없는 값을 결정한다.</p>\n</blockquote>\n<p><strong>1. Inherent or implicit constraints</strong></p>\n<p>• 데이터 모델 자체를 기반으로 결정된다.</p>\n<p>• ex) Relation Model은 list를 Attribute 값으로 허용하지 않는다.</p>\n<br />\n<p><strong>2. Schema-based or explicit constraints</strong></p>\n<p>• 데이터 모델 Schema에 직접 표현한다.</p>\n<p>• ex) Unique Constaint</p>\n<br />\n<p><strong>3. Application-based or semantic constraints</strong></p>\n<p>• Application Program에 의해 시행된다. 데이터 모델로 설명할 수 없다.</p>\n<p>• If age > 65 and hours > 40, 총 급여는 2배.</p>\n<hr />\n<h2>Relational Integrity Constraints</h2>\n<blockquote>\n<p>constraint는 모든 유요한 Relation States를 유지해야 하는 조건을 말한다. (No exception)</p>\n</blockquote>\n<h2>1. Key constraints (키 제약조건): Unique constraints (유일 제약조건)</h2>\n<p>정의에 따라 Relation의 모든 <strong>Tuple</strong>은 구별되어야 한다.</p>\n  <br />\n<ul>\n<li>\n<h3>Superkey (SK)</h3>\n<p>한 Relation 내에 있는 <strong>Attribute들의 집합으로 구성</strong>된 Key이다.</p>\n<p>r(R)에 있는 두 개의 Tuple은 <strong>SK</strong>에 대해 동일한 값을 갖지 않아야 한다.</p>\n<p>즉 r(R)의 Tuple t1과 t2에 대해 <strong>t1[SK] ≠ t2[SK]</strong> 이다.</p>\n<br />\n</li>\n<li>\n<h3>Key</h3>\n<p><strong>A “minimal” Superkey</strong></p>\n<p><strong>모든 Key는 Superkey이다. 하지만 반대의 경우는 아니다.</strong></p>\n<p>‘학생’ Relation에서 ‘학번’, ‘주민번호\u001d’, ‘학번 + 주민번호’, ‘학번 + 주민번호 + 성명’ 등 으로 Superkey를 구성할 수 있다.\n이 때 ‘학번 + 주민번호 + 성명’ Superkey인 경우 3개의 Attribute 조합을 통해 다른 Tuple과 구별이 가능하지만, ‘성명’ Attribute를 단독적으로 Superkey로 사용했을 때는 구별이 가능하지 않기 때문에 <strong>‘minimal’</strong> 을 만족시키지 못한다. <strong>즉, 뭉쳤을 경우 유일성이 생기고, 흩어지면 몇 개의 Attribute들은 단독적으로 유일성이 있는 Key로 사용할 수 없다.</strong></p>\n<br />\n</li>\n<li>\n<h3>Candidate Key</h3>\n<p>Relation을 구성하는 Attribute들 중에서 <strong>Tuple을 유일하게 식별할 수 있는 Attribute들의 부분집합</strong>을 의미한다.</p>\n<p>모든 Relation은 반드시 하나 이상의 Candidate Key를 가져야 한다.</p>\n<p>Candidate Key들 중 하나를 <strong>Primary Key로 임의로 선택한다.</strong></p>\n<br />\n</li>\n<li>\n<h3>Primary Key (PK)</h3>\n<p><strong>Candidate Key</strong> 중에서 선택한 Main Key이다.</p>\n<p><strong>한 Relation에서 특정 Tuple을 유일하게 구별할 수 있는 Attribute이다.</strong> (Tuple ID)</p>\n<p>다른 Tuple에서 Tuple을 참조한다. 참조 관계에서 <strong>Foreign Key</strong>로 지정된다.</p>\n<p>null 값을 가질 수 없다. (<strong>Entity integrity constraints</strong>)</p>\n<p>Primary Key로 정의된 Attribute에는 동일한 값이 중복되어 저장될 수 없다. (<strong>Entity integrity constraints</strong>)</p>\n<br />\n</li>\n<li>\n<h3>Foreign Key (FK)</h3>\n<p>Relation R1, R2에서 R1이 참조하고 있는 <strong>R2의 Primary Key와 같은 R1 Attribute</strong></p>\n<p>Foreign Key는 참조되는 Relation의 Primary Key와 대응되어 Relation 간에 참조 관계를 표현하는데 중요한 도구로 사용된다.</p>\n<p>Foreign Key로 지정되면 참조 테이블의 Primary Key에 없는 값은 입력할 수 없습니다. (<strong>Referential integrity constraints</strong>)</p>\n</li>\n</ul>\n<br />\n<h2>2. Entity integrity constraints (엔티티 무결성 제약조건)</h2>\n<p>각 Relation Schema R의 <strong>Primary Key Attribute (PK)는 r(R) Tuple에서 NULL 값을 가질 수 없다.</strong></p>\n<p>PK에 여러 Attribute가 포함된 경우 <strong>NULL은 어느 것에도 허용되지 않는다.</strong></p>\n<br />\n<h2>3. Referential integrity constraints (참조 무결성 제약조건)</h2>\n<p>두 Relation에서 Tuple 간의 <strong>일관성</strong>을 유지하는데 사용된다.</p>\n<p>Relationa R1의 Tuple t1이 R2의 Tuple t2를 참조할 때, <strong>t1은 t2의 PK를 FK로 가지고 있어야 한다.</strong></p>\n<p><strong>ex)</strong> 모든 EMPLOYEE Tuple의 값 EMPLOYEE의 <strong>Dno</strong> (각 직원이 근무하는 부서 번호)는 DEPARMENT Relation에 있는 일부 Tuple의 <strong>Dnumber</strong> 값과 일치해야 한다.</p>\n<h2>4. Domain constraint</h2>\n<p>• Tuple의 모든 값은 해당 Attribute의 Domain에서 가져와야한다.</p>\n<p>• 해당 Attribute에 대해 허용되는 경우 값은 <strong>null</strong>일 수 있다.</p>\n<br />\n<h1>업데이트 작업 및 제약 조건 위반 처리</h1>\n<p>Relation에 대한 <strong>Update</strong> 작업</p>\n<ol>\n<li>INSERT</li>\n<li>DELETE</li>\n<li>MODIFY(Update) a tuple</li>\n</ol>\n<blockquote>\n<p>이러한 작업이 적용될 때 마다 관계형 DB에서 무결성 제약조건 위반이 발생하지 않아야 한다.</p>\n</blockquote>\n<ul>\n<li>\n<p>여러 업데이트 작업을 함께 <strong>그룹화</strong>해야 할 수 있다.</p>\n</li>\n<li>\n<p><strong>업데이트가 전파되어 다른 업데이트가 자동으로 발생</strong>할 수 있다.</p>\n<ul>\n<li>\n<p>이것은 무결성 제약 조건을 유지하기 위해 필요할 수 있다.</p>\n</li>\n<li>\n<p>ex) 신입 사원이 오면 전체 사원 수가 1씩 증가한다.</p>\n</li>\n</ul>\n</li>\n</ul>\n<br />\n<p>무결성 위반의 경우 몇 가지 조치를 취할 수 있다.</p>\n<p><strong>1. 위반을 유발하는 작업을 취소한다</strong></p>\n<p>• <strong>RESTRICT</strong> (no action) 또는 <strong>REJECT</strong> 옵션 -> (많은 DBMS에서 사용)</p>\n<br />\n<p><strong>2. 작업을 수행하되 위반 사실을 사용자에게 알린다</strong></p>\n<p>• 대부분의 DBMS에서 하지는 않는다.</p>\n<br />\n<p><strong>3. 위반 사항이 수정되도록 추가 업데이트를 Trigger한다</strong></p>\n<p>• <strong>CASCADE</strong> (갱신이 전파됨\u001d)</p>\n<p>• <strong>SET NULL</strong> 또는 <strong>SET DEFAULT</strong> 옵션</p>\n<br />\n<p><strong>4. 사용자 지정 오류 수정 루틴을 실행한다</strong></p>\n<p>• 프로그램을 작성해야 한다.</p>","frontmatter":{"title":"[Database] Relational Model","date":"2021.10.16.","category":"Data Science","categoryColor":"seagreen","tags":["DB"]}}}]}},"pageContext":{"slug":"/211016-database-relational-model/","next":{"frontmatter":{"title":"[Database] ER Model"},"fields":{"slug":"/211015-database-er-model/"}},"previous":{"frontmatter":{"title":"[Database] Relation Database Design"},"fields":{"slug":"/211016-database-relation-design/"}}}},"staticQueryHashes":[]}