youbing 2024. 11. 14. 02:13
본 내용은 유튜버 쉬운코드의 강의 "데이터베이스"를 참고하여 작성하였습니다.

 

relational data model


수학에서 relation

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

relational model에서 relation

  • Cartesian product

→ relation in mathematics : subset of Cartesian product == set of tuples

 

  • relational data model

domain 설정
domain/attribute 설정

  • domain으로 설정한 phone_numbers에 학생의 연락처와 비상 연락망도 적고 싶어서 이를 구분하기 위해 attribute를 사용
  • 근데 이걸 relation으로 나타내는 것보다 테이블이 더 효율적

relation을 테이블로 표현

 

주요 개념과 설명

  • domain : set of atomic values
  • domain name : domain 이름
  • attribute : domain이 relation에서 맡은 역할 이름
  • tuple : 각 attribute의 값으로 이루어진 리스트. 일부 값은 Null일 수 있다.
  • relation : set of tuples
  • relation name : relation의 이름

 

  • atomic -> 더 이상 나눌 수 없는 값들의 집합
  • relation(or relation state)이 튜플들의 집합으로써 데이터 자체를 가리키는 것일 수도 있지만 테이블 자체를 부를 수도 있음.

relation schema, … etc

relation schema

  • relation의 구조
  • relation 이름과 attributes 리스트로 표기 : 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.는 degree가 6

relational DB(관계형 DB)

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

 

relational DB schema

  • relation schemas set + integrity constraints set

relation의 특징들

  • relation은 중복된 tuple을 가질 수 없다. (Def of "relation" is a set of tuples)
  • relation에서 tuple의 순서는 중요하지 않다. ( " )
  • relation의 tuple을 식별하기 위해 attribute의 부분 집합을 key로 설정
    • 앞선 예제에서는 학생의 id로 식별
  • 하나의 relation에서 attribute의 이름은 중복되면 안된다.
  • 하나의 tuple에서 attribute의 순서는 중요하지 않다.
  • attribute는 atomic 해야 한다.
    • composite or multivalued attribute 허용 안됨. (e.g.) address(composite), major(multivalued)


Null의 의미

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

-> 여러 의미가 담겨 있기 때문에, 최대한 쓰지 않는 것이 좋음.


key 설명(기본키, 외래키 등)

super key

  • relation에서 tuples을 unique하게 식별할 수 있는 attributes set

 

candidate key (key or minimal super key)

  • 어느 한 attribute라도 제거하면 unique하게 tuples를 식별할 수 없는 super key

 

primary key (PK)

  • relation에서 tuples를 unique하게 식별하기 위해 선택된 candidate key
  • 보통 attribute 수가 적은 candidate key를 고름.
  • 밑줄로 표시함.

 

unique key (alternate key)

  • primary key가 아닌 candidate keys

 

foreign key

  • 다른 relation의 PK를 참조하는 attribute set

constraints 설명

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

 

implicit constraints

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

 

schema-based constraints (explicit constraints)

  • 주로 DDL을 통해 schema에 직접 명시할 수 있는 constraints
  • domain constraints : attribute의 value는 해당 attribute의 domain에 속한 value여야 한다.
    • ex) STUDENT라는 relation에서 grade라는 attribute는 100 같은 값을 가질 수 없다.
  • key constraints : 서로 다른 tuples는 같은 value의 key를 가질 수 없다.
  • Null value constraint : attribute가 Not Null로 명시됐다면 Null을 값으로 가질 수 없다.
  • entity integrity constraint : primary key는 value에 Null을 가질 수 없다.
  • referential integrity constraint : FK와 PK의 도메인이 같아야 하고, PK에 없는 values를 FK가 값으로 가질 수 없다.