진박사의 일상

[데베시] 6강 본문

프로그래밍/공부

[데베시] 6강

진박사. 2021. 10. 18. 15:27

Relational Database Design(Logical)
- ER model to relational model mapping

ER의 Entity, Attribute, Relationship Type을 Relational에 매핑

ER-to-Relational Mapping
conceptual design(ER 모델)을 이용해서 relational DB schema를 디자인

Regular Entity Types Mapping
Entity -> Relation : Entity relations
Attributes : simple attribute는 그대로 relations의 simple attribute로 변환
Primary key : Entity type의 key attributes (2 이상 존재 가능)중 하나를 고름

Weak Entity Types Mapping
weak entity -> Relation
Attribute : simple이면 그대로 ~~
Primary key는 소유자의 foreign key를 가져와서 설정
Primary key : weak entity의 Partial key + 소유자에게 가져온 foreign key를 합쳐서 하나의 Primary key로 설정.

Multivalued Attributes Mapping
- Relational model에서는 하나의 Attribute가 둘 이상의 값을 가지지 못함 -> 별도의 새로운 Relation으로 만들어줌
Attribute : multivalued attribute를 속성으로, 원래 Multivalued attribute가 속하던 Entity의 key를 가져와서 Foreign key로 설정
Primary key : multivalued attribute와 Foreign key를 결합하여 생성

Composite Attributes Mapping(복합속성)

- 각각의 복합 속성을 별개의 relation으로 만들어줌

- Attribute : component attributes에서 simple attribue로, 원래 속했던 Entity type relation은 FK로

- Primary key : FK

- 다른 방식 : 그냥 복합 속성의 각각 개별 속성을 원래 Entiry type의 속성으로 변환

 

Binary 1:1 Relationship Type Mapping

- 별도의 relationship type을 만들지 x

- 관여된 2개의 Entity type을 찾는다 -> 그 2개의 사이의 하나의 relation을 골라서 하나의 PK를 다른 하나의 FK로 설정(이 때 total participation인 쪽에 FK를 넣어주는 것이 좋다. -> 그래야 가져온 FK에 NULL이 없음.)

- ex) Department와 Employee의 Manage 관계에서 Department에 Employee의 PK인 ssn를 FK로 설정해주면 모든 Department의 ssn는 NULL이 아니지만 반대로 Employee에 Department의 d_id를 넣어주면 매니저가 아닌 대다수의 사원에서는 d_id가 NULL이 된다.

 

Binary 1:N Relationship Type Mapping

- 두 개의 Entity type이 1:N 관계일 때 N 쪽에 해당하는 Entity type를 식별 (Employee - Department 일 때 Employee)

- 해당 Entity에 다른 Entity tpye의 PK를 FK로 가져옴. (Department의 PK를 Employee의 FK로 가져옴)

 

Binary M:N Relationship Type Mapping

- M:N Relationship에 해당하는 Independent relation을 새롭게 생성

- Attributes: Relationship에 참가하는 양쪽의 Entity의 PK를 FK로 가져옴. 기존 Relationship이 가지고 있던 Attribute가 있다면 그것도 포함.

- Primary Key : 두개의 FK을 Combination한 것

 

N-ary Relationship Type

- M:N Relationship Type Mapping과 같이 처리함.(Attribute는 참가하는 Entity들의 PK를 FK로, 기존 Attribute, PK는 FK의 조합)

 

COMPANY DB Example

COMPANY DB Relation Diagram

순서

1. Regular Entity Type Mapping

- EMPLOYEE : 새 relation 만들기, Ssn, Bdate, Address, Sex, Salay Attribute 설정, Ssn을 PK로

- DEPARTMENT : 새 relation 만들기, Name->Dname, Number->Dnumber Attribute 설정, 두개의 후보키(Dname, Dnumber) 중에서 Dnumber를 PK로

   (Attribute이름을 바꾸는 이유 : Attribute name을 Distinct하게 하기 위해)

   (후보키 선정 방법 : string보다 int를 PK로 삼는게 좋음)

- PROJECT : 새 relation 만들기, Name->Pname, Number -> Pnumber, Location->Plocation Attribute 설정, 두개의 후보키(Pname,Pnumber) 중에서Pnumber를 PK로

 

2. Weak Entity Type Mapping

- DEPENDENT : 새 relation 만들기, Name -> Dependent_name, Sex, Birth_date -> Bdate, Relationship으로 Attribute 설정, owner의 Entity type인 EMPLOYEE의 PK인 Ssn -> Essn으로 설정, Essn + Department_name 을 PK로 설정

 

3. Multivalued Attribute Mapping

- Location in DEPARTMENT : 새 relation DEPT_LOCATIONS 만들기, Locations를 Dlocation Attribute로, DEPARTMENT의 PK인 Dnumber를 FK로 가져옴, Dnumber + Dlocation을 합쳐서 PK로

 

4. Composie Attributes Mapping

- Name in EMPLOYEE : Name을 그냥 sub attribute인 Fname, Minit, Lname으로 나눠서 EMPLOYEE의 Attribute로 설정

 

5. Binary 1:1 Relationship Type Mapping

- MANAGES (DEPARTMENT-EMPLOYEE) : DEPARTMENT = total participation -> EMPLOYEE의 PK -> DEPARTMENT의 FK (Ssn -> Mgr_ssn), MANAGES의 Simple Attribute를 Start_date -> Mgr_start_date로 DEPARTMENT의 Attribute 처리.

 

6. Binary 1:N Relationship Type Mapping

- WORKS_FOR (DEPARTMENT-EMPLOYEE) : EMPLOYEE가 N에 해당하는 Entity type임 -> DEPARTMENT의 PK -> EMPLOYEE의 FK (Dnumber -> Dno)

- CONTROLS (DEPARTMENT-PROJECT) : PROJECT가 N에 해당하는 Entity tpye임 -> DEPARTMENT의 PK -> PROJECT의 FK (Dnumber -> Dnum)

- SUPERVISON (EMPLOYEE-EMPLOYEE) : recursive relation임. -> EMPLOYEE의 PK를 EMPLOYEE의 FK로 (Ssn -> Super_ssn)

 

7. Binary M:N Relationship Type Mapping

- WORKS_ON : 새 relation WORKS_ON 만들기, WORKS_ON의 simple attribute Hours 설정, EMPLOYEE와 PROJECT의 PK를 FK로 가져오기(Ssn -> Essn, Pnumber -> Pno), Essn + Pno를 PK로 설정

 

-> Mapping 결과

'프로그래밍 > 공부' 카테고리의 다른 글

[컴보] 7장 DoS  (0) 2021.11.01
[데베시] 7강 - Relational Algebra  (0) 2021.10.25
[컴보] 6강 악성 소프트웨어  (0) 2021.10.17
[데베시] 5강 - Relational Data Model  (0) 2021.10.14
[컴보] 5강 DB Security  (0) 2021.10.05