공대생의 공부흔적

[DB#2] 관계형 모델(Relational Model) 본문

Database & Big Data

[DB#2] 관계형 모델(Relational Model)

생대공 2024. 3. 7. 17:07

참고: Database Systems: The Complete Book (2nd edition)
이번 글에서는 지난 글에서 살펴본 데이터 모델 중 관계형 모델에 대해 알아볼 것이다.
2024.03.07 - [Database & Big Data] - [DB#1] 데이터 모델 (Data Model)


Basics/용어 정리

  • Relation: 데이터를 포함하는 이차원 표
  • Schema: relation 이름과 attribute의 집합 e.g. Movies(title, year, length, genre)
  • Database schema: 데이터베이스의 relation을 위한 schema의 집합
  • Tuple: 각 attribute별로 원소 하나씩을 포함하고 있다. e.g. (Star Wars, 1977, 124, sciFi)
  • Instance: relation에서 tuple의 집합. 보통 데이터베이스 시스템에서는 현재 인스턴스만 유지하고 있는다.

즉, relation은 tuple의 집합, schema는 attribute의 집합이다. 따라서 relation에서 tuple이나 attribute의 순서는 중요하지 않다. (예를 들어 4개의 attribute와 3개의 tuple을 갖는 relation의 경우 가능한 표현 방법 수는 4!*3!=144가지이다.)

  • 다음 표에서 각 행은 rows/tuples/records라 부르고, 각 열은 columns/attributes/fields라 부른다. 각 attribute는 정수, 문자열과 같은 특정 type을 가진다. 다만 set, list, array와 같이 더 작은 구성요소들로 나눠질 수 있는 타입은 불가하다.
Moviestitleyearlengthgenre
 Gone with the Wind1939231drama
 Star Wars1977124sciFi
 Wayne's World199295comedy

<Figure 1> Movies Relation

Relation의 key

  • 고유하게 기록을 식별하는 attribute의 집합.
  • 보통 실제 데이터베이스에서는 인위적으로 만든 key를 ID처럼 사용한다.
    • e.g.아래 표에서 movieId
  • 여러 key를 사용할 수 있지만, primary key는 딱 한 가지의 attribute만 될 수 있다.
    • e.g. movieId가 primary key라면, title, year 등 나머지 attribute는 key로 사용하는 경우 primary key가 아닌 일반 key가 된다.
MoviesmovieIdtitleyearlengthgenre
 111Gone with the Wind1939231drama
 123Star Wars1977124sciFi
 456Wayne's World199295comedy

<Figure 2> key가 포함된 Movies Relaion

  • Foreign key: 값이 다른 relation의 정보의 key인 attribute.
    • e.g. 위 Figure 2의 Movies relation과 아래 StarsIn relation이 있는 경우, StarIn의 movieMovies.movieId에 대한 foreign key가 된다.
StarsInactormovie
 John123
 Will456

<Figure 3> StarsIn Relation

 

Relational Model의 두 가지 구현 방법

앞서 attribute와 tuple은 순서가 없다고 했다. 즉, 관계형 모델의 표는 row major로도, column major로도 구현될 수 있다.
아래 데이터를 각각 두 가지 방식으로 구현하는 경우 어떻게 조직되는지 알아보자.

Moviestitleyearlengthgenre
 Gone with the Wind1939231drama
 Star Wars1977124sciFi
 Wayne's World199295comedy

Row-major 구현

지금까지 계속 사용되었던 방식이다. 오브젝트가 array를 이루고 있어, tuple 단위의 삽입과 삭제가 용이하다.

Gone with The Wind
1939
231
drama
Star Wars
1977
124
sciFi
Wayne's World
1992
95
comedy

Column-major 구현

반대로 column major 방식은 attribute당 하나의 array를 이루고 있어 attribute별로 데이터를 수정하고자 할 때 용이하다.

Gone with The Wind / Star Wars / Wayne's World
1939 / 1977 / 1992
231 / 124 / 95
drama / sciFi / comedy

두 방법 중 각 상황에 맞게 더 적합해 보이는 방식으로 구현하면 될 것으로 보인다!

'Database & Big Data' 카테고리의 다른 글

[DB#1] 데이터 모델 (Data Model)  (2) 2024.03.07