[Ch 1/3] 데이터베이스 입문
데이터베이스 개론
Database
전자적으로 저장되고 사용되는 관련있는 데이터들의 조직화된 집합
- 관련있는 데이터? -> 같은 서비스 안
- 조직화된 집합? -> 데이터를 빠르게 찾을 수 있게 함. 중복 관리 등
- 전자적으로 저장? -> 컴퓨터에 저장
DBMS(database management systems)
사용자에게 DB를 관리(정의, 만들기 등)하는 기능을 제공하는 소프트웨어 시스템
ex) PostgreSQL, MySQL, ORACLE, SQL Server
DB를 정의하다 보면 부가적인 데이터 발생(data를 설명하기 위해 필요한 data)
- metadata: DB를 정의하거나 기술하는 데이터, catalog라고도 부름
Database System
database + DBMS + 연관된 applications
줄여서 database라고도 부름
Database System 작동
- 사용자가 어플리케이션을 만짐
- 프로그램에서 쿼리를 보냄
- DBMS에서 쿼리를 받아서 무엇을 요청하는지 파악, 부가적인 정보 파악
- 실제 요청한 데이터를 찾아 어플리케이션이 돌려줌
Data Models
DB의 구조(structure)를 기술하는데 사용될 수 있는 개념들이 모인 집합
DB구조를 추상화해서 표현할 수 있는 수단 제공
datamodel의 종류
conceptual (high-level) |
logical (representational) |
physical (low-level) |
---|---|---|
일반 사용자들이 쉽게 이해할 수 있는 개념들로 이루어진 모델 추상화 수준이 가장 높음(이해하기 가장 쉬움) |
디테일하게 DB를 구조화 할 수 있는 개념들을 제공 데이터가 컴퓨터에 저장 될 때의 구조와 크게 다르지 않게 DB구조화 가능 |
컴퓨터에 데이터가 어떻게 파일 형태로 저장되는지를 기술할 수 있는 수단을 제공 데이터가 실제로 저장장치에 저장되는 형태에 가장 근접 하게 DB를 표현 |
entity-relationship model(ERD) | relational data model(테이블의 형태), object data model | data format, access path |
Database Schema
data model을 바탕으로 DB의 구조를 기술한 것
주로 DB를 정의할 때 정해지며, 자주 바뀌지 않음
Database State
특정 시점에 DB에 있는 데이터(snapshot라고도 함)
Three-schema architecture
- user application으로부터 물리적인 DB를 분리시키는 것이 목적
- 각 레벨의 독립 -> 어느 level에서의 변화가 상위 레벨에 영향을 주지 않기 위함
- 각 레벨 사이의 mapping만 변경
- 요즈음 주로 사용하지만 세가지의 level을 명확하게 나누지는 않음
Three Layers
- external schemas(user views) at external(/view) level: 사용자가 필요한 데이터만 표현
- conceptual schemas at conceptual level: 전체 DB에 대한 구조를 기술(데이터의 중복 등의 문제를 예방)
- internal schemas at internal level: 데이터가 존재하는 곳, 실체가 있는 내용 기술
Database Language
- Data Definition Language(DDL): conceptual schema를 정의하기 위해 사용되는 언어
- Storage Definition Language(SDL): internal schema를 정의하기 위해 사용되는 언어, 요즘은 SDL이 아닌 파라미터 등의 설정으로 대체
- View Definition Language(VDL): external schema를 정의하기 위해 사용되는 언어, 요즘 DBMS는 DDL이 VDL의 역할까지 수행
- Data Manipulation Language(DML): DB의 data를 활용하기 위한 언어(data 추가, 삭제, 수정, 검색 등의 기능 제공)
오늘날의 DBMS는 DML, VDL, DDL이 통합된 언어로 존재 ex) SQL
관계형 데이터베이스
Relational Data Model
수학에서의 Relation이란?
- Cartesian product의 부분집합
- 튜플(tuple)의 집합(set)
Cartesian product
여러(n개의) set에서 나올 수 있는 모든 경우의 수(집합) -> n-ray relation ex) n개의 set에 대한 Cartesian product의 부분집합이다.
Relational Data Model에서의 Relation
Relational Data Model에서의 하나의 set -> Domain
Domain은 하나 또는 여러 개의 (원자적)element/value로 이루어져 있음
Table로 표현하기 가장 적합
- 행: tuple -> 각 attribute의 값으로 이루어진 리스트(일부 값은 null 가능)
- 열(의 상단): attribute -> domain이 relaion에서 맡은 역할 이름
- table 자체: relation -> set of tuples
(예)학생 정보 관리
domain: students_ids, human_names, …, phone_numbers(본인), phone_numbers(비상연락망)
-> domain 명칭의 중복!
attribute로 해결 => phone_num, emer_phone_num
Relation Schema
relation의 구조를 나타냄
relation 이름과 attributes 리스트로 표기
ex) STUDENT(id, name, …, phone_num, emer_phone_num)
이때 degree of relation은 relation schema에서 attributes의 수가 됨
Relational Database
relational data model에 기반하여 구조화된 database
relational database는 여러 개의 relations로 구성
Relational Database Schema
relation schemas set + integrity constraints set
relation의 특징
- relation은 중복된 tuple은 불가
- relation의 tuple를 식별하기 위해 attribute의 부분 집합은 key로 설정
- tuple의 순서는 중요하지 않음(순서 order은 여러 방법 존재)
- 하나의 relation에서 attribute의 이름 중복 불가
- attribute의 순서 중요하지 않음
- attribute는 atomic해야 함(value가 하나여야 함)
Key
- superkey: relation에서 tuples를 unique하게 식별 할 수 있는 attributes set. ex) {id, name}, …
- candidate key: 어느 한 attribute라도 제거하면 unique하게 tuple를 식별 할 수 없는 superkey(key 또는 minimal superkey라고도 부름)
- primary key(PK): relation에서 tuples를 unique하게 식별하기 위해 선택된 candidate key. 주로 attribute의 숫자가 적은 candidate key를 선택. (보통 primary key로 선택이 되면 under bar로 표시) ex) {id}
- unique key: primary key가 아닌 candidate key(alternate key라고도 함)
- 다른 relation의 PK를 참조하는 attributes set
Constraints
Relational Database의 relations들이 언제나 항상 지켜줘야 하는 제약 사항
- implicit constraints: relational data model자체가 가지는 constraints
- relation은 중복되는 tuple를 가질 수 없음
- relation 내에서는 같은 이름의 attribute를 가질 수 없음
- schema-based constraints(exeplicit constraints): 주로 DDL을 통해 schema에 직접 명시할 수 있는 constraints
- domain constraints: attribute의 value는 해당 attribute의 domain에 속한 value이어야 함. ex) grade의 value로 100 <- (X)
- key constraints: PK의 값은 중복될 수 없음
- NULL value constraints: attribute가 NOT NULL로 명시되었다면 NULL값을 값으로 가질 수 없음
- entity integrity constraint: PK는 value값으로 NULL값을 가질 수 없음
- referential integrity constraint: FK, PK의 도메인이 같아야 하고 PK에 없는 values를 FK가 값으로 가질 수 없음
강의 영상
시니어 백엔드 개발자가 알려주는 데이터베이스 개론 & SQL
인프런: 데이터베이스 개론
인프런: 관계형 데이터베이스