728x90
목차
5. 관계 데이터 모델
01 관계 데이터 모델의 개념
1. 관계 데이터 모델의 기본 용어
열: 속성 | 고객아이디 CHAR(20) | 고객 이름 CHAR(20) | 나이 INT | 등급 CHAR(10) | 직업 CHAR(10) | 적립금 INT |
---|---|---|---|---|---|---|
attribute 행: tuple | apple | 김현준 | 20 | gold | 학생 | 1000 |
BANANA | 정소화 | 25 | vip | 간호사 | 2500 |
하나의 개체에 관한 데이터를 릴레이션(relation) 하나에 담아 데이터베이스에 저장
- 속성
- 릴레이션의 열
- 서로 다른 이름을 이용해 구별
- 릴레이션: 파일 관리 시스템의 파일, 속성: 파일의 필드에 대응
- 투플(tuple)
- 릴레이션의 행
- 개체의 인스턴스
- 도메인
- 속성 하나가 가질 수 있는 모든 값의 집합
- 관계 데이터 모델에서는 더 분해할 수 없는 원자 값만 속성 값으로 사용 가능
- 특정 속성이 가질 수 있는 모든 원자 값의 모임
- 장점: 사용자가 속성 값을 입력, 수정할 때 데이터베이스 시스템이 적합성을 판단 → 항상 올바른 값으로 유지
- 일반적으로 속성의 특성을 고려한 데이터 타입으로 정의(문자, 정수 .. )
- 널 값
- 릴레이션에 있는 특정 투플의 속성 값을 모르거나 적합한 값이 없는 경우
- 차수(degree)
- 하나의 릴레이션에서 속성의 전체 개수
- 최소 1 이상의 차수를 유지해야 한다
- 일반적으로 자주 변하지 않음
- 카디널리티(cardinality)
- 투플의 전체 개수
- 투플이 없는 릴레이션이 존재할 수도 있음
- 일반적으로 자주 변한다 - 동적
2. 릴레이션과 데이터베이스의 구성
릴레이션 스키마 | 고객아이디 CHAR(20) | 고객 이름 CHAR(20) | 나이 INT | 등급 CHAR (10) | 직업 CHAR(10) | 적립금 INT |
---|---|---|---|---|---|---|
릴레이션 인스턴스 | apple | 김현준 | 20 | gold | 학생 | 1000 |
banana | 정소화 | 25 | vip | 간호사 | 2500 |
- 릴레이션 스키마
- 일반적으로 다음과 같은 형태로 표현
릴레이션이름(속성이름1, 속성이름2, ... , 속성이름n)
- 위의 예시에서는
고객(고객아이디, 고객이름, 나이, 등급, 직업, 적립금)
- 스키마를 보면 전체 구조를 쉽게 파악 가능
- 릴레이션 내포(relation intension)라고도 부른다
- 일반적으로 다음과 같은 형태로 표현
- 릴레이션 인스턴스
- 어느 한 시점에 릴레이션에 존재하는 투플들의 집합
- 릴레이션 스키마에서 정의하는 각 속성에 대응하는 실제 값으로 구성
- 데이터베이스 스키마와 데이터베이스 인스턴스
- 데이터베이스는 릴레이션 여러개로 구성
- ex. 인터넷 쇼핑몰 - 고객, 상품, 주문
- 데이터베이스 스키마: 데이터베이스를 구성하는 릴레이션들의 스키마를 모아놓은 것
- 데이터베이스 인스턴스: 어느 한 시점에서 데이터베이스에 저장된 데이터 내용의 전체 집합
3. 릴레이션의 특성
- 투플의 유일성: 하나의 릴레이션에는 동일한 투플이 존재할 수 없다
키(key): 투플을 유일하게 구별하기 위해 선정하는 속성 - 투플의 무순서: 하나의 릴레이션에서 투플 사이의 순서는 무의미함
순서 상관없이 투플 내용이 같아야 같은 릴레이션 - 속성의 무순서: 하나의 릴레이션에서 속성 사이의 순서는 무의미함
- 속성의 원자성: 속성 값으로 원자 값만 사용할 수 있다
4. 키의 종류
- 슈퍼키
- 유일성의 특성을 만족하는 속성 또는 속성들의 집합
- 유일성-키로 지정된 속성 값은 투플마다 달라야 함
- 후보키
- 유일성과 최소성을 만족하는 속성 또는 속성들의 집합
- 최소성- 꼭 필요한 최소한의 속성들로만 키를 구성
- 슈퍼키 중에서 최소성을 만족 → 후보키
- 릴레이션 인스턴스만 보고 판단하면 안됨 → 속성의 본래 의미를 정확히 이해해야 함
- 기본키(primary key)
- 여러 후보키 중에서 기본적으로 사용할 키
- 고려 기준
- 널 값을 가질 수 있는 속성이 포함된 후보키는 부적합
- 값이 자주 변경될 수 있는 속성은 부적합
- 단순한 후보키를 선택
- 대체키(alternate key)
- 기본키로 선택되지 못한 후보키
- 외래키(foreign key)
- 어떤 릴레이션에 소속된 속성 또는 속성 집합이 다른 릴레이션의 기본키가 되는 키
- 릴레이션들 사이의 관계를 올바르게 표현하기 위해 필요
고객 릴레이션 - 고객(고객아이디, 고객이름, 나이, 등급, 직업, 적립금)
주문 릴레이션 - 주문(주문번호, 주문고객, 주문제품, 수량, 단가, 주문일자)
주문 고객 → 고객아이디 속성을 참조 : 주문 고객 속성=외래키 - 외래키가 되는 속성과 기본키가 되는 속성의 이름은 달라도 됨, 도메인은 같아야 함
- 기본키가 아닌 속성을 외래키가 참조한다면? 투플을 유일하게 구별하기 어려움 → 반드시 외래키는 기본키를 참조해야 한다
- 하나의 릴레이션에 외래키가 여러개 존재 가능
- 외래키=기본키 O, 외래키를 포함하여 기본키 구성 O
- 외래키 자신이 속한 릴레이션의 기본키를 참조 O
고객아이디, 고객이름, 나이, 등급, 직업, 적립금, 추천고객: 고객아이디를 참조하는 외래키 - 기본키가 아니기 때문에 널 값을 가질 수 있음
- 서로 다른 투플이 같은 값을 가질 수 있음
02 관계 데이터 모델의 제약
무결성 제약 조건 = 키와 관련
무결성: 권한이 있는 사용자의 잘못된 요구에 의해 데이터가 부정확해지지 않도록 보호
cf. 보안: 권한이 없는 사용자로부터 데이터를 보호
1. 개체 무결성 제약 조건
기본키를 구성하는 모든 속성은 널 값을 가질 수 없다
삽입/기존 투플의 기본키 변경 → 기본키에 널 값이 포함되면 연산 수행 거부 (보통 DBMS가 처리)
기본키에 대한 규칙 - 각 릴레이션마다 적용
2. 참조 무결성 제약조건
외래키에 대한 규칙 - 연관된 릴레이션들에 적용
외래키는 참조할 수 없는 값을 가질 수 없다
= 자신이 참조하는 릴레이션에 기본키 값으로 존재하는 값=참조 가능한 값만 가져야 한다
ex. 고객 아이디 인스턴스={apple, banana, carrot, orange} 외래키: cherry → 참조 불가
cf. 널 값을 가진다고 해서 제약조건 위반x
- 조건 준수
- 고객 릴레이션에 삽입: 고객 아이디 반드시 포함
- 주문 릴레이션에 삽입: 참조 무결성 제약조건 위반여부 확인
- 고객 릴레이션에 삭제: 참조 무결성 제약조건을 위반하지 않는 경우에만
ex. 고객 릴레이션에 고객아이디 apple 삭제 → 주문 릴레이션에 apple 존재시 데이터 무결성x - 주문 릴레이션에 삭제: 조건 위반x
- 고객 릴레이션에 변경: 기본키가 아닌 속성값→ 영향x, 기본키 속성값 → 제약조건 확인
- 주문 릴레이션에 변경: 외래키가 아닌 속성값→ 영향x, 외래키 속성값 → 제약조건 확인
728x90
'CS > DataBase' 카테고리의 다른 글
[데이터베이스 개론] 9. 정규화 (0) | 2022.12.01 |
---|---|
[데이터베이스 개론] 6. 관계 데이터 연산 (0) | 2022.10.02 |
[데이터베이스 개론] 4. 데이터 모델링 (0) | 2022.09.27 |
[데이터베이스 개론] 3. 데이터베이스 시스템 (0) | 2022.09.24 |
[데이터베이스 개론] 2. 데이터베이스 관리 시스템 (1) | 2022.09.20 |