[데이터베이스시스템] 6. Database Design Using the E-R Model

한빈수윤·2024년 4월 16일

ER model

  • entity sets
  • relationship sets
  • attributes
instructor=(ID, name, salary)

여기서 instructor이 entity, id,name,salary가 attribute이다.

Relationship Sets

relationship set

(여기서 id의 밑줄은 primary key임을 의미한다. )
advisor라는 관계 안에 binary relationship set의 예시이다. advisor에 date와 같은 relationship을 describe하는 속성을 추가할 수도 있다.

relation2

하나의 entity set에대한 relationship도 가능하고 이를 "recursive relationship set"이라고 한다.

relation3

이와같이 non-binary relationship sets 즉, 여러개의 entity로도 relationship을 나타낼 수 있다. 3개의 entity set 사이의 관계는 Ternary Relationship이라고 한다.

Attribute

- simple attribute: subpart로 나누어지지 않음
- composite attribute: subpart로 나누어짐

- single-valued attribute: entity가 하나의 값만을 갖는 속성(ex. id)
- multivalued attribute: entity가 여러개의 값을 갖는 속성(ex. phone number)

- derived attrbutes: 다른 attribute로부터 값이 결정된다. 
  • domain: attribute의 값, 타입, 제약사항 등에 대한 값의 범위(ex. semester은 봄,여름,가을,겨울 중 하나)

complex attribute

complex
complex attribute는 세분화가 가능하다. id, adress, street은 subparts가 있다. phone_number에 {}가 있는 이유는 여러개의 번호를 가질 수 있는 multivalued이기 때문이다. 또한 age()는 derived된다.

Mapping Cardinality

- one to one
- one to many
- many to one
- many-many

-: many
->: one

  • one to one
    1-1
    각 instructor와 student가 서로 최대 한개의 entity와 관련이 있다.

  • one to many
    1-many
    insturctor은 여러(0 포함) 학생과 관련이 있다.

  • many to one
    many-1

  • many to many
    m-m

Total and Partial Participation

  • Total Participation: entity set의 모든 entity가 적어도 하나의 relationship에 참여
    total
    total prticipation은 double line으로 표현된다. 모든 student는 적어도 한명의 instuctor과 relation을 가진다.

  • Partial Prticipaion: entity set에서 relationship에 참여하지 않는 entity가 존재
    double line이 아닌 경우라고 보면 되겠다.

Cardinality constraints on Ternary Relationship

Ternary Relationship(여러개의 entity set이 relationship을 이룰 때)에서는 오직 한 개의 화살표만 허용한다. 중복적인 의미를 담을 수 있기 때문에 이를 피하기 위함이다.
ex

Primary Key

entity와 relation 간의 구분 방법을 구체적으로 보여주는 방법

Primary key for Entity sets

  • 각 entity는 구별되어있는 distinct이다.
  • entity들의 차이는 attribute의 관점에서 표현되어야 한다.
  • entity set의 어떤 두 entity로 모든 attribute에 대해 완전 같을 수 없다.
  • entitity set 중에 entity들을 구별할 수 있는 attribute의 집합을 key라고 한다.

Primary key for Relationship sets

  • relationship set 사이에서 식별할 수 있도록 하는 것.
  • relation set을 구성하는 primary key의 합집합(속성이 필요에 따라 추가됨)이 relationship의 superkey이다.
  1. one-to-many인 경우의 관계
    many side의 primary key가 primary key

예제)
instructor와 student는 one-to-many관계. 이때 학생은 최대 한 명의 지도교수만을 가질 수 있다면 student의 primary key는 advisor에서도 primary key가 된다.

  1. one-to-one인 경우의 관계
    relation에 참여하는 어떤 하나의 entity set의 primary key중 하나

학생과 교수의 relation이 one-to-one이라면 student의 primary key나 instructor의 primary key 중 한 쪽의 주 키가 최소 super key로 형성되기 때문에 advisor 의 primary key 는 학생의 primary key나 교수의 primary key로 선택할 수 있다.

  1. many-many인 경우의 관계
    두 entity set의 primary key의 union이 곧 minimal superkey이자 primary key

Weak Entity sets

  • 다른 entity set(idntifying entity set)에 의존하는 entity set.(idntifying relationship)
    +idenfiying entity는 weak entity set의 owner라서 없으면 안됨

  • entity들을 식별하기 위해 identifying entity set으로부터 key attribute를 빌려옴(primary key가 없다 =자체적인 key가 없다)
    -weak entity set이 아니면 strong entity set.

  • weak

  • weak entity set은 double rectangle로 표현.

  • weak entity set의 discriminator(구별자)는 점선으로 표현.

  • weak entity set과 strong entity set을 연결짓는 relationship set은 double diamond로 표현.
    weak entity set은 identifying relation 이외에 다른 relationship에도 참여할 수 있다. 이때 section의 primary key={course_id,sec_id,semester,year}

Redundant Attribute

er-diagram에 relationship set에 속하는 entity에서 중복이 발생할 때 중복은 제거한다.

red

student_dept라는 relationship set을 관계에서 student, department의 entity가 모두 dept_name이라는 attribute 중복이 발생하므로 student에서 dept_name을 삭제해준다. student에서 삭제하는 이유는 dep_name이 department에서 primary key이기 때문.

E-R diagram example

ex2

자세한 설명 추가 예정

Reduction to Relationship Schemas

  • E-R diagram을 relation schema로 표현
  • entity set, relationship set 을 스키마로 표현

Composite attributes

속성의 하위 속성이 존재할 때(여러개의 단일 속성으로 표현)


전체 instructor schema는
instructor(ID, first_name, middle_initial, last_name, street_number, street_name, apt_number, city, state, zip_code, date_of_birth)
로 기록된다. (multivalued attributes는 새로운 스키마가 생성되었기 때문에 무시)

Multivalued attributes

  • 속성에 대한 값이 여러 개 있을 수 있을 때 (별도의 relation schema 생성->새로운 table 만들어야 함)
  • 생성된 새로운 schema는 기존의 entity의 primary key 역할의 attribute와 multivalued attribute로 구성된다.

ㅇㅇ

ㅇ
이런식으로 표현된다.

추가적으로 새롭게 만들어진 schema에서 entity set의 primary key를 가져온 attribute(ex ID)는 entity set으로부터 만들어진 relation을 참조해야 한다.

Representing Relationship Sets

- many-to-many
rrs

many-to-many인 경우 별도의 table을 생성해야 한다. 이때 두 entity set의 primary key를 속성으로 갖는다.


- many-to-one (advisor)

many-to-onedls ruddndpsms 별도의 table은 선택사항이다.
advisor=(s_id(pk,fk), i_id(fk))
별도의 테이블을 만든다면 many-side인 student의 primary key가 advisor의 primary key로 온다.

- many-to-one(inst_dept,stud_dept)-other case

이 때는 total이 있기 때문에 앞에 many-to-one과는 다르게 나온다. inst_dept relationship을 살펴보면 instructor은 total이므로 instructor의 모든 entity가 inst_dept에 참여하므로 모든 속성이 들어가게 되는 것이다. 하지만 many-side가 total이므로 별도의 스키마를 추가하지 않고 dept_name을 해당 instructor relation의 속성으로 추가하는 것으로 스키마 중복을 피할 수 있다.

- one-to-one(advisor)

두 entity set 중 하나를 many-side로 취급하고, 이전 내용의 many-to-one의 경우를 실행한다.
만약 many-side가 total이 아니라면 null 값이 존재할 수 있다.

  • weak entity set

weak entity set의 경우 불필요한 스키마 중복에 의해 스키마로 옮기는 것이 불가능하다.

Specialization

entity set 내에서 다른 entity들과 구별될 수 있는 subgroup으로 나눈다.

  • sub group은 high-level entity set에 적용되지 않는 속성을 가지거나 relation에 참여하고 있다.
  • lower-level entity set은 연결된 higher-level entity set의 모든 attribute와 relationship participation을 상속받는다.

person은 enployee, student로 subgrouping을 했고, employee는 instructor, secretary로 subgrouping하였다.
instructor은 higher-level entity에게 상속을 받아 id,name,street,city,salary,rank로 묘사된다.

  • Overlapping: person은 employee,student(양쪽에 다 속하는 것이 존재할 때)
  • Disjoing: employee는 instructor, secretary(둘중 하나=서로소)

specialization은 attribute 중복 단점이 존재한다.

Generalization

bottom-up,top-down을 사용하여 더 높은 entity set을 구상하는것

E-R diagram이 주어지면 스키마 diagram으로 바꾸는 것을 의미한다.

Completeness constraint

higher-level entity set의 entity가 generalization을 하는 lower-lever entity set중 하나에 속해 있는지 확인

  • total: 모든 higher-level entity 는 어떤 lower-level entity에 속한다.
  • partial: 어떤 higher-leverl entity 는 어느 lower-level entity에 속하지 않아도 된다.
    bottom-up에 경우 partial이 나올 수 없으므로 total만 가능하다.

Aggregation(집합)

3개 이상의 entity set이 참여하는 관계

eval_for은 총 4개의 entity set이 참여하는 관계다. 여기서 proj_guide는 세개의 entity만 참여하고 있는데 project의 평가여부에 따라 바뀌기 때문인데 중복되어 있는 관계로 prj_guide는 없앨 수 있다.

  • relationship과 relation사이의 relationship을 허용한다.

project, instructor, student, evaluation은 하나의 entity로 entity로 modeling되었다. 이때 이 entity의 primary key는 각 3개의 entity set이 같는 primary key를 모두 합친 것이다.

이를 테이블로 바꾸면?

  • proj_guide의 스키마는 별도로 만들지 않는다. (aggregation 에 대한 스키마는 만들 필요없다.)
profile
할 수 있다 방돌!!!

0개의 댓글