[DB] CH7. ER 모델(Entity-Relationship Model)
ER 모델에서 Entity, Relationship이란?
- DB는 다음의 두가지로 구성될 수 있다.
- collection of entities
- relationship among entities
- ER 모델을 사용하면 "관계성"에 기반한 구조화로 인해 high-level 단계에서 좀 더 visualize하게 나타낼 수 있다.
- Entity : DB에 존재하며 다른 것들과 구별되는 object(쉽게 tuple(row)로 생각하면 된다.), entity는 attribute를 갖는다. 예컨대, people는 name와 address를 갖는다. EX) instructor=(ID, name, street, city, salary)
- Entity set : set of entity(table과 비슷하다 보면 된다.)
- Relationship : entity들 간의 관계를 나타내는 association
- binary relationship(degree of relationship set) : 일반적으로 두 entity set 간의 relationship인 경우가 많다.
- Relationship set : set of relationship
Entity의 Attribute type엔 어떤 것들이 있나?
- Simple/Composite attribute
- Single-valued/Multi-valued attribute : 예컨대, phone_numbers라는 attribute는 multivalued이다. 왜냐하면 폰을 여러개 가지고 있을 수도 있기 때문이다.
- Derived attribute : 다른 attribute로부터 계산해 구해질 수 있는 것 EX) date_of_birth가 주어졌을 때 age
ER model의 mapping cardinality에는 어떤 것들이 있나?(Cardinality Constraints)
- one-to-one
- one-to-many
- many-to-one
- many-to-many
Relationship set의 key(super key)는 어떻게 구성되나?
- relationship set을 구성하는 entity의 primary key들의 combination으로 구성된다.
- EX) (student_id, instructor_id)는 advisor라는 relationship set의 super key이다.
E-R Diagram
Mapping cardinality의 표현법
- "one"을 나타낼 땐 directed line(->)으로, "many"를 나타낼 땐 undirected line(-)으로 나타냄
참여 제약조건(Participation Constraints)란?
- Total participation : entity set의 모든 entity들이 relationship에 참여하는 경우. 이중선(double line)으로 나타낸다.
- Partial participation : 일부 entity는 relationship에 참여하지 않기도 하는 경우
구조적 제약조건(Structural Constraints)
정리하자면, ER model에서 아래의 두 가지를 구조적 제약조건이라 한다.
- Cardinality Constraints
- Participation Constraints
Weak entity set
- weak entity set : primary key가 존재하지 않는 entity(아래 예시에서 section) -> 이러한 weak entity set의 entity들을 구분하기 위해선 identifying set(혹은 strong entity)가 필요함
- discriminator(혹은 partial key) : weak entity set의 entity들을 구분하는 attribute 집합
- identifying relationship은 double diamond로 나타냄
- weak entity set의 primary key는 strong entity set의 primary key와 weak entity set의 discriminator를 조합해 만듬
- 관계성 제약조건(Identifying relationship Constraints)
- weak entity set은 total participation
- one-to-many(from identifyinig entity set to weak entity set)
- 만약, 아래 예시에서 course_id를 section에 직접 포함시키면 section이 더 이상 weak entity가 아니게 되지만 course와 section을 연결하는 relationship에서 course_id에 의한 duplicate이 생긴다.(data redundancy)
ER diagram for a University
정리하자면 ER diagram을 그릴 때 각 케이스별 다음을 고려하면 된다.
- mapping cardinality(-, ->)
- total participation(=)
- identifying relationship, weak entity(double diamond, ---) : ---는 discriminator를 나타내는 dashed line
아래 예시를 보면 정리가 될 것이다.
Reduction to Relational Schemas(ER model -> Relational model)
- ER model을 Relational model로 바꾸기 위한 방법(추후 자세히 포스팅)