The Database Design
- data model을 이제 Database Design으로 바꿀 수 있음.
- database design은 이제 특정 DBMS 제품에 따라 달라지는 DB 명세서의 집합임.
- 그러니까, MySQL을 사용하냐, Microsoft SQL Server를 사용하냐 이런 거에 따라서, 같은 data model이여도 다른 database design이 나올 수 있음.
Logical Design + Some Physical Design
- 보통 아래 3개의 단계로 나누는데,
- Conceptual design
- Logical design
- Physical Design
- Database Design은 Logical design과 조금의 Physical design을 합친 꼴.
Data Model -> Database Design
- 각 entity를 table로 생성
- Surrogate Key를 고려하며, Primary Key(PK)를 만들고
- Alternate Key(대체키) 도 신경쓰고
- 각각의 column마다
- NULL / Not Null
- Data Type (Int, VarChar...etc)
- Default value
- Data constraint
- 정규화 확인
- FK를 고려하며 Relationship 설정
- Strong Entity 사이의 Relationship 설정
- Identifying Relationship 설정
- non-Identifying (weak) entity와 strong entity 사이의 Relationship 설정
- Supertype - Subtype Relationship 설정 (IS-A)
- Recursive Relationship 설정
- Minimum Cardinality를 고려
- Optional / Mandatory
1. Create table
Primary Key
- 이상적인 PK는 짧고, 숫자이고, 고정된 값.
- Surrogate Key는 PK의 이상적인 특징을 갖고 있으나, user에게 의미가 별로 없는 값.
Candidate(Alternate) Key
Alternate key (unique key) is designated by the symbol (AKn.m), where n is a number that is placed after the attributes that form the alternate key group, m identifies the column's order inside the alternate key group:
- Candidate key == Alternate key (동의어)
- notation으로 AKn.m을 사용. n은 대체키 그룹의 식별번호, m은 그 대체키 그룹 내에서의 column number.
Specify Column Properties
Default value
default value(기본값)은 말 그대로 새로운 row가 table에 생성될 때 자동으로 설정되는 값.
Data Constraint
Data constraint는 value에 제한을 거는 것. 아래와 같은 종류가 있음.
- Domain Constraint : value가 주어진 set 안에 있는 값이어야 함
- Range Constraint : value가 정해진 구간(범위) 안의 값이어야 함
- Intrarelation Constraint : 같은 테이블 내의 다른 column의 값과 비교하여 value를 제한.
- Interrelation Constraint : 다른 테이블의 column의 값과 비교하여 value를 제한.
Verify Nomarlization
- 테이블은 모두 data model에 기반하여 정규화되어 있어야 함.
- 모든 테이블이 BCNF 또는 4NF인지 체크.
2. Create Relationship
1:1 Strong Entity Relationship
- 한 entity에서 PK는 다른 entity에서 FK로 설정.
- parent, child 상관 없음. (1:1 strong entity들의 관계니까!)
- Minimum Cardinality를 고려하는 것이 중요함. (Mandatory - Optional과 Optional-Mandatory가 서로 다름. 순서 중요!)
1:N Strong Entity Relationship
- 1 쪽 Entity의 PK를 N 쪽 Entity의 FK로 설정.
- 1 쪽이 parent, N 쪽이 child가 되는 것.
N:M Strong Entity Relationship
- N:M Strong Entity Relationship에는 FK를 놓을 자리가 서로 없음. 그래서 이 관계를 해소해줄 필요가 있음.
- Intersection table을 중간에 만들어서 이를 해소함.
- 각 Strong Entity의 PK가 Intersection table의 composite PK(합성 PK)가 됨.
ID-Dependent Relationship
4가지 종류가 있음.
- N:M relationship 표현
- association relationship 표현
- Storing multivalued attributes
- archetype/instance relationship 표현
ID-Dependent: Association Relationship
- Association Table -> Intersection Table과 비슷함.
- Intersection table처럼, 두 entity의 PK를 모두 가져와 composite PK로 사용.
- 거기에, 추가로 다른 attribute를 포함!