09 정규화
01 정규화의 개념과 이상 현상
정규화의 개념
정규화(normalization)
이상(anomaly) 현상: 데이터베이스를 잘못 설계해서 불필요한 데이터 중복이 발생하여 릴레이션에 대한 데이터의 삽입 수정 삭제 연산을 수행할 때 발생하는 부작용
2. 이상 현상의 종류
삽입 이상(insertion anomaly)
- 새 데이터를 삽입하기 위해 불필요한 데이터도 함께 삽입해야 하는 문제
고객아이디 | 이벤트번호 | 당첨여부 | 고객이름 | 등급 |
---|---|---|---|---|
apple | E001 | Y | 정소화 | gold |
apple | E005 | N | 정소화 | gold |
apple | E010 | Y | 정소화 | gold |
banana | E002 | N | 김선우 | vip |
banana | E005 | Y | 김선우 | vip |
carrot | E003 | Y | 고명석 | gold |
carrot | E007 | Y | 고명석 | gold |
oragne | E004 | N | 김용욱 | silver |
ex. 아이디: melon, 이름: 성원용, 등급: gold → 신규 가입
참여한 이벤트가 없다면 삽입할 수 없음(기본키가 이벤트번호)
→임시 이벤트 번호를 삽입해야함 → 삽입이상
갱신 이상(update anomaly)
- 중복 투플 중 일부만 변경하여 데이터가 불일치하게 되는 모순의 문제
아이디가 apple인 고객의 등급: gold → vip
이벤트 참여 릴레이션에서 apple 고객에 대한 투플 3개의 등급 값이 모두 수정되어야 한다
→ 일부만 수정되면 갱신 이상(같은 고객이 서로 다른 등급을 가지는 모순o)
삭제 이상(deletion anomaly)
- 투플을 삭제하면 꼭 필요한 데이터까지 함께 삭제되는 데이터 손실의 문제
orange 고객이 이벤트 참여를 취소 → 투플 삭제 → 해당 고객에 대한 고객아이디, 고객이름, 등급 데이터까지 손실됨
3. 정규화의 필요성
이상 현상 발생 이유: 관련이 없는 데이터(관련 없는 속성)를 하나의 릴레이션에 모아두고 있기 때문
정규화: 이상현상이 발생하지 않도록 릴레이션을 관련이 있는 속성들로만 구성하기 위해 릴레이션을 분해하는 과정
02 함수 종속
X → Y
릴레이션 내의 모든 투플에서 X 값에 대한 Y 값이 항상 하나일 때
“X가 Y를 함수적으로 결정한다.” , “Y가 X에함수적으로 종속되어 있다.”
고객아이디 | 고객이름 | 등급 |
---|---|---|
apple | 정소화 | gold |
banana | 김선우 | vip |
고객아이디가 apple인 고객 → 이름이 정소화, 등급이 gold인 한 사람만 있음
고객 릴레이션에서 고객이름과 등급 속성은 고객아이디 속성에 함수적으로 종속
고객아이디=결정자, 고객이름, 등급=종속자
**유의
- 현재 시점에 릴레이션에 포함된 속성 값만으로 판단하면 안 된다
- 속성 자체의 특성을 고려하여 판단
기본키, 후보키: 다른 모든 속성들을 함수적으로 결정한다
기본키/후보키가 아닌 속성도 결정자는 될 수 있다
고객아이디 | 이벤트번호 | 당첨여부 | 고객이름 |
---|---|---|---|
apple | E001 | Y | 정소화 |
apple | E005 | N | 정소화 |
apple | E010 | Y | 정소화 |
banana | E002 | N | 김선우 |
banana | E005 | Y | 김선우 |
carrot | E003 | Y | 고명석 |
carrot | E007 | Y | 고명석 |
oragne | E004 | N | 김용욱 |
- 함수 종속 관계
- 고객아이디 → 고객이름
- {고객아이디, 이벤트번호} → 당첨 여부
- {고객아이디, 이벤트번호} → 고객 이름
부분함수 종속
- 고객 이름은 고객아이디에도 종속, {고객아이디, 이벤트 번호}에도 종속
- 속성 집합 일부에도 종속
완전함수 종속
- 당첨여부 속성: 속성 집합 전체에 종속
03 기본 정규형과 정규화 과정
1. 정규화의 개념과 정규형의 종류
정규화: 이상현상이 발생하지 않는 올바른 릴레이션으로 만들어나가는 과정
2. 제1정규형 (1NF; First Normal Form)
릴레이션에 속한 모든 속성의 도메인이 원자 값으로만 구성되어야 한다
고객아이디 | 이벤트번호 | 당첨여부 | 고객이름 |
---|---|---|---|
apple | E001, E005, E010 | Y, N, Y | 정소화 |
다중 값을 가지는 속성을 포함하지 않을 것
3. 제2정규형 (2NF; Second Normal Form)
릴레이션이 제1정규형에 속하고 기본키가 아닌 모든 속성이 기본키에 완전 함수 종속되어야 한다
부분함수 종속을 제거하고 모든 속성이 기본키에 완전 함수 종속되도록 릴레이션을 분해
'데이터베이스의 릴레이션 R(테이블)에서 X와 Y를 R의 부분집합이라고 할 때, X의 값 각각에 대해 Y의 값이 오직 하나로 결정 될때 Y는 X에 종속 된다고 한다.'
완전함수 종속
종속자가 기본키에만 종속되며, 기본키가 여러 속성으로 구성되어 있을 경우 기본키를 구성하는 모든 속성이 포함된 기본키의 부분집합에 종속된 경우를 완전함수 종속이라고 한다.
→ 기본키를 알아야만 나머지 속성을 식별할 수 있는 경우
주의: 분해된 릴레이션들을 자연 조인하여 분해 전의 릴레이션으로 복원 가능해야 한다
의미상 동등한 릴레이션으로 분해, 정보 손실 발생x (무손실 분해)
발생할 수 있는 이상 현상
- 삽입 이상
- 새로운 등급과 할인율에 대한 정보 → 고객이 없으면 삽입 불가
- 갱신 이상
- 등급에 대한 할인율 변경 → 모순 발생 가능
- 삭제 이상
- 고객 탈퇴로 투플이 삭제되면 등급, 할인율에 대한 정보 삭제
4. 제3정규형 (3NF; Third Normal Form)
릴레이션이 제2정규형에 속하고, 기본키가 아닌 모든 속성이 기본키에 이행적 함수 종속이 되지 않아야 한다
이행적 함수 종속
A→B, B→C이면 A→C 성립: C가 A에 이행적으로 종속됨
고객아이디 → 등급, 등급 → 할인율
할인율: 고객아이디에 이행적으로 종속됨
할인율은 고객 아이디에 따라 달라지는 것이 아니라, 등급에 따라 달라지는 것 → 한 테이블에 있으면 고객아이디가 할인율을 결정하게 됨
5. 보이스/코드 정규형 (BCNF; Boyce/Codd Normal Form)
릴레이션의 함수 종속 관계에서 모든 결정자가 후보키인 상태
제3정규형을 만족하지만, 보이스/코드 정규형에 속하지 않는 예시
한 고객은 동일한 강좌를 한 번만 신청
강사 한명은 인터넷 강좌 하나만 담당, 하나의 인터넷 강좌는 여러 강사가 담당 가능
기본키: {고객아이디, 인터넷강좌}, 기본키가 아닌 후보키: {고객아이디, 담당강사번호}
담당강사번호 → 인터넷강좌 결정: 담당강사번호=결정자, 그러나 후보키x
이상 현상
- 삽입
고객아이디는 null이 될 수 없음,
강사가 새로운 인터넷 강좌를 담당 + 고객이 없다면 삽입 불가
- 갱신
P004가 담당하는 강좌가 변경되면 관련된 속성값이 모두 변경되어야 한다
- 삭제
고객이 인터넷 강좌 신청 취소 → P002가 담당하고 있는 강좌 정보도 함께 삭제됨
변경
- 고객담당강사 릴레이션
- 기본키: {고객아이디, 담당강사번호}
- 후보키가 아닌 결정자x(함수 종속 관계x)
- 강좌담당 릴레이션
- 기본키: 담당강사번호
- 담당강사번호=결정자, 유일한 후보키
6. 제4정규형과 제5정규형
제4정규형: 릴레이션이 보이스/코드 정규형을 만족하면서, 함수 종속이 아닌 다치 종속을 제거해야 만족
제5정규형: 릴레이션이 제4정규형을 만족하면서 후보키를 통하지 않는 조인 종속을 제거해야 만족
'CS > DataBase' 카테고리의 다른 글
[데이터베이스 개론] 11. 보안과 권한 관리 (0) | 2022.12.02 |
---|---|
[데이터베이스 개론] 10. 회복과 병행제어 (0) | 2022.12.02 |
[데이터베이스 개론] 6. 관계 데이터 연산 (0) | 2022.10.02 |
[데이터베이스 개론] 5. 관계 데이터 모델 (1) | 2022.09.29 |
[데이터베이스 개론] 4. 데이터 모델링 (0) | 2022.09.27 |