cs

[데이터베이스] 관계형 모델 소개

2025-04-22
3분 분량
데이터베이스

1. 관계형 모델 소개

1.1 관계형 모델 개요

  • 상업용 데이터 처리 응용을 위한 주요 데이터 모델
  • 네트워크 모델이나 계층형 모델에 비해 프로그래머 작업을 쉽게 해주는 단순성으로 널리 사용됨
  • 객체 지향 특징 지원: 복잡한 데이터 타입, 저장형 프로시저
  • XML 데이터 및 반구조형 데이터 지원

2. 관계형 데이터베이스 구조

2.1 릴레이션의 구조

  • 관계형 데이터베이스: 테이블(table)들의 집합
  • 각 테이블은 고유한 이름을 가짐
  • 튜플(tuple): 테이블의 행
  • 속성(attribute): 테이블의 열
  • 릴레이션(relation): 테이블을 의미하는 용어
  • 릴레이션 인스턴스(relation instance): 특정 시점의 튜플 집합
  • 튜플 순서와 무관, 동일한 튜플 집합이면 같은 릴레이션

속성과 도메인

  • 속성(attribute)는 도메인(domain)이라는 허가된 값의 집합을 가짐
  • 예: salary 속성의 도메인 → 가능한 모든 salary 값
  • 도메인은 원자적(atomic)이어야 함 → 더 이상 나뉠 수 없는 단위
  • 원자성 예시:

널 값 (null value)

  • 알려지지 않거나 존재하지 않는 값을 의미하는 특별한 값
  • 접근이나 갱신 시 어려움을 초래할 수 있으므로 가능하면 제거하는 것이 바람직함

3. 데이터베이스 스키마

3.1 스키마와 인스턴스

  • 데이터베이스 스키마(database schema): 논리적 설계 → 릴레이션들의 스키마 집합
  • 데이터베이스 인스턴스(database instance): 특정 시점의 릴레이션 인스턴스 집합 → 데이터 스냅샷
  • 릴레이션 스키마(relation schema): 속성과 도메인 명세의 집합
  • 릴레이션 인스턴스: 시간에 따라 튜플이 변화 가능
  • 스키마는 일반적으로 변하지 않음

예시:

  • department 릴레이션 스키마:
  • 공통 속성을 통한 릴레이션 연결:
  • 대학교 데이터베이스 주요 릴레이션:

4. 키 (Key)

4.1 키의 개념과 종류

  • 키(key): 릴레이션의 튜플을 유일하게 식별하는 속성 또는 속성 집합

키의 종류:

  1. 수퍼 키(super key):
  2. 후보 키(candidate key):
  3. 주 키(primary key):
  4. 외래 키(foreign key):

키의 예시:

  • classroom(building, room_number, capacity) → 주 키: (building, room_number)
  • time_slot(time_slot_id, day, start_time, end_time) → 주 키: (time_slot_id, day, start_time)

5. 스키마 다이어그램

5.1 스키마 다이어그램 개요

  • 데이터베이스 스키마를 시각적으로 표현
  • 릴레이션 → 네모 상자
  • 주 키 → 밑줄 표시
  • 외래 키 → 화살표
  • 참조 무결성 제약 조건 → 이중 화살표

6. 관계형 질의어

6.1 질의어의 종류

  • 질의어(query language): 데이터베이스에서 정보를 요청할 때 사용
  • 명령형, 함수형, 선언형 질의어 존재

명령형 질의어:

  • 연산 수행 순서를 명시

함수형 질의어:

  • 함수 실행으로 계산 → 부작용 없음

선언형 질의어:

  • 필요한 정보만 기술, 수행 방법은 DBMS가 결정

7. 관계 대수

7.1 단항 연산

선택 (Selection)

조건을 만족하는 튜플을 선택

기호: σ\sigma

  • 물리학과 교수들: σdept_name="Physics"(instructor)\sigma_{\text{dept\_name} = \text{"Physics"}} (\text{instructor})
  • 급여가 90,000 이상인 교수들: σsalary≥90000(instructor)\sigma_{\text{salary} \geq 90000} (\text{instructor})
  • 물리학과에 속하고 급여가 90,000 이상인 교수들: σdept_name="Physics"∧salary≥90000(instructor)\sigma_{\text{dept\_name} = \text{"Physics"} \land \text{salary} \geq 90000} (\text{instructor})
  • 학과명과 건물명이 같은 학과: σdept_name=building(department)\sigma_{\text{dept\_name} = \text{building}} (\text{department})

