The Data Model
- data model은 DB 디자인의 계획, 청사진임.
- DB design에 비해 좀 더 일반화하고 모호하게 만든 거.
- DB 디자인보다는 바꾸기도 쉬워서 개념적, 논리적인 문제 해결을 위해서 사용하면 좋음.
E-R Model
- Entity-Relationship (E-R) model 은 개념과 기호(concepts and graphical symbols)들로 구성한 모델.
- 개념적(conceptual) 모델 만드는 데 좋음.
Entity
- 그 객체지향에서 class랑 instance (클래스 - 객체) 처럼 Entity class, Entity instance가 있다나 봄.
Attribute
- 직역하면 속성인데.. entity를 구성하는 놈임 그냥
이거 만약에 DB에서 user table을 만든다 치면, user는 엔티티고 name, age 같은 애들이 entity 되는거.
Identifiers
- 직역하면 식별자. 여러 entity instance들을 구별하기 위한 값 정도라고 생각하면 됨.
아까랑 똑같이 DB에서 예를 들자면.. 얘가 PK같은 Key가 되는 거.
Relationships
- Entity들끼리의 관계..
- Entity class끼리의 relation은 Relationship class라고 함.
- 반대로 instance 는 Relationship instance겠죠 뭐
- 원래 ER모델에선 relation도 attribute 가질 수 있었는데, 지금은 아니래
- relationship class는 2개 이상의 entity class를 포함할 수 있다고 함.
- relationship으로 연결된 entity 개수를 relationship의 degree(차수)라고 함. 엔티티 2개면 binary relationship이고 이런 식.
Entity vs Table?
- 둘이 다른점은 entity는 relationship을 FK 없이 나타낼 수 있다는 거
- 그래서 entity나 relationship이 존재한다는 게 확실치 않을 때 쓰기 용이함.
Cardinality
- Cardinality == Count
- Maximum Cardinality = 최대 개수 (가능한 최대의 개수)
- Minimum Cardinality = 최소 개수 (최소 개수. 최소한 n개는 있어야 해!)
Maximum Cardinality
- relationship에서 최대 몇 개의 entity instance가 올 수 있는 지..
- 1:1 / 1:N / N:M의 3종류가 있음
Parent-Child Entity (1:N)
- 그중에 1:N은 one-to-many relationship이라고 하는데,
- 1쪽이 parent고 N쪽이 child임.
- 뭐 예를 들면 학교-학생의 관계?
- 이걸 Has-A 관계라고도 하는데.. 포함관계라고 생각하면 됨
Minimum Cardinality
- 이건 relationship에서 최소한 몇 개의 entity instance가 와야 하는 지
- 0 또는 1임.
- 0은 optional인데, 존재할 수도 있고 아닐 수도 있는 거.
- 1은 mandatory라고 하는데, 얘는 무조건 최소 1개는 있어야 한다는 뜻.
RelationShips
ID-Dependent Entities
- entity(child)가 다른 entity(parent)의 식별자(identifier)를 포함하는 식별자를 가질 때 ID-Dependent라 함.
- ID-Dependent 관계에서 parent의 mininum cardinality는 무조건 1.
- 그러니까, parent의 PK를 child의 PK로 그대로 가져와서 사용해야 식별이 가능하게 될 때 사용한다고 보면 됨.
Weak Entities
- 한 Entity의 존재가 다른 Entity에 종속될 때 Weak Entity라고 함.
- ID-Dependent는 weak의 일종임.
- 물론, ID-Dependent가 아닌 weak도 존재.
Identifying Relationships
- ID-dependent entity랑 parent entity랑 실선으로 연결한 게 Identifying임.
- 점선은 non-Identifying임 (strong entity거나, non-identifying weak entity와 parent일 수도 있겠죠?)
Subtype Entities
- subtype entity는 supertype entity의 special case.
- supertype은 일반적인.. 그니까 싹 다 포함하는 거고, subtype은 특정한 attribute만 갖고 있는 거.
- 예를 들면, School Entity가 있는데, 남학교, 여학교, 남녀공학마다 다른 attribute를 갖게 하고 싶음. 이럴 때 supertype은 School / subtype은 남학교, 여학교, 남녀공학 entity가 되는 거.
- discriminator attribute를 supertype이 가질 거임. 어떤 subtype을 가리킬 지를 나타내는 attribute.
Exclusive - 한 supertype이 한 subtype과만 relation을 가질 수 있음.
Inclusive - 여러 subtype과도 relation 쌉가능
Total - supertype이 subtype에 포함될 수 있다면, total subtype임.
Partial - supertype이 어떤 subtype에도 포함될 수 없다면, partial.
IS - A Relationship
- subtype - supertype의 관계를 IS-A relationship이라고 함. subtype IS A supertype (woman is a human 이런느낌)
- supertype과 subtype의 identifier는 모두 같아야 됨!
- subtype에서는 value-inappropriate null(값이 적절하지 않은 NULL)은 피하자.
Recursive Relationship
- Entity가 그 자기 자신과 relation을 가질 때.
- 예를 들면 여러 employee가 있고, 한 employee가 다른 employee를 manage할 때.. 등등
Weak / Strong / ID-dependent 그리고 Identifying 정리
Entity의 종류에는 크게 Weak Entity와 Strong Entity가 있다고 보면 됨. (Supertype-Subtype는 논외로 치고.)
Weak Entity는 혼자서 존재할 수 없음. 다른 Entity(Strong Entity나..)에 종속되어야 함. [종속, 의존]
Strong Entity는 혼자서 존재할 수 있음. 다른 Entity에 종속적, 의존적이지 않은 개체인 거.
그럼 ID-Dependent Entity는 뭐냐?
Weak Entity의 일종임. 당연히 다른 Entity에 종속적. 그런데 이제 parent entity(의존하는 entity)의 identifier(식별자)를 갖고 와서 자기 Identifier로 쓰면 그게 ID-dependent entity임.
예를 들어서, 아파트 한 채 - 집 한 채의 개념으로 생각해 보자.
아파트의 동수(101동, 102동...)을 PK(identifier)라고 해 보자.
그럼 아파트 한 채 안에 있는 집 한 채는 아파트가 존재하지 않으면 존재할 수 없겠지? 의존적이지? 그럼 일단 Weak Entity인거.
그런데 이제, 친구한테 나 701호 살아! 하면 뭐라고 하겠어 친구가
몇 동 701호인데? 라고 물어보겠지?
집 한 채에는 PK(Identifier)로 아파트 호수는 물론, 동수도 포함되어야 하는 거임.
그럼 아파트 entity의 PK(Identifier)를 가져와서 자기 PK로 사용하는 꼴이 되지?
이게 ID-Dependent Entity임.
그럼 Identifying Relationship은 뭐냐?
ID-dependent Entity와 다른 Entity의 관계면 Identifying임. 이걸 이제 실선으로 나타냄.
그리고 non-ID-dependent Entity와 parent의 관계, Strong Entity와 다른 Entity의 관계면 이제 non-Identifying이 되는 거.