MySQL
관계형 데이터베이스
김꼬알
2023. 3. 7. 17:32
relational data model (관계형 데이터 모델)이란?
- relational 의 개념 알기
- set
- 서로 다른 elements를 가지는 collection(중복 X)
- 하나의 set에서 elements의 순서는 중요하지 않다
- e.g.) {1, 3, 11, 4, 7}
- relation in mathematics
- Cartesian product A X B = {(a, b) | a∈A and b∈B}
- set A와 B로 만들 수 있는 모든 pair의 조합
- binary는 set이 두 개일 때, n-ary는 set이 세 개 이상일 때
- subset(부분 집합) of Cartesian pruduct
- set(집합) of tuples
- relational data model
- student relation을 예를 들어 relational data model을 이해해 보자
- domain 정의하기
- students_ids: 학번 집합, 7자리 integer 정수
- human_names: 사람 이름 집합, 문자열
- university_grades: 대학교 학년 집합, {1, 2, 3, 4}
- major_names: 대학교에서 배우는 전공 이름 집합
- phone_numbers: 핸드폰 번호 집합
- domain 정의하기
- student relation을 예를 들어 relational data model을 이해해 보자
- Cartesian product A X B = {(a, b) | a∈A and b∈B}
- set
domain, attribute, tuple 개념
- relational data model
- domain: set of stomic values(더이상 나눌 수 없는 값)
- domain name: domain 이름
- attribute: domain이 relation에서 맡은 역할 이름
- tuple: 각 attribute의 값으로 이루어진 리스트, 일부 값은 null일 수 있다
- relation: set of tuples(튜플들의 집합)
- relation name: relation의 이름
relation, relational database 개념
- relation schema
- relation의 구조를 나타낸다
- relation 이름과 attributes 리스트로 표기된다
- e.g.) STUDENT(id, name, grade, major, phone_num, emer_phone_num)
- attributes와 관련된 constraints도 포함된다
- degree(차수) of a relation
- relation schema에서 attributes의 수
- e.g.) STUDENT(id, name, grade, major, phone_num, emer_phone_num) → degree 6
- relation(or relation state)
- set of tuples로 표현할 때도 있음, 문서를 읽을 때 문맥을 파악해서 읽을 것
- relational database
- relational data model에 기반하여 구조화된 database
- relational database는 여러 개의 relations로 구성된다
- relational database schema
- relation schemas set + integrity constraints set
relation 특징 소개
- relation은 중복된 tuple을 가질 수 없다(relation is set of tuples)
- relation의 tuple을 식별하기 위해 attribute의 부분 집합을 key로 설정한다
- relation에서 tuple의 순서는 중요하지 않다
- 하나의 relation에서 attribute의 이름은 중복되면 안된다
- 하나의 tuple에서 attribute의 순서는 중요하지 않다
- attribute는 atomic 해야 한다(composite or multivalued attribute 허용 안됨)
null의 의미
- 값이 존재하지 않는다
- 값이 존재하나 아직 그 값이 무엇인지 알지 못한다
- 해당 사항과 관련이 없다
key 개념과 종류
- superkey
- relation에서 tuples를 unique하게 식별할 수 있는 attributes set
- e.g.) PLAYER(id, name, team_id, back_number, birth_date)의 superkey는 {id, name, team_id, back_number, birth_date}, {id, name}, {name, team_id, back_number}, … etc
- candidate key
- 어느 한 attribute라도 제거하면 unique하게 tuples를 식별할 수 없는 super key
- key or minimal superkey
- e.g.) PLAYER(id, name, team_id, back_number, birth_date)의 candidate key는 {id}, {team_id, back_number}
- primary key
- relation에서 tuples를 unique하게 식별하기 위해 선택된 candidate key
- e.g.) PLAYER(id, name, team_id, back_number, birth_date)의 primary key는 {id} or {team_id, back_number}
- unique key
- primary key가 아닌 candidate keys
- alternate key
- e.g.) PLAYER(id(primary key로 선택됨), name, team_id, back_number, birth_date)의 unique key는 {team_id, back_number}
- foreign key
- 다른 relation의 PK를 참조하는 attributes set
- e.g.) PLAYER(id, name, team_id, back_number, birth_date)와 TEAM(id, name, manager)가 있을 때 foreign key는 PLAYER의 {team_id}
constraints 개념과 종류
- 개념
- relational database의 relations들이 언제나 항상 지켜줘야 하는 제약 사항
- 종류
- implicit constraints
- relational data model 자체가 가지는 constraints
- relation은 중복되는 tuple을 가질 수 없다
- relation 내에서는 같은 이름의 attribute를 가질 수 없다
- schema-based constraints
- 주로 DDL을 통해 schema에 직접 명시할 수 있는 constraints
- explicit constraints
- domain constraints
- attribute의 value는 해당 attribute의 domain에 속한 value여야 한다
- key constraints
- 서로 다른 tuples는 같은 value의 key를 가질 수 없다
- NULL value constraints
- attribute가 NOT NULL로 명시됐다면 NULL을 값으로 가질 수 없다
- entity integrity constraint
- primary key는 value에 NULL을 가질 수 없다
- referential integrity constraint
- FK와 PK의 도메인이 같아야 하고 PK에 없는 values를 FK가 값으로 가질 수 없다
- domain constraints
- implicit constraints