관계형 DB

Single Ko·2023년 5월 2일
0

db

목록 보기
2/3

relation

relation이란 무엇인가?

Set

  • 서로 다른 elements를 가지는 collection
  • 하나의 set에서 elements의 순서는 중요하지 않다
  • e.g. {1, 3, 11, 4, 7}

binary relation ⊂ A x B
Cartesian product = set A,B 두개로 만들수 있는 모든 집합의 합
Tuple = 몇개의 엘리먼트들로 이루어진 리스트

relation in mathematics에서의 relation

  • subset of Cartesian product(Cartesian product의 부분 집합)
  • set of tuples (튜플들의 집합)

relational data model에서의 relation

  • Domain : element, 값들의 집합
  • 관계형데이터 모델에서는 도메인 마다 이름을 붙일 수 있음.
  • relation in mathematics와 같이 전체를 relation이라고 부른다.

student relation을 예로 본다.

relational data model

domain 정의

  • students_ids : 학번 집합, 7자리 integer 정수
  • human_names : 사람 이름 집합, 문자열
  • university_grades : 대학교 학년 집합, {1, 2, 3, 4}
  • major_names : 대학교에서 배우는 전공 이름 집합
  • phone_numbers : 핸드폰 번호 집합

왜 phone number가 두개인가? -> 학생의 번호말고 비상연락망으로 사용할 번호 이때 이름이 중복됨.

attriubte

  • 각각의 domain들이 relation에서 어떤 역할을 하는지 이름을 붙여 주는 것이 속성이다.

table

✨ 왜 테이블인가?

  • relational data model에서 가장 표현하기 좋은 방식
  • 위의 예제들은 표현하기에 좋은 방식이 아니다.
  • 그래서 relation data model에서 relation을 table로 많이 이야기하는 이유

주요 개념                      설명
domain                set of atomic values
domain name                domain 이름
attribute         domainl relation에서 맡은 역할 이름
tuple        각 attribute의 값으로 이루어진 리스트. 일부 값은 NULL일 수 있다
relation                  set of tuples
relation name             relation의 이름

relation schema

정의

  • relation의 구조를 나타낸다
  • relation 이름과 attributes 리스트로 표기된다
  • e.g. STUDENT(id, name, grade, major, phone_num, emer_phone_num)
  • attributes와 관련된 constraints도 포함한다

degree of a relation

  • relation schema에서 attributes 수
  • e.g. STUDENT(id, name, grade, major, phone_num, emer_phone_num) -> degree 6

relational database

  • relational data model에 기반하여 구조화된 database
  • relational database는 여러 개의 relations로 구성된다.

relational database schema

  • relation schemas set + integrity constraints set

relation의 특징들

특징

  1. relation은 중복된 tuple을 가질 수 없다.(relation is set of tuples)

    • 서로 다른 튜플인데 그 튜플의 값이 모두 동일한 것은 있을 수 없다.
  2. relation의 tuple을 식별하기 위해 attribute의 부분 집합을 key로 설정한다

    • id를 통해서 각 튜플을 유니크 하게 구분되어 식별
  3. relation에서 tuple의 순서는 중요하지 않다

    • 순서를 정하는 것은 여러가지 방법이 있을 수 있다.
  4. 하나의 relation에서 attribute의 이름은 중복되면 안된다.

  5. 하나의 tuple에서 attribute의 순서는 중요하지 않다.

  6. attribute는 atomic해야 한다(composite or multivalued attribute 허용 안됨)

    composite -> address에서 '서울특별시', '강남구' '청담동' 으로 나눌 수 있음

    multivalued attribute -> 하나의 attribute에서 컴공, 디자인과 같이 multivalued가 들어가면 안됨.

NULL의 의미

  • 값이 존재하지 않는다
  • 값이 존재하나 아직 그 값이 무엇인지 알지 못한다
  • 해당 사항과 관련이 없다

Key

superkey

  • relation에서 tuples를 unique하게 식별할 수 있는 attributes set
  • e.g. PLAYER(id, name, team_id, back_number, birth_date)의 superkey는?
    → {id, name, team_id, back_number, birth_date} , {id, name} , {name, team_id, back_number}, ... etc

candidate key

  • 어느 한 attribute라도 제거하면 unique하게 tuples를 식별할 수 없는 superkey
  • key or minimal superkey라 하기도 한다
  • e.g. PLAYER(id, name, team_id, back_number, birth_date) candidate key
    → {id}, {team_id, back_number}

primary key

  • relation에서 tuples를 unique하게 식별하기 위해 선택된 candidate key
  • e.g. PLAYER(id, name, team_id, back_number, birth_date) primary key
    → {id} or {team_id, back_number}

unique key

  • primary key가 아닌 candidate keys
  • alternate key
  • e.g. PLAYER(id, name, team_id, back_number, birth_date) unique key
    → {team_id, back_number}

foreign key

  • 다른 relation의 PK를 참조하는 attributes set
  • e.g. PLAYER(id, name, team_id, back_number, birth_date)와 TEAM(id, name, manager)가 있을 때
    → foreign key PLAYER {team_id}

constraints 뜻

  • relational database의 relations들이 언제나 항상 지켜줘야 하는 제약 사항

implicit constraints

  • relational data model 자체가 가지는 constraints
  • relation은 중복되는 tuple을 가질 수 없다
  • relation 내에서는 같은 이름의 attribute를 가질 수 없다

schema-based constraints

  • 주로 DDL을 통해 schema에 직접 명시할 수 있는 constraints

  • explicit constraints

  • schema-based constraints의 종류에 대해 알아보겠다.

    1. domain constraints

      • attribute의 value는 attribute의 domain에 속한 value여야 한다
    2. key constraints

      • 서로 다른 tuples는 같은 value의 key를 가질 수 없다
    3. NULL value constraint

      • attribute가 NOT NULL로 명시됐다면 NULL을 값으로 가질 수 없다
    4. entity integrity constraint

      • primary key는 value에 NULL을 가질 수 없다
    5. referential integrity constraint

      • FK와 PK와 도메인이 같아야 하고 PK에 없는 values를 FK가 값으로 가질 수 없다
profile
공부 정리 블로그

0개의 댓글