관계형 DB
JeongSeulho
2025년 01월 02일
준비중...
클립보드로 복사
수학에서의 relation
- 카티션 곱(Cartesian Product)의 부분 집합
- 또는 튜플(tuple)의 집합
카티션 곱(Cartesian Product)
- 두 집합의 모든 가능한 조합을 생성하는 연산
(1, p),(1, q),(1, r),(2, p),(2, q),(2, r)이 위 2개의 집합에서 카티션 곱- relation은 위 카티션 곱의 부분 집합, 현재 2개의 집합이므로
binary relation
튜플(tuple)
- 위의 그림에서 각 페어를 tuple이라고 함
(1, p)등 각 페어- relation은 tuple의 집합
relation data model
- 학생을 예시로 설명
도메인 정의하기
- 수학의 집합을
relation data model에서는 도메인(domain)을 의미 - 도메인 정의 :
student_ids,human_names,university_grades,major_names,phone_numbers - 각 도메인에는 정해진 데이터 타입이 있음(
university_grades는 1 ~ 4 사이의 정수)
속성(attribute) 정의하기
- 동일한 도메인이 2번 사용되는 것을 구분하기 위해 속성(attribute)을 사용
- 속성 정의 :
id,name,grade,major,phone_number,emer_phone_number=>phone_number와emer_phone_number는 동일한 도메인이고 다른 속성으로 정의
tuple
- 각 속성에서 하나씩 값을 선택한 리스트 또는 집합
- tuple을 쉽게 표현하기 위해
테이블(table)을 사용
테이블
relation data model에서 tuple을 쉽게 표현하기 위해 테이블을 사용- 즉
relation은 곧table을 의미
attribute(column): id, name, grade, major, phone_number, emer_phone_numbertuple(row): (1, 홍길동, 3, 컴퓨터공학, 010-1234-5678, 010-1234-5678)relation(table): tuple의 집합relation(table) name: student
relation schema
- relation의 구조를 나타내며 relation의 이름과 attribute 리스트로 표기
STUDENT(id, name, grade, major, phone_number, emer_phone_number)- attribute과 관련된
constraint도 포함
degree of a relation
- relation의 속성 개수
- id, name, grade, major, phone_number, emer_phone_number로 6개
relation state
- 특정 시점에서의 relation의 상태
- 문맥에 따라 그냥
relation또는relation state,relation snapshot등으로 지칭
relation DB
- 위의
relation data model에 기반하여 구조화된 DB - 여러개의
relation으로 구성
relation DB schema
relation schema의 집합과integrity constraint의 집합을 합쳐 정의relation schema예시 :STUDENT(id, name, grade, major, phone_number, emer_phone_number)integrity constraint예시 :id는 유일한 값이어야 함
relation DB 특징
중복 tuple 허용 X
- relation 자체가 tuple의 집합이므로 정의에 어긋남
attribute의 부분 집합을 key로 사용
- 각 tuple을 구분하기 위해 attribute의 부분 집합을 unique하게 정의된 key로 사용
- 예시 :
id는 유일한 값이어야 함 - 복수개의 attribute를 합쳐서 key로 사용하는 경우도 있음
tuple의 순서는 중요하지 않음
- 어떤 순서로 정렬되어도 동일한 의미를 가진다
- 정렬하는 방법이 여러가지일 수 있음
중복되는 attribute는 허용 X
하나의 tuple에서 attribute의 순서는 중요하지 않음
- attribute의 순서가 변경되어도 동일한 의미를 가진다
attribute는 atomic해야 한다
-
composite attribute는 허용하지 않음composite attribute: 더 세부적으로 나눌 수 있는 attribute- 예시 :
address의서울시 강남구 청담동은composite attribute이고서울시,강남구,청담동으로 분리해야 함
-
multi-valued attribute는 허용하지 않음multi-valued attribute: 하나의 tuple에서 여러개의 값을 가질 수 있는 attribute- 예시 :
major의컴퓨터공학,경영학은multi-valued attribute이고컴퓨터공학과경영학으로 분리해야 함 => 테이블 분리, 중계 테이블을 사용 등
NULL의 의미
- 값이 존재 하지 않음
- 예시 :
test_score가 NULL => 테스트를 응시한 이력이 없어 점수가 없음
- 예시 :
- 값이 존재하나 알 수 없음
- 예시 :
test_score가 NULL => 테스트를 응시하여 점수가 있지만 아직 등록되지 않음, 누락됨
- 예시 :
- 해당 사항과 관련이 없음
- 예시 :
test_score가 NULL => 테스트 응시 자격이 없어 관련 없음
- 예시 :
super key
- relation에서 tuple을 unique하게 식별하는 attribute 집합
- 예시 :
PLAYER(id, name, team_id, position, height, weight, birth_date)에서 super keyid=> 항상 unique한 값(id, name)=> id가 unique하므로 id를 포함하는 어떤 조합이라도 유일한 값(name, team)=> 같은 팀에서 동명이인이 없다면 가능- 등등
candidate key(minimal super key)
- super key 중에서 한가지라도 attribute를 삭제하면 튜플이 unique하지 않게 되는 super key
- super key 중에서도 최소한의 attribute 집합
- 예시 :
PLAYER(id, name, team_id, position, height, weight, birth_date)에서 candidate keyid=> 항상 유일한 값(name, team_id)=> 다른 팀에서 동명이인이 있다면team을 삭제하면 유일하지 않게 됨
primary key
- relation에서 각 tuple을 유일하게 식별하기 위해 선택된 candidate key
alternate key(unique key)
- primary key로 선택되지 않은 candidate key
foreign key
- 다른 relation의 primary key를 참조하는 attribut 집합
- 예시 :
PLAYER(id, name, team_id, position, height, weight, birth_date)에서team_id는TEAM(id, name, coach, stadium)의id를 참조하는 foreign key
constraint
- relational DB의 relation들이 항상 지켜야할 제약 사항
implicit constraint
- relation data model 자체가 가지는 제약 사항
- 중복 tuple 허용 X, 한 relation에서 같은 이름의 attribute는 허용 등
explicit constraint
- DDL을 통해 schema에 직접 명시할 수 있는 제약
domain constraint
- attribute의 값은 해당 attribute의 domain에 속하는 값이어야 함
key constraint
- 다른 tuple은 같은 key 값을 가지지 않아야 함
NULL value constraint
- attribute가
NOT NULL로 명시되면 NULL 값을 가지지 않아야 함
entity integrity constraint
PK(primary key)는 NULL 값을 가지지 않아야 함
referential integrity constraint
FK(foreign key)와PK(primary key)는 같은 도메인이어야 하고, FK는 맵핑되는 PK가 존재해야 함