추출 (Projection)

특정 속성만 선택 (중복 제거)

기호: π\pi

  • 모든 교수의 ID, 이름, 급여: πID,name,salary(instructor)\pi_{\text{ID}, \text{name}, \text{salary}} (\text{instructor})
  • 교수들의 월급 (연봉 ÷ 12): πID,name,salary/12(instructor)\pi_{\text{ID}, \text{name}, \text{salary} / 12} (\text{instructor})
  • 물리학과 교수들의 이름: πname(σdept_name="Physics"(instructor))\pi_{\text{name}} \left( \sigma_{\text{dept\_name} = \text{"Physics"}} (\text{instructor}) \right)

7.2 이항 연산

카티션 곱 (Cartesian Product)

두 릴레이션의 모든 튜플 조합

기호: ×\times

  • instructor와 teaches의 카티션 곱: instructor×teaches\text{instructor} \times \text{teaches}

조인 (Join)

조건을 만족하는 튜플끼리 결합

기호: ⋈\bowtie

  • 교수가 강의하는 수업 정보: instructor⋈instructor.ID=teaches.IDteaches\text{instructor} \bowtie_{\text{instructor.ID} = \text{teaches.ID}} \text{teaches}
  • 물리학과 교수들이 강의하는 수업: σdept_name="Physics"(instructor⋈instructor.ID=teaches.IDteaches)\sigma_{\text{dept\_name} = \text{"Physics"}} \left( \text{instructor} \bowtie_{\text{instructor.ID} = \text{teaches.ID}} \text{teaches} \right)
  • 동등 질의: (σdept_name="Physics"(instructor))⋈instructor.ID=teaches.IDteaches\left( \sigma_{\text{dept\_name} = \text{"Physics"}} (\text{instructor}) \right) \bowtie_{\text{instructor.ID} = \text{teaches.ID}} \text{teaches}

집합 연산 (Set Operations)

합집합 (Union)

기호: ∪\cup

  • 2017년 가을 또는 2018년 봄에 개설된 수업:

πcourse_id(σsemester="Fall"∧year=2017(section))∪πcourse_id(σsemester="Spring"∧year=2018(section))\pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Fall"} \land \text{year} = 2017} (\text{section}) \right) \cup \pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Spring"} \land \text{year} = 2018} (\text{section}) \right)


교집합 (Intersection)

기호: ∩\cap

  • 두 학기 모두 개설된 수업:

πcourse_id(σsemester="Fall"∧year=2017(section))∩πcourse_id(σsemester="Spring"∧year=2018(section))\pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Fall"} \land \text{year} = 2017} (\text{section}) \right) \cap \pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Spring"} \land \text{year} = 2018} (\text{section}) \right)


차집합 (Difference)

기호: −-

  • 2017년 가을에만 개설된 수업:

πcourse_id(σsemester="Fall"∧year=2017(section))−πcourse_id(σsemester="Spring"∧year=2018(section))\pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Fall"} \land \text{year} = 2017} (\text{section}) \right) - \pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Spring"} \land \text{year} = 2018} (\text{section}) \right)


배정 (Assignment)

릴레이션을 임시 변수에 저장

기호: ←\leftarrow

courses_fall_2017←πcourse_id(σsemester="Fall"∧year=2017(section))\text{courses\_fall\_2017} \leftarrow \pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Fall"} \land \text{year} = 2017} (\text{section}) \right)

courses_spring_2018←πcourse_id(σsemester="Spring"∧year=2018(section))\text{courses\_spring\_2018} \leftarrow \pi_{\text{course\_id}} \left( \sigma_{\text{semester} = \text{"Spring"} \land \text{year} = 2018} (\text{section}) \right)

courses_fall_2017∩courses_spring_2018\text{courses\_fall\_2017} \cap \text{courses\_spring\_2018}


재명명 (Rename)

릴레이션 또는 속성 이름을 새 이름으로 변경

기호: ρ\rho

  • ID가 12121인 교수보다 더 많은 연봉을 받는 교수들의 ID와 이름:

πi.ID,i.name(σi.salary>w.salary(ρi(instructor)×σw.ID=12121(ρw(instructor))))\pi_{i.\text{ID}, i.\text{name}} \left( \sigma_{i.\text{salary} > w.\text{salary}} \left( \rho_{i} (\text{instructor}) \times \sigma_{w.\text{ID} = 12121} (\rho_{w} (\text{instructor})) \right) \right)

함께 읽으면 좋은 글