[DB05] Relational data model 이해하기
* 10.10 업데이트
해당 게시물은 한양대학교 컴퓨터소프트웨어학부 김상욱 교수님 데이터베이스시스템 온라인 강의를 듣고 정리한 자료입니다.
오류가 있다면 언제든 알려주세요!
● 해당 강의의 목표
1. relational data model에 대해서 이해한다.
· Relational model의 개념 (logical model 중 대표적인 모델)
· Relational model의 표기법(기호)
· Relational model의 제약
◆ Relational Model Concepts
· Database: relations(or tables)의 집합
· Relation: tuples(or rows)의 집합
데이터베이스는 relation들의 집합이고, 하나의 relation은 tuple들의 집합이다.
· Tuple: 실세계에서 나타나는 entity나 relationship을 표현하기 위해서 사용되는 것, attribute들의 집합으로 구성
· ER model: entity와 그들간의 relationship으로 표현
· Relational model: 모든 정보를 tuple로 표현
· Attribute: tuple과 대응되는 object 각각의 특징
전체가 데이터베이스
하나하나가 relation(table)
즉, University database consists of 5 tables
하나의 table은 tuple들의 집합. ex) Student table is a table that has 2 tuples.
한줄 한줄이 다 tuple이다.
하나의 tuple은 entity에 대응된다.
Smith라는 엔티티에 대응되는 tuple
Course와 선수강과목과의 관계를 나타내는 tuple
어떤 학생이 어떤 과목을 수강했는데 점수가 B였다라는 관계를 나타내는 tuple
어떤 course에 대한 section이 언제 열리고 그 강사는 King이라는 엔티티에 대응되는 tuple
하나의 tuple은 attribute의 집합이다. -> smith, 17, 1, CS라는 attribute value의 집합
각각의 attribute는 해당되는 실세계에 존재하는 것의 특성
각 tuple은 실세계에 존재하는 entity나 relationship을 표현하고 하나의 tuple에는 여러 attribute value들로 이루어진다.
Student table
Name, Ssn, Home_phone, Address, Office_phone, Age, Apa attribute를 가진다.
table schema: table name + attribute name
name은 attribute, Benjamin Bayer는 attribute value이다.
attribute value로 이루어진 table row를 tuple이라 부른다.
용어 정리
· Domain D
- A set of atomic values. 보통은 어떤 attribute의 domain이라고 씀.
- ex) domain of Age attribute: 16~80인 정수들의 집합
· Attribute Ai
- relation schema R에 있는 domain D가 어떤 역할을 하는지 나타내는 이름
- dom(Ai): relation schema R에서 attribute Ai의 domain
- ex) relation schema R에서 Employee_ages라는 속성은 16~80이라는 domain을 가진다.
-> dom(Employee_ages)(=D)
그 도메인이 employee의 나이를 나타낸다.
· Relation schema R
- relation 이름 R과 R에 포함되는 attribute들의 집합 (Ai~An)
- R(A1, A2, ... An) 으로 표기한다.
- ex) STUDENT(Name, SSN, HomePhone, Address, OfficePhone, Age, GPA)
relation schema 이름은 Student고 들어가있는 속성들을 알 수 있다.
· Degree(or arity) of a relation
- table에 들어있는 속성들의 수
- ex) degree of STUDENT relation: 7 (7개의 attributes를 가지고 있다.)
· Tuple t of relation R
- n-tuple이라고도 부른다. (n: 그 relation schema에 속하는 attribute의 수)
- t = <v1, v2, ..., vn>: tuple에 해당되는 attribute value들이 들어감
- 각각의 value vi, 1 ≤ i ≤ n, 는
1. dom(Ai): i번째 attribute의 범위에 속하는 하나의 값이거나
2. NULL 값이다.
· Relation instance r(R)
- 해당되는 relation schema R을 따르는 tuple들의 집합
- r(R) = {t1, t2, ... tm}
- domain들의 Cartesian product의 subset이다.
r(R) ⊆ (dom(A1)×dom(A2)×... ×dom(An))
Cartesian product: 가능한 값들의 모든 가능한 조합
· Relation Schema(Table Schema) = Relation Name(Table Name) + Attribute들의 집합.
그러한 table schema를 따르는 각각의 tuple이 있다.
Name이라는 속성의 domain 중 하나인 Benjamin Bayer가 attribute value가 된다.
tuple들의 집합이 해당 relation schema를 따르는 relation instance가 되는 것이다.
◆ Characteristics of Relations
· relation내에 존재하는 tuple들의 순서
- 집합 내에서의 원소의 순서가 중요하지 않듯 table에서 tuple간의 순서도 중요하지 않다.
· relation 내에서 attribute들의 순서
- n-tuple은 n value들의 ordered list이기 때문에 order들의 순서는 의미가 있다.
- 의미는 있지만 실상 attribute과 그 값들의 순서는 그렇게 중요하지 않다.
· Values in tuples
- 각각의 value는 모두 atomic하다. (쪼갤 수 없다.)
ex) 홍길동과 대응되는 tuple에 주소라는 속성이 있으면 이는 그것은 통째로 하나의 value인 것이다. 이를 나누어서 생각할 수 없다.
우리 학교의 주소 '서울시 성동구 행당1동 한양대학교 17번지' 이를 나누어서 생각할 수 없다. 따라서 '행당동에 있는 학교를 찾아라' 하면 우리학교를 찾을 수 없다. full string을 통해서만 찾을 수 있다.
- 따라서 composite attribute와 multivalued attribute들을 허용하지 않는다. (ER model에서는 허용함)
- First normal form assumption이라고 한다. (중간고사 이후)
- NULLs
- 알려지지 않거나 tuple에 적용될 수 없는 값일 때 사용된다.
ex) 회원가입을 할 때 급여를 넣으라고 하면 우리는 보통 아무것도 넣지 않는다. 이럴 때 NULL이 들어간다. unknown의 예시이다.
또는 employee라는 table이 있을 때 자신의 managing department를 써야하면 부서장은 자신의 managing department가 있지만 신입사원은 없다. 따라서 이는 not applicable인 경우이다.
◆ Relational Model Notation
· t[Ai] : t라는 tuple의 속성 Ai에 해당되는 value 값 vi
· t[Au, Aw, .. Az] : <vu, vw,... , vz>의 값을 갖는 전체 tuple t의 sub-tuple
◆ Relational Model Constraints
· Constraints
- relation schema에 있는 실제 값들의 제약조건 (schema를 정하고 instance의 값을 enforce한다.)
- 그 relation에 있는 모든 Instance는 해당 제약조건을 따져야한다.
- DBMS의 기능은 이러한 제약조건을 정하게 하는 방법을 제공하고 정해진 제약조건이 위배되지 않게 instance를 check 하는 것이다.
- mini-world에서 실제로 돌아가는 rule들도 이러한 제약조건이 정해진다.
· Relational model 자체에서도 중요한 제약 조건이 있다.
- Key Constraints
- Entity integrity constraint
- Referential integrity constraint
Constraints를 보기 전에 key의 종류부터 알아보자.
키의 종류
1. relational schema R의 Superkey
- R에 속하는 attribute들의 집합이어야 한다. (아무거나 다 superkey가 될 수 없다)
- R에 해당되는 어떠한 relation instance r(R)도 SK라면 같은 값을 가질 수 없다.
- r(R)에 속하는 t1, t2가 있을 때 항상 t1[SK] ≠ t2[SK]여야 한다. unique하게 식별된다.
- 어떠한 두 개의 tuple도 superkey의 값은 같을 수 없다. 따라서 uniqueness constraint라 불린다.
2. relational schema R의 Candidate key K
- R의 superkey여야 한다.
- Minimal superkey이다.
- K에서 어떠한 속성 A를 제거했을 때 그 나머지 attribute들이 더 이상 superkey가 아니게 된다.
- 하나의 relation schema는 2개 이상의 candidate key를 가질 수 있다.
위는 5개의 attribute을 가지는 table이다.
Q) 여기서 모든 attribute들의 set으로 구성된 것이 superkey일까?
A) yes! 5개의 attritbute가 모두 같은 tuple이 존재하지 않기 때문이다.
Q) 그러면 candidate key일까?
A) no! year 빼더라도 여전히 나머지 4개가 superkey이기 때문이다.
여기서 License_number가 primary key이기 때문에 나머지 4개의 attribute를 다 제거하더라도 superkey가 된다. 따라서 해당 License_number키가 candidate key가 된다. 또한 Engine_serial_number도 candidate key가 된다.
만약 candidate key와 Make가 붙어있다면 이는 superkey이지만 candidate key는 아니다.
정리!
Superkey: 유일하게 식별할 수 있으면 된다.
candidate key: Superkey가 되기 위해서 꼭 필요한 것들만 모여있어야한다.
Another Example)
STUDENT relational schema
- {SSN, Name, Age}: superkey (서로 다른 학생들 중에 주민번호, 이름, 나이가 모두 같은 학생은 없기 때문)
not a candidate key (나이를 빼더라도 혹은 이름을 빼더라도 superkey이기 때문에)
- {SSN, Name} : superkey O , candidate key X
- {SSN} : superkey O, candidate key O
3. relational schema R의 primary key(PK)
- 하나의 table 내에서는 candidate key가 2개 이상 있을 수 있기 때문에 database designer가 무엇을 PK로 쓸지 정한다.
- 해당 attribute에 밑줄을 긋는다.
ex) DEPARTMENT relational schema
- DepartmentID, DepartmentName 둘다 candidate key이지만 database designer가 DepartmentID를 PK로 정한 것이다. key는 string보다는 고정길이를 가진 id로 정한다. DepartmentName는 secondary key라고 부른다.
· Key Constraints
- 하나의 테이블은 반드시 candidate key를 가져야한다.
모든 attribute를 다합치면 candidate key가 될 수 밖에 없어서 최소한 candidate key가 없는 경우는 존재하지 않는다. 다만 superkey를 찾고 불필요한걸 찾아서 빼면 real candidate key가 된다.
· Relational Database Schema
- 같은 database안에 속하는 relation schema의 집합이다.
S = {R1, R2, ... Rm}
Relational database schema S는 relation schema들로 구성된다.
- integrity constraints IC(무결성 제약조건)의 set도 포함한다.
(key constraint도 integrity constraints 중 하나이다.)
해당 스키마는 ER Schema를 Relational Schema로 바꾼 결과이다.
6개의 테이블로 구성된다.
· Relational database instance
- 같은 database에 속하는 relation instance들의 집합이다. {r1, r2, ... rm}
- ri는 Ri의 instance이고 이는 integrity constraint들을 모두 만족하는 relation들의 집합이다.
그 schema를 따르는 tuple들의 집합이 relation instance이다.
이 relation instance에 속하는 모든 value들은 미리 database schema에 저장되어 있는 integrity constraints들을 모두 만족하는 instance들이다.
· Entity Integrity Constaints
- 각 relation schema의 PK와 관련된 제약조건이다.
- PK에 해당되는 attribute value는 NULL이 될 수 없다.
- relation instance r(R)에 있는 모든 tuple들은 t[PK] != NULL 즉, PK에 해당되는 값들이 NULL이 아니다.
- PK에 해당되는 attribute value는 그 relation tuple들을 식별하는데 사용되는 값이기 때문이다.
· Referential Integrity Constaints(참조 무결성)
- 2개의 relations(table) 사이의 제약조건
* 앞의 key constraint과 entity integrity constaint는 하나의 table에 대해서 정의되어 있는 constaint이다. key와 관련되어 있기 때문이다.
- 두 테이블에 있는 tuple간의 관계를 설명할 때 일관성을 유지하도록 만들어 주는 제약조건이다.
- 하나의 테이블에 속하는 tuple이 다른 테이블에 속하는 존재하는 tuple를 참조해야 한다. 존재하지 않는 tuple을 참조해서는 안 된다.
ex) EMPLOYEE에 있는 DNO attribute는 DEPARTMENT에 있는 존재하는 Dnumber를 참조해야한다.
- Foreign key (FK)
- relation schema R1과 R2에 대해서 다음을 만족시키는 attribute들의 집합이다.
1. R1에 있는 FK(DNO)는 참조당하는 relation R2의 PK와 같은 domain을 갖는다.
2. 참조하는 table R1의 tuple t1에 있는 FK는 참조되는 table R2의 tuple t2의 primary key값이어야 한다. (항상 존재하는 key를 참조해야한다.) 단, FK가 NULL인 것은 허용한다.
ex) employee의 dno가 foreign key이고 dno값은 department의 primary key인 dnumber 중 하나여야 한다. 어떤 employee의 DNO가 NULL인 것은 이 사람은 아직 발령을 받지 못했다는 뜻이다.
R1의 foreign key가 R2의 primary key를 참조한다.
즉, 참조하는 attribute는 항상 참조되는 attribute의 값들 중 하나거나 NULL이어야한다.
t1[FK] = t2[PK] (or NULL)
t1 refers to t2
정리!
· Referential Integrity Constaints(참조 무결성)
- foreign key를 설명하는 constraint이다.
- R1의 foreign key가 R2의 primary key를 참조할 때 FK에서 PK로 화살표를 그린다.
- Integrity Constraints
Summary
1. Relational Model Concepts
- Relations, tupes, attributes, domains, relational schemas, and relational instances
Relations: table이라고도 하는데 tuple들의 집합이다.
Tuple: attribute value들의 집합이다.
Attribute: 각각의 tuple을 설명하는 특성에 해당된다.
domain: 그 attribute값으로 쓸 수 있는 atomic한 값들의 집합
relational schemas: relational database schema는 relational schemas의 집합인데 이는 각각의 relation의 골격과 그것과 관련 된 integrity constaint으로 구성된다.
relational instances: relational schema를 따르는 instances들이다.
2. Characteristics of Relations
3. Relational Model Constraints
1) Key Constraints: 하나의 테이블 내에는 candidate key가 존재해야만 한다.
2) integrity constaint
2-1) Entity integrity constaint: primary, secondary key에 해당되는 attribute들은 NULL값을 가질 수 없다.
2-2) Referential integrity constaint: 두 개의 table간의 관계에 대해 일관성이 유지되어야한다. 참조하는 테이블의 FK는 참조당하는 table PK 값 중 하나이거나 NULL이어야한다.