set
- 서로 다른 elements를 가지는 collection
- 하나의 set에서 elements의 순서는 중요하지 않다. ( {1, 3, 11, 4, 7} )
relation in mathematics
- subset of Cartesian product
- set of tuples
set A, set B 가 있을 때, A와 B에서 하나의 element씩 골라서 만들 수 있는 가능한 모든 pair의 조합(A x B)을
Cartesian product 라고 함.
set이 두개일 때의 relation은 binary relation이라 하고, 이는 A와 B의 Cartesian product의 부분집합을 의미함.
만약 set이 n개가 있다면 relation은 n-ary relation이라 하고, 이는 n개의 집합에 대한 Cartesian product의 부분집합임.
이러한 각각의 리스트들을 tuple이라고 부름. 이 경우엔 n-tuple이라 함.
relational data model
student relation을 예를 들어 ralational data model 이해하기
domain 정의하기
- students_dis : 학번 집합, 7자리 integer 정수
- human_names : 사람 이름 집합, 문자열
- university_grades : 대학교 학년 집합, {1, 2, 3, 4}
- major_names : 대학교에서 배우는 전공 이름 집합
- phone_numbers : 핸드폰 번호 집합
relational data model에서는 추가적으로 attribute라는 개념이 있음. 각각의 도메인들이 이 relation 안에서 어떤 역할을 수행하는지 그 역할의 이름을 붙여주는 것.
phone_numbers 도메인이 두번 나타나는데, 같은 도메인 안에서 다른 목적으로 사용하기 위해 attribute를 부여함.
하나는 학생의 연락처, 또다른 하나는 그 학생의 비상연락망임.
relational data model을 사용할 때 tuple을 table로 주로 표현함.
ralational data model 주요 개념
- domain : set of atomic values
- domain name : domain 이름
- attribute : domain이 relation에서 맡은 역할 이름
- tuple : 각 attribute의 값으로 이루어진 리스트. 일부 값은 NULL일 수 있음
- relation : set of tuples
- relation name : relation의 이름
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
relational databse
- relational data model에 기반하여 구조화된 database
- relational database는 여러 개의 relations로 구성됨
relational database schema
relation schemas set + integrity constraints set
relation의 특징
- relation은 중복된 tuble을 가질 수 없음 (relation is set of tuples)
- relation의 tuple을 식별하기 위해 attribute의 부분 집합을 key로 설정함
- relation에서 tuple의 순서는 중요하지 않음
- 하나의 relation에서 attribute의 이름은 중복되면 안됨
- 하나의 tuple에서 attribute의 순서는 중요하지 않음
- attribute는 atomic 해야함 (composite or multivalued attribute 허용 안됨)
e.g. 주소) 서울특별시 강남구 청담동 (composite attribute), 전공) 컴공, 디자인 (multivalued attribute)
NULL의 의미
- 값이 존재하지 않음
- 값이 존재하나 아직 그 값이 무엇인지 알지 못함
- 해당 사항과 관련이 없음
중의적으로 사용되는 경우가 많기 때문에 가급적 사용하지 않는 것이 좋음
keys
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를 식별할 수 없는 superkey
- 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}
보통 primary key를 고를 때에는 attribute 수가 적은 것을 고름. 그게 더 편해서
unique key
- primary key가 아닌 candidate keys
- alternate key 라고도 부름
- e.g. PLAYER(id, name, team_id, back_number, birth_date)의 unique key는 {team_id, back_number}
보통 primary key로 선택이 되면 그 attribute는 밑줄로 표시함
foreign key
- 다른 relation의 primary key를 참조하는 attributes set
- e.g. PLAYER(id, name, team_id, back_number, birth_date)와 TEAM(id, name, manager)가 있을 때 foreign key는 PLAYER의 {team_id}
(PLAYER relation의 team_id가 TEAM relation의 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여야 함
- e.g. university_grades 도메인에서 grade라는 attribute의 value는 {1, 2, 3, 4} 값 중 하나여야함. 100을 value로 정할 수
없음. (100학년 못하죠?)
key constraints
- 서로 다른 tuples는 같은 value의 key를 가질 수 없음
NULL value constraint
- attribute가 NOT NULL로 명시됐다면 NULL을 값으로 가질 수 없음
- e.g. name이라는 attribute를 NOT NULL로 명시했으면, value에 NULL이 들어갈 수 없음.
entity integrity constraint
- primary key는 value에 NULL을 가질 수 없음
(tuple을 unique하게 식별하기 위해서 사용되는 key인데 NULL이 들어가면 식별할 수가 없음)
referential integrity constraint
- foreign key와 primary key가 도메인이 같아야 하고 primary key에 없는 value를 foreign key가 값으로 가질 수 없음
- e.g. TEAM relation의 id(primary key) 값 중에 team_023이란 값이 없는데, PLAYER relation의 team_id(foreign key)가 team_023이란 값을 가질 수 없음
'DB' 카테고리의 다른 글
6. MySQL 데이터 조회 - 2 (subquery) (0) | 2023.04.29 |
---|---|
5. MySQL 데이터 조회 - 1 (SELECT) (0) | 2023.04.27 |
4. SQL 데이터 추가, 수정, 삭제 (0) | 2023.04.27 |
3. SQL 기본개념과 기본문법 (0) | 2023.04.27 |
1. 데이터베이스 기본개념 (0) | 2023.04.26 |
댓글