출처: https://3months.tistory.com/307 [Deep Play]

4-1/데이터베이스시스템

Chapter 6 : ER Model

코딩하는 랄뚜기 2022. 3. 21. 21:08

Redundancy

데이터 베이스에서 중복(Reduandancy)은 무조건 피해야 하는 요소이다.

 

 

위는 고객, 상품, 주문의 data를 담고 있는 표이다. ID는 Primary key이기 때문에 제외하고 고객명과 상품명이 중복된 것을 알 수 있다.

 

 

이렇게 데이터가 중복되는 경우 위처럼 하나의 Relation의 값이 갱신되면 다른 relation과의 값이 불일치(inconsistency)되는 경우가 발생하게 된다.

 


 

ER Model

 

ER model에는 3가지 기본 컨셉이 있다.

  • entity sets
  • relationship sets
  • attributes

ER diagram을 통해 ER model을 시각적으로 표현할 수 있다.

 


 

Entity Sets

 

Entity란 다른 object들과 구별이 되는 object를 말한다.

 

Entity set공통된 특징을 가지고 있는 entity들의 모임이다.

 

Entity는 속성들로 표현 할 수 있다.

 

※예시

 

Entity set은 다음과 같이 ER Diagram으로 나타낼 수 있다.

 


 

Relationship Sets

 

Relationship은 각각의 entity들 간의 association이다. Entity의 primary key를 속성으로 갖는다.

 

※예시

 

※예시

(44553,2222) ∈ advisor

 

 

Relationship set은 entity의 primary key외에도 속성을 가질 수 있다.

다음은 instructor와 student의 relationship set인 advisor가 date속성을 가지고 있는 것을 표현한 것이다.

 

 

 

Relation을 이루는 Entity는 같아도 된다.

 

 

만약 과목들 중에 선이수 과목이 있는 것을 ER model로 나타낼 때 course entity만 사용하여 표현하면 된다.

여기서 course_id와 prereq_id를 Roles라고 부른다.

 

두 개 이상의 entity끼리도 Relationship이 생길 수 있는데 이것을 Non-binary Relationship이라고 한다.

 


 

Complex Attributes

속성들에도 여러가지 종류가 있다.

 

  • Simple and composite attributes
  • Single-valued and multivalued attributes
  • Drived attributes
    • Drived attributes란 나이, 생일 등 다른 속성들로 계산되어 질 수 없는 속성이다.

 

Composite attribute는 attribute를 subpart로 나눌 수 있게 해준다.

 

 


 

Mapping Cardinality

 

CardinalityRelationship set 안에서 Entity들의 association의 수를 나타낸다.

Binary relationship set에 대한 relationship set에는 4가지 종류가 있다.

  1. One to one
  2. One to many
  3. Many to one
  4. Many to many

위 사진들을 보고 알아야 할 것은 Constraint가 없고 단순한 one, many 관계에서는 모든 element가 association을 가질 필요가 없다는 것이다.

 

 

ER Diagram으로 Cardinality Constraint를 표현 할 수 있다.

 

왼쪽 (one to one), 오른쪽 (one to many)

 

->(화살표)는 one이라는 의미이고 -(선)은 many라는 의미이다.

 

 

한 줄은 partial participation이고 두 줄은 total participation이다. Total participation은 Entity의 모든 element들이 association을 가져야 한다는 의미이다.

 

 

줄 위에 x..y (x~y)를 사용하여 element가 가지는 association의 범위를 제한을 표현 할 수 있다. *는 아무 값이나 와도 상관없다는 것이다. 앞에 숫자(x)가 0이면 partial participation, 이 외의 숫자이면 total participation이라는 것을 직관적으로 알 수 있다.

 


 

Non-binary Relationship set의 경우 모호한 해석 때문에 one arrow를 하나 밖에 사용하지 못한다.

 

 

위 ER Digram은 3가지 해석이 가능하다.

  1. 각각의 instructor가 한 명의 student와 한 개의 project의 proj_guide가 된다.
  2. instructor와 student가 project의 proj_guide가 된다.
  3. instructor와 project가 student의 proj_guide가 된다.

이 해석이 맞는지는 모르겠지만.... 아무튼 모호한 해석 때문에 Non-binary Relationship에서는 one arrow를 한 개 밖에 사용하지 못하는 것만 기억하자.

 

 


 

Primary Key for Relationship set

 

Relationship set의 KeyRelation을 이루는 Entity들의 primary key와 Relation의 자체적인 속성들의 Union으로 이루어져 있다.

 

 

Binary Relation set의 경우 many to many를 제외하고는 one의 primary key가 Relationset의 primary key가 될 수 있다. (one이 unique한 값이기 때문). Many to many인 경우는 두 primary key의 union이 primary key가 된다.

 

Non-binary Relation set의 경우도 Binary Relation set과 같다.

 


 

Weak Entity Sets

 

데이터베이스에서 중복(Redundancy)은 피해야 할 요소 중 하나이다.

하지만 중복을 피하기 위해 겹치는 속성을 제거하게 되면 primary key가 존재하지 않는 entity set이 만들어 지는데 이러한 set을 Weak Entity Set이라고 한다.(Primary key가 존재하는 entity set은 strong entity set이라고 한다.)

 

Primary key가 존재하지 않기 때문에 primary key를 다른 entity에서 Foreign key로 받아와야 한다. 이 때 primary key를 받아오는 entity를 identifying entity라고 하고 이 관계를 identifying relationship이라고 한다.

 

Weak entity set에서 그나마 서로 구분 될 수 있는 속성들의 모임을 discriminator라 한다.

 

 

ER diagram으로 identifying relationship과 weak entity set을 나타낼 때는 두 개의 선을 이용하여 나타낸다. Weak entity set의 discrimintor는 점선으로 나타낸다.

 

Section의 primary key는 (course_id, sec_id, semester, year)가 된다.

'4-1 > 데이터베이스시스템' 카테고리의 다른 글

Chapter 2 : Reduction to Relation Schemas  (0) 2022.03.28
Chapter2 : Relational Algebra  (0) 2022.03.18
Chapter 2 Relation, Key  (1) 2022.03.14