[DataBase] 관계형 모델 소개
[DataBase] 관계형 모델 소개
관계형 데이터베이스 (Relational Database)
- 관계형 데이터베이스는 테이블(Table)의 모임으로 구성
- Table
- 고유한 이름을 갖고 있습니다.
- 예. instructor table(교수 table), department table(부서 table)
- Example of a Table (Instructor Table)
Relational Model (관계형 모델)
- Table = Relation
- Column = Attribute
- Row = Tuple
- Example of a Relation
Attribute Types(1)
-
각 속성에 대해 허용된 값 집합을 도메인(domain) 이라고 합니다.
-
예) 급여 attribute
- salary = 95,000 (ok)
- salary = -10,000 (not ok)
- salary = Moart (not ok)
-
Attribute Types(2)
- 속성 값은 (일반적으로) atomic(원자적) 이어야 합니다.
- 분할이 불가능합니다.
- 예) 핸드폰 번호 attribute(속성)
- 82 - 2 - 780 - 7590
- phone_number = {82, 2, 780-7590} (not ok)
- phone_number = 82–2–780-7590 (ok)
Attribue Types(3)
- 특수 값 null 은 모든 도메인의 구성원입니다.
- Unknown
- 존재하지 않습니다. (Does not exist)
Relation Schema and Instance
struct instructorInfo {
int ID;
char name[100];
char dept_name[100];
int salary;
}; // type definition : 구조체 (structure)
instructorInfo inst[1000]; // 변수
- Type definition -> Relation Schema -> 고정적임
- Variable -> Relation Instance -> 시간에 따라 변함
Relation Schema
-
A1, A2, …, An are attributes
- A1 - ID
- A2 - name
- A3 - dept_name
- A4 - salary
-
R(A1, A2, …, An ) 이 realtion schema 입니다.
-
예) instructor(ID, name, dept_name, salary)
-
department(dept_name, building, budget)
-> dept_name : join에 사용됩니다.
-
Relation Instance
- 주어진 시간에 데이터베이스에 있는 데이터의 요약
DataBase
-
데이터베이스는 여러 관계로 구성됩니다.
-
기업에 대한 정보는 여러 부분으로 나뉩니다.
-
Good Design
- instructor (instructor-ID, name, dept_name)
- student (student-ID, name)
- advisor (instrcutor-ID, student-ID)
-
Bad Design (Data Modeling)
-
univ (instructor-ID, instructor-name, dept_name, student-id, student-name)
-
null 값의 필요성(예. advisor가 없는 학생을 나타냅니다.)
-
정보의 반복(예. 두 명의 학생이 동일한 강사를 가지는 경우)
-> Data Modeling : Normalization
-
Keys
- 하나의 tuple을 다른 tuple들로부터 구별하는 방법
- Student number -> O
- SSN(Social security number, 주민등록번호) -> O
- Email address -> O
- Student name -> X
- {Student numebr, Student name} -> O
- superkey, candidate key, primary key, foreign key
1. SuperKey
: 관계에서 튜플을 고유하게 식별할 수 있는 속성 세트
- instructor (ID, name, dept_name, salary, SSN)
- {ID} -> superkey
- {SSN} -> superkey
- {ID, name} -> superkey
- {ID, SSN} -> superkey
- {SSN, name, dept_name} -> superkey
- {name}
- {name, dept_name}
2. Candidate Key
- : 관계에 대한 최소 superkey
-
적절한 하위 집합이 superkey가 아닌 superkey
즉, 관계 내에서 튜플을 고유하게 식별하는 속성 또는 최소한의 속성 집합
- instructor (ID, name, dept_name, salary, SSN)
- {ID} -> superkey -> candidate key
- {SSN} -> superkey -> candidate key
- {ID, name} -> superkey
- {ID, SSN} -> superkey
- {SSN, name, dept_name} -> superkey
3. Primary Key
: 관계 내에서 교유하게 튜플을 식별하기 위해 선택된 후보키(candidate key)
- instructor (ID , name,dept_name, salary, SSN)
- department (dept-name, building, budget)
- advisor (instructor-ID, student-ID)
- classroom (building, room-number, capacity)
4. Foreign Key
: 관계는 속성 사이에 다른 관계의 기본키를 포함할 수 있습니다.
-
instructor (instructor-ID , name,dept_name, salary)
-
student (student-ID, name)
-
advisor (instructor-ID , student-ID)
-> Foreign Key
-
Primary Key <- Foreign Key
Relational Algebra (관계대수)
- Relation에 수행하는 연산의 집합
- Selection (선택 연산)
- Projection (추출 연산)
- Natural Join (자연 조인)
- Cartesian product (카티션 곱)
- Union (합집합)
- Intersection (교집합)
- Set difference (차집합)
1. Selection (선택 연산) - σ
: 선택 조건을 만족하는 Relation의 tuples을 출력한다.
-
σcondition(relation)
-
instructor relation에서 “급여가 $85,000보다 높은 조건”을 만족하는 tuples
-
σsalary > $85,000 (instructor)
salary > $85,000 -> condition
instructor -> relation
-
σsalary > $85,000 Λ dept_name=”Physics”(instructor)
Λ -> And
V -> Or
-
2. Projecion (추출 연산) - Π
: Relation에서 선택된 attributes를 출력한다.
-
Πattribute-list(relation)
-
instructor relation에서 교수의 ID와 salary 만 필요한 경우
- ΠID, salary(instructor)
3. Natural Join (자연 조인) - 
: 같은 이름을 갖고 있는 attributes에서 두 relations이 같은 값을 갖고 있는 tuples의 쌍을 출력한다.
- relation1
relation2
4. Cartesian product (카티션 곱) - x
: 두 relations 부터 가능한 모든 tuple의 쌍을 출력한다.
- relation1 x relation2
5. Union (합집합) - U
: 두 relations의 tuples에 대해서 합집합을 수행한다.
- relation1 U relation2
6. Intersection (교집합) - 
: 두 relations의 tuples에 대해서 교집합을 수행한다.
- relation1
relation2
7. Set difference (차집합)
: 두 relations의 tuples에 대해서 차집합을 수행한다.
- relation1 - relation2
예)
Leave a comment