CS/Database

[데이터베이스] 4. Relational Data Model and Relationship Database Constraints (Chapter 5)

공영재 2023. 12. 2. 15:07

Reference - Fundamentals of Database Systems 7th edition

 

 

Relational Model Concepts

 

- Terms

Relation에 관한 model concept. relation은 table of values와 유사하다.

tuple은 relation의 행을 말하며, 각 행은 real-world의 entity (혹은 relationship)을 의미한다. tuple은 unique한 key가 존재한다.때로 row-ids나 table row의 sequential number가 키가 되는 경우가 있는데, 이를 artifical key 혹은 surrogate key라 한다.

domain은 데이터 타입에 대한 정의를 말한다. (ex.phone numbers are the set of 11 digit in Korea (xxx-xxxx-xxxx))

relation state는 attribute와 domain 간의 연결을 의미한다.

NULL은 모르거나 사용할 수 없는 값을 나타낸다.

Relation Schema는 R(A1, A2, ... An)으로 나타낸다. R은 relation name, A는 attributes를 말한다.

흔히 Relation Model을 표현할 때, Informal Terms와 Formal Terms가 다르다. 각 용어 정의는 아래와 같다.

 

tuple의 value vi는 dom(Ai)에 대응되며, atomic하다고 본다. 

 

Relational Model Constraints and Relational Database Schemas

 

- Constraints 

제약조건은 어떤 value가 허용되고 안되는지를 나타낸다.

Constraints에는 3개의 main type이 있다.

1. Inherent or Implicit Constraints : 모델 자체에서 내포하고 있는 정해진 스키마의 데이터 테입에 대한 제약조건으로, DB에서 받을 수 없는 값을 보냈을 때를 말한다.

2. Schema-based or Explicit Constraints : 스키마에서 직접적으로 표현되는 제약조건이다. (아래에서 더 자세히 설명)

3. Application based or semantic Constraints : dbms 규칙이 아닌 응용프로그램에 따라 달라지는 제약조건으로, 예시로 주민번호가 유효한지 확인하는 과정 등을 말한다.

 

2번의 Explicit Constraint에는 세가지 main type이 있다.

 

- Key constraints (= PK constraint): Key constraints를 살펴보기 전에 Superkey라는 개념에 대해 먼저 알아야 한다.

Superkey란, 한 테이블 내 튜플의 슈퍼키들은 다른 튜플의 슈퍼키와 같을 수 없음을 말한다.

우리가 말하는 key는 minimal superkey를 말하는데, superkey에서 어떠한 attribute도 뺄 수 없는 상태를 말한다.

예시로, 한 tuple에 (학번, 이름, 전공)의 attribute가 있을 때 set(학번, 이름)은 superkey다.

하지만 이름 attribute를 제외하더라도 학번만으로 key로서의 역할을 할 수 있기에(not minimal) set(학번, 이름)은 key가 아니다. Candidate Key는 Key가 여러개 존재할 때 각각의 Key를 의미한다.

Primary Key는 Candidate Key 중 선택된 Key를 의미하며, 최대한 size가 작은 것을 선택해야 한다.

PK는 relation에서 unique해야 한다.

 

- Entity integrity constraints : 각 Tuple의 기본키(primary key)는 NULL값을 절대 가질 수 없다.

 

- Referential integrity constraints (= FK constraint) : Foreign Key는 테이블의 어떤 key가 다른 테이블의 Primary Key를 참조하고 있는 키를 말한다. 두 relation 사이에 참조하고 있는 값이 있을 때, 참조되는 값이 바뀌면 참조하고 있는 값도 바뀌어야 한다. 즉, Primary Key가 바뀌면 Foreign Key도 바뀌어야 한다.

 

+ domain constraint : attribute의 value는 논리적으로 합당해야 한다. 초등학교 학년이 attribute일 때, value가 50이 돼선 안된다.

 

 

Dealing with Constraint Violations

 

INSERT, DELETE, UPDATE operations이 수행될 때, relational DB에서 Integrity constraints가 위반되어선 안된다.

 

Operation 수행 시 integrity violation이 발생하면, RESTRICT(no action)이나 REJECT option; 을 통해 operation을 cancel한다. 대부분의 DBMS가 해당 방식을 취한다. 혹은 CASCADE와 같은 추가적인 updates를 통해 violation이 해결되도록 한다.

 

INSERT에서 발생하는 violation은 Primary Key에 NULL을 삽입하는 Entity integrity constraints나, PK가 중복되는 Key constraints, FK에 존재하지 않는 PK를 삽입할 때 발생하는 referential integrity constraint, 그리고 추가로 삽입할 attribute가 맞지 않는 domain constraint가 있다.

 

DELETE는 referential integrity만 문제가 생긴다. 이때 delete할 tuple을 referencing하고 있다면, 1. RESTRICT로 operation을 reject하거나, 혹은 CASCADE로 해당 tuple을 삭제한 뒤 참조하는 테이블 tuple에 deletion을 전파할 수도 있다.

 

UPDATE에서 PK를 업데이트하면, DELETE와 유사한 option이 필요하며, FK를 업데이트하면 referential integrity violation이 발생할 확률이 높다. 그 외 Attribute를 업데이트할땐 domain constraints와 NOT NULL constraints를 주의해야하며, 발생한다면 reject하면 된다.

반응형
loading