[DB] 쉬운코드 Relational Database 개념 및 기본키, 외래키, 제약조건

대원·2023년 9월 14일

DB

목록 보기
3/4

Relation

  • 기본적으로 수학의 개념에서 파생
  • 중고교 시절 배웠던 정의역↔치역해서 선(관계)를 그렸던 그림을 생각하면 이해가 빠름.

Relational Data model

  1. Domain(=Set)
    • Atomci한 Value들의 집합
  2. Attribute
    • Domain에서 맡은 역할에 따라 세부화
    • ex)같은 핸드폰번호여도 응급연락책을 테이블에 넣는 경우
      phone_num, emr_phone_num로 세부화 할 수 있음
  3. Tuple
    • Attribute의 List
    • 자바언어로 따지면 대응하는 것은 일종의 인스턴스들?
  4. Relation
    • 위의 Tuple(Instance)들의 집합
    • 쉽게 말해 Table

하단의 그림을 보면 전술한 내용이해에 어려움이 없다.

Relation Schema

  • Relation의 구조를 나타냄
  • Relation의 이름과 attributes의 리스트로 표기
  • Ex) STUDENT(id, name, …)
    • 시각적으로 표현되진 않지만 속성과 관련된 constraints도 포함

Degree(차수) of a relation

  • Relation Schema에서 attributes의 수
  • ex) STUDENT(id, name… phone_num) →속성 값이 6이라면 degree가 6

Relation(Or Relation State)

  • 위의 그림자체로 Table을 의미할 수도 있지만, 문맥에 따라서는
    현재 Table이 갖고 있는 Set of Tuples를 의미할 수도 있음.
  • 문맥에 따라 잘 해석하자.

Relational Database

  • relational data model에 기반하여 구조화된 database
  • 관계형 DB는 여러 개의 relation으로 구분

Relational database schema

  • relation schemas set + Integrity constraints, set
    • relation schema의 집합 + 여러개의 Relation 간의 Constraints, set

Relation의 특징들

  • 중복된 튜플을 가질 수 없다.
    • relation 자체가 Tuple들의 집합(Set)이므로 중복 불가
  • relation의 Tuple을 식별하기 위해 attribute의 부분집합을 Key로 설정
  • relation에서 tuple의 순서는 중요 X
  • 하나의 relation에서 attribute의 이름은 중복되면 x 또한 순서가 중요하지 않는다.
  • attribute는 항상 atomic해야함.

Null의 의미

  • 값이 존재하지 않거나 존재해도 알지 못하는 것
  • cf) is null로 선언

superkey

  • relation에서 tuples를 Unique하게 식별할 수 있는 attributes set(복수임)
  • ex) Student(id, name, sex,..)에서 super key는 unique하게 식별할 수 있는 부분집합 모두 가능

Candidate key

  • 어느 한 attribute라도 제거하면 Unique하게 식별할 수 없는 super key
    • key or minimal superkey
    • ❓통상적으로 우리가 알고 있는 pk개념인듯?

Primary key

  • Relation에서 tuples를 Unique하게 식별하기 위해 선택된 candidate key
    • 통상 attribute 숫자가 적은 것을 PK로 지정하긴 함

Unique key

  • PK가 아닌 다른 candidate keys
    • alternate key
  • ex)PLAYER(id, name, team_id, back_number,birth_date)에서 PK가 Id였다면
    unique key는 (team_id, back_number)

Foreign key

  • 다른 Relation의 PK를 참조하는 attributes set
  • ex) PLAYER(id, name, team_id, back_number,birth_date), TEAM(id, name, manager)가
    있고 연관관계가 있을 때, PLAYER의 FK는 TEAM의 id가 될 수 있다.

Constraints(→ 데이터의 일관성 및 정합성 보장 위함)

  • Relational Database에서 Relation이 반드시 준수해야되는 사항
  1. implicit constraints
    • Relational data model 자체가 가지는 constraints
    • ex) Relation은 중복되는 Tuple을 가질 수 없다.
  2. schema-based constraints
    • explicit constraints
    • DDL를 통해 schema에 직접명시
    1. domain constraints
      • attribute value는 항상 도메인 그 자체에 속해야함
    2. key constraints
      • 서로 다른 Tuples는 같은 value의 Key를 가질 수 없다.
    3. Null value constraint
      • NOT NULL
    4. Entity integrity constraint
      • PK는 Value에 null을 가질 수 없다.
      • 즉 PK는 Null을 가지면 안된다.
    5. Referential integrity constraint
      • FK와 PK와 도메인이 같아야 하고 PK에 없는 values를 FK가 값으로 가질 수 없다.
      • FK에 있을 수 있는 값은 FK가 참조하는 PK의 존재하는 값을 참조해야한다.
      • 즉 없는 값을 참조하면 안된다.

참조
쉬운코드 데이터베이스 2강

profile
고민하고 공부하는 사람

0개의 댓글