Relational Database
[Database] Relational database
Relation
์ํ์์ ๋์จ ๊ฐ๋ ์ผ๋ก, ์ด ๋จ์ด๋ฅผ ์ดํดํ๊ธฐ ์ํด์ ๋ช๊ฐ์ง ๋ฐฐ๊ฒฝ์ง์์ด ํ์ํจ
set
์๋ก ๋ค๋ฅธ elements๋ฅผ ๊ฐ์ง๋ collection
ํ๋์ set์์ elements์ ์์๋ ์ค์ํ์ง ์์
์) {1, 3, 11, 4, 7}
์ํ์์์ relation
์ฌ๋ฌ๊ฐ์ set์์ ์์๋ฅผ ๊ณจ๋ผ์ ๋ง๋ pair(list)๋ค์ ์งํฉ
์นด๋ฅดํ ์ง์ ํ๋ก๋ํธ์(Cartesian product) ๋ถ๋ถ ์งํฉ
Cartesian product : ๊ฐ ์งํฉ์์ ์์ ํ๋์ฉ ๋ฝ์์ ์์ ๋ง๋๋๋ฐ, ๋ชจ๋ ๊ฒฝ์ฐ์ ์ ์์ ๋ชจ์๋์ ์งํฉ์ ์๋ฏธํจ

๊ฐ๊ฐ์ pair(list)๋ฅผ ํํ์ด๋ผ๊ณ ํจ -> ํํ๋ค์ ์งํฉ(set of tuple)์ relation์ด๋ผ๊ณ ํ ์ ์์
relational data model์์์ relation
set : ๋๋ฉ์ธ์ ์๋ฏธํจ. element์ ์งํฉ. ๋๋ฉ์ธ๋ง๋ค ์ด๋ฆ์ ์ ์์
element (or value) : set ์์ ์๋ ํ๊ฐ์ ๊ฐ
set๋ค์ ์งํฉ์ relation์ด๋ผ๊ณ ํจ
relation ๋ง๋๋ ๊ณผ์
๋๋ฉ์ธ ์ ์
๊ฐ ์ด์ ์ด๋ค ๊ฐ(element, value)์ด ๋ค์ด์ฌ ๊ฒ์ธ์ง์ ๋ํ ์ ์
Attribute ์ ์
๊ฐ๊ฐ์ ๋๋ฉ์ธ์ด ์ด๋ค ์ญํ ์ ์ํํ๋์ง, ๊ทธ ์ญํ ์ ์ด๋ฆ์ ์ ์ํ๋ ๊ฒ
ํ ๋๋ฉ์ธ์ด ์ฌ๋ฌ๊ฐ์ง ๋ชฉ์ ์ ์ํด ์ฌ์ฉ๋ ์ ์์
๊ฐ๊ฐ์ ๋๋ฉ์ธ์ ๊ฐ์ด์์ ๊ฒ์ด๊ณ , ๊ทธ ๊ฐ๋ค์ ์์ผ๋ก ๋ง๋ค์ด์ง ํํ์ด ์์
์ด๋ฅผ ๊ฐ์ฅ ์ ํํํ ์ ์๋ ๋ฐฉ๋ฒ์ด table. ๊ทธ๋์ relation์ด table๋ก ๋ง์ด ํํ๋จ
์ํ์ ๊ทธ๋ฆผ์ผ๋ก ํํ

table๋ก ํํ

์ฉ์ด ์ ๋ฆฌ
domain
set of atomic values
domain name
domain ์ด๋ฆ
attribute
domain์ด relation์์ ๋งก์ ์ญํ ์ด๋ฆ
tuple
๊ฐ attribute์ ๊ฐ์ผ๋ก ์ด๋ฃจ์ด์ง ๋ฆฌ์คํธ. ์ผ๋ถ ๊ฐ์ NULL์ผ ์ ์์
relation
set of tuples (= table)
relation name
relation ์ด๋ฆ
relation schema
relation์ ๊ตฌ์กฐ๋ฅผ ๋ํ๋
relation ์ด๋ฆ๊ณผ attributes๋ฆฌ์คํธ๋ก ํ๊ธฐ๋จ
์) STUDENT(id, name, grade, major, phone_num, emer_phone_num)
attributes์ ๊ด๋ จ๋ constraints๋ ํฌํจํจ (์์ ์ ์ ์์์ ์๊ฐ์ ์ผ๋ก ํํ๋์ง ์์ง๋ง, schema์ ํฌํจ๋๋ ๋ด์ฉ์)
degree of a relation
relation schema์์ atrributes์ ๊ฐ์
์) STUDENT(id, name, grade, major, phone_num, emer_phone_num) -> degree 6
relation์ ๋ค๋ฅธ ์๋ฏธ : relation state
table ์ ์ฒด๋ฅผ relation์ด๋ผ๊ณ ๋ ํ๋๋ฐ,
ํน์ ์์ ์์ tuples์ ์งํฉ์ relation ์ด๋ผ๊ณ ํ๊ธฐ๋ ํจ(๋ฐ์ดํฐ์ ํ์ ํด์ ์๋ฏธํจ)
relational database
relational data mdoe์ ๊ธฐ๋ฐํ๊ณ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐํํ database
relational database๋ ์ฌ๋ฌ ๊ฐ์ relation์ผ๋ก ๊ตฌ์ฑ๋จ
relational database schema : relation schemas set + integrity constraints set
relaion์ ํน์ง
์ค๋ณต๋ tuple์ ๊ฐ์ง ์ ์์ (relation์ set of tuple์ด๊ธฐ ๋๋ฌธ์). tuple์ด ๊ฐ๊ณ ์๋ ๋ชจ๋ ๊ฐ์ด ๋ค ๋์ผํ ๋๋ค๋ฅธ tuple์ ์์ ์ ์์
relation์ tuple์ ์๋ณํ๊ธฐ ์ํด attribute์ ๋ถ๋ถ ์งํฉ์ key๋ก ์ค์ ํจ (id๋ผ๋ attribute๋ฅผ ๋ฌ์ ๊ฐ ํํ์ ์ ๋ํฌํ๊ฒ ๊ตฌ๋ถํจ)
tuple๊ฐ์ ์์๋ ์ค์ํ์ง ์์. ์์๊ฐ ๋ฐ๋์ด๋ relation์ ์๋ฏธ๊ฐ ๋ฌ๋ผ์ง๊ฑฐ๋ ์ํฅ์ ์ฃผ์ง ์์
ํ๋์ relation์์ attribute์ ์ด๋ฆ์ ์ค๋ณต๋์ง ์๊ณ , attribute์ ์์๋ ์ค์ํ์ง ์์
attribute๋ atomic(์์์ , ๋์ด์ ๋๋ ์ง ์ ์๋)ํด์ผ ํจ (composite or multivalued attribute๋ ํ์ฉ ์๋จ)
NULL์ ์๋ฏธ
๊ฐ์ด ์กด์ฌํ์ง ์์
๊ฐ์ด ์กด์ฌํ๋ ์์ง ๊ทธ ๊ฐ์ด ๋ฌด์์ธ์ง ์์ง ๋ชจ๋ฆ
ํด๋น ์ฌํญ๊ณผ ๊ด๋ จ์ด ์์
Key
superkey
relation์์ tuples๋ฅผ uniqueํ๊ฒ ์๋ณํ ์ ์๋ attributes set
์ ๋ํฌํ๊ฒ ์๋ณํ ์ ์๋ attributs set์ด๋ฉด ์ด๋ค ๋ฌถ์์ด๋ ๊ฐ๋ฅํจ. id ํ๋๋ง ํด๋ ๋๊ณ , ๋ชจ๋ attribute ์ ์ฒด๋ฅผ superkey๋ก ํด๋ ๋จ
candidate key
superkey ์ค์ ์ด๋ ํ attribute๋ผ๋ ์ ๊ฑฐํ๋ฉด uniqueํ๊ฒ tuple์ ์๋ณํ ์ ์๋ ๊ฒ
key, minimal superkey ๋ผ๊ณ ๋ ํจ
primary key๊ฐ ๋ ์ ์๋ ํ๋ณด๋ค
primary key
relation์์ tuples๋ฅผ uniqueํ๊ฒ ์๋ณํ๊ธฐ ์ํด ์ ํ๋ candidate key
๋ณดํต์ attribute ์ซ์๊ฐ ๋ ์ ์ key๋ฅผ ์ ํํจ
unique key
primary key๋ก ์ ํ๋์ง ๋ชปํ candidate keys
alternate key
foreign key
๋ค๋ฅธ relation์ PK(primary key)๋ฅผ ์ฐธ์กฐํ๋ attributes set
Constraints
์๋ฏธ : relational database์ relation๋ค์ด ์ธ์ ๋ ํญ์ ์ง์ผ์ค์ผ ํ๋ ์ ์ฝ ์ฌํญ
๋ถ๋ฅ
implicit constraints
relational data model ์์ฒด๊ฐ ๊ฐ์ง๋ constraints
relation์ ์ค๋ณต๋๋ tuple์ ๊ฐ์ง ์ ์์
relation ๋ด์์๋ ๊ฐ์ ์ด๋ฆ์ attribute๋ฅผ ๊ฐ์ง ์ ์์
schema-based constraints
์ฃผ๋ก DDL(sql ๋ฑ)์ ํตํด schema์ ์ง์ ๋ช ์ํ ์ ์๋ constraints
explicit constraints
schema-based constraints ์ข
๋ฅ
domain constraints
attribute์ value๋ ํญ์ attribute์ domain์ ์ํ value์ฌ์ผ ํจ
domain์ ์ค์ ํ๊ฒ 1~4 ์ฌ์ด์ int๋ผ๋ฉด, ์ํ๋ฒณ์ด๋ 4์ด๊ณผ ์ซ์๋ฅผ value๋ก ๋ฃ์ ์ ์์
key constraints
์๋ก ๋ค๋ฅธ tuple์ ๊ฐ์ value์ key๋ฅผ ๊ฐ์ง ์ ์์
key๋ uniqueํด์ผ ํจ
NULL value constraints
attribute๊ฐ NOT NULL๋ก ๋ช ์๋๋ค๋ฉด NULL์ ๊ฐ์ผ๋ก ๊ฐ์ง ์ ์์
NOT NULL๋ก ๋ช ์๋๋ค๋ฉด ๊ทธ attribute์๋ ๊ฐ์ ๊ผญ ๋ถ์ฌํด์ค์ผํจ
entity integrity constraint
primary key๋ value์ NULL์ ๊ฐ์ง ์ ์์
primary key๋ uniqueํ ํํ์ ์๋ณํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ํค์ธ๋ฐ, NULL์ด๋ฉด ์๋๊ฒ ์ง!
referential integrity constraint
FK(foreign key)์ PK(primary key)๋ ๋๋ฉ์ธ์ด ๊ฐ์์ผํ๊ณ
PK์ ์๋ value๋ฅผ FK๊ฐ ๊ฐ์ง ์ ์์
์๋ฐํ๋ฉด?
database system์์ ์๋ฌ(exception)๋ฅผ ๋ฐํํจ. ๋ฐฑ์๋ ์ดํ๋ฆฌ์ผ์ด์ ์ ์๋ฌ ์ํฉ ๋ฐ๋ผ ํธ๋ค๋งํด์ฃผ๋ฉด ๋จ
์ฐธ๊ณ
Last updated