๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Computer Science/Database

3 Relational Database (feat. key, constraints)

by Dowon Kang 2023. 12. 23.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ” ๊ฐ„์—๋Š” ๊ด€๊ณ„๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์˜ˆ๋ฅผ ๋“ค์–ด, ๋‘ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•˜๋ฉด ํ•˜๋‚˜์˜ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์ •๋ณด๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

์ด๋Ÿฌํ•œ ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•˜๊ณ  ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์ •๊ทœํ™”๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ์ค‘๋ณต์„ ์ตœ์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ์˜ ์ผ๊ด€์„ฑ์„ ์œ ์ง€ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ตฌ์กฐ๋ฅผ ์ตœ์ ํ™”ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

  • tuple, attribute๋Š” ์ค‘๋ณต์„ ๋ถˆํ—ˆ
  • tuple, attribute์˜ ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค
  • attribute๋Š” atomic (ex. ์ข‹์•„ํ•˜๋Š” ์ƒ‰๊น” : ํŒŒ๋ž‘, ์ดˆ๋ก ๋ถˆ๊ฐ€๋Šฅ ์˜ค์ง ํ•˜๋‚˜์˜ ๊ฐ’)
  • relation(table)๋‚ด์—์„œ๋Š” ๊ฐ™์€ ์ด๋ฆ„์˜ attribute๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค
  • primary key๋Š” value๋กœ Null๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค 

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” ์‹ ๋ขฐ์„ฑ, ์ผ๊ด€์„ฑ, ํšจ์œจ์„ฑ ๋“ฑ์˜ ์žฅ์ ์„ ์ œ๊ณตํ•˜๋ฉฐ, ๋งŽ์€ ๊ธฐ์—… ๋ฐ ์กฐ์ง์—์„œ ๋‹ค์–‘ํ•œ ์‘์šฉ ๋ถ„์•ผ์—์„œ ํ™œ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 


 


 


Relation Schema 

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ Relation Schema(๊ด€๊ณ„ ์Šคํ‚ค๋งˆ)๋Š” ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. Relation์€ ํ…Œ์ด๋ธ”์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ์ด๋ฉฐ, Schema๋Š” ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. Relation Schema๋Š” ํŠน์ • ํ…Œ์ด๋ธ”์ด ์–ด๋–ค ์ปฌ๋Ÿผ๋“ค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ  ๊ฐ ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ ์œ ํ˜•, ์ œ์•ฝ ์กฐ๊ฑด ๋“ฑ์„ ๋ช…์‹œ์ ์œผ๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.


Relation Schema์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ

  • Relation Name (๊ด€๊ณ„ ์ด๋ฆ„): ํ…Œ์ด๋ธ”์˜ ์ด๋ฆ„
  • Attributes (์†์„ฑ): ํ…Œ์ด๋ธ”์˜ ๊ฐ ์ปฌ๋Ÿผ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ์†์„ฑ์€ ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ํ•„๋“œ ๋˜๋Š” ์—ด
  • Domain (๋„๋ฉ”์ธ): ๊ฐ ์†์„ฑ์ด ํ—ˆ์šฉํ•˜๋Š” ๊ฐ’์˜ ๋ฒ”์œ„ ๋˜๋Š” ์œ ํ˜• (์ •์ˆ˜ํ˜• ๋„๋ฉ”์ธ, ๋ฌธ์ž์—ด ๋„๋ฉ”์ธ) 
  • ๊ธฐํƒ€ ์ œ์•ฝ ์กฐ๊ฑด: Relation Schema๋Š” ํŠน์ • ์†์„ฑ์— ๋Œ€ํ•œ ์ œ์•ฝ ์กฐ๊ฑด์„ ์ •์˜ (ex. Primary Key, Foreign Key, Unique)

 

 

 


 

 

Keys

1) Primary Key: ๊ธฐ๋ณธ ํ‚ค๋Š” ํ…Œ์ด๋ธ” ๋‚ด์—์„œ ๊ฐ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ณ ์œ ํ•˜๊ฒŒ ์‹๋ณ„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ํ•„๋“œ

  • ๊ฐ ํ…Œ์ด๋ธ”์€ ์˜ค์ง ํ•˜๋‚˜์˜ ๊ธฐ๋ณธ ํ‚ค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ ํ‚ค๋Š” NULL ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†์œผ๋ฉฐ, ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด ๊ณ ์œ ํ•˜๊ณ  ์‹๋ณ„ ๊ฐ€๋Šฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. (์ค‘๋ณต x)

 

2) Candidate Key: ํ›„๋ณด ํ‚ค๋Š” Primary key๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ์œ ๋งํ•œ ํ‚ค

  • ํ…Œ์ด๋ธ”์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ›„๋ณด ํ‚ค๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํ›„๋ณด ํ‚ค๋Š” ๊ธฐ๋ณธ ํ‚ค๋กœ ์„ ํƒ๋˜์ง€ ์•Š๋”๋ผ๋„ ๊ฐ๊ฐ ๊ณ ์œ ํ•˜๊ฒŒ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

3) Unique Key (๊ณ ์œ  ํ‚ค): ๊ณ ์œ  ํ‚ค๋Š” ํ…Œ์ด๋ธ” ๋‚ด์—์„œ ์ค‘๋ณต ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์†์„ฑ์ด๋‚˜ ์†์„ฑ์˜ ์ง‘ํ•ฉ

  • ๊ธฐ๋ณธ ํ‚ค๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ณ ์œ  ํ‚ค๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • NULL ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋™์ผํ•œ ํ…Œ์ด๋ธ” ๋‚ด์—์„œ ์ค‘๋ณต๋œ ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

4) Foreign Key (์™ธ๋ž˜ ํ‚ค): ์™ธ๋ž˜ ํ‚ค๋Š” ํ•œ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์˜ ํ•„๋“œ

  • ์™ธ๋ž˜ ํ‚ค ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์™ธ๋ž˜ ํ‚ค๋Š” ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค์™€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ํ˜•์‹์ด์–ด์•ผ ํ•˜๋ฉฐ, ์ฐธ์กฐ ๋ฌด๊ฒฐ์„ฑ์„ ์œ ์ง€ํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์™ธ๋ž˜ ํ‚ค ๊ฐ’์ด ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธ ํ‚ค ๊ฐ’๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•จ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

 


 

 

 

์ œ์•ฝ ์กฐ๊ฑด(Constraints)์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์„ค์ •๋˜๋Š” ์กฐ๊ฑด์ด๋‚˜ ๊ทœ์น™์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ œ์•ฝ ์กฐ๊ฑด๋“ค์€ ๋ฐ์ดํ„ฐ์˜ ์ •ํ™•์„ฑ๊ณผ ์ผ๊ด€์„ฑ์„ ๋ณด์žฅํ•˜๋ฉฐ, ์ž˜๋ชป๋œ ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…์ด๋‚˜ ์ˆ˜์ •์„ ๋ฐฉ์ง€ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ’ˆ์งˆ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ฃผ์š”ํ•œ ์ œ์•ฝ ์กฐ๊ฑด๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

Key constraints

1) Primary key 

ํ•˜๋‚˜ ์ด์ƒ์˜ attributes๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ ์ค‘๋ณต๋œ ๊ฐ’, NULL๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค

 

 

2) Unique key 

Unique๋กœ ์ง€์ •๋œ attribute๋Š” ์ค‘๋ณต๋œ ๊ฐ’์„ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค

 

 

3) NOT NULL

NOT NULL constraint is used to specify that a column must have a value and cannot contain NULL.

 

 

4) Default 

In MySQL, the DEFAULT keyword is used to specify a default value for a column. 

 

 

5) Check

Allows you to enforce rules on the values in one or more columns of a table, ensuring that only valid data is stored.

 

 

6) Foreign key 

๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ Foreign key ์ œ์•ฝ ์กฐ๊ฑด๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

  1. Cascade ์ฐธ์กฐ๊ฐ’ ์‚ญ์ œ/๋ณ€๊ฒฝ์‹œ ๊ทธ๋Œ€๋กœ ๋ฐ˜์˜
  2. SET NULL ์ฐธ์กฐ๊ฐ’ ์‚ญ์ œ/๋ณ€๊ฒฝ์‹œ NULL๋กœ ๋ฐ˜์˜
  3. RESTRICT ์ฐธ์กฐ๊ฐ’ ์‚ญ์ œ/๋ณ€๊ฒฝ ๋˜๋Š” ๊ฒƒ์„ ๊ธˆ์ง€
  4. NO ACTION  =restrict 
  5. SET DEFAULT ์ฐธ์กฐ๊ฐ’ ์‚ญ์ œ/๋ณ€๊ฒฝ์‹œ default ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ 

์ด๋Ÿฌํ•œ ์˜ต์…˜์€ ๋ถ€๋ชจ ํ…Œ์ด๋ธ”(์™ธ๋ž˜ ํ‚ค๋กœ ์ฐธ์กฐ๋˜๋Š” ํ…Œ์ด๋ธ”)์—์„œ ์ฐธ์กฐ๋œ ํ–‰์ด ์ˆ˜์ • ๋˜๋Š” ์‚ญ์ œ๋  ๋•Œ ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. 

 

7) Constraint ์ด๋ฆ„ ๋ช…์‹œ

์ด๋ฆ„์ด ๋ช…์‹œ๋˜์–ด ์žˆ์œผ๋ฉด ์ž˜๋ชป๋œ ๊ฐ’์ด ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ, ์™œ ์ž…๋ ฅ์ด ์•ˆ ๋˜์—ˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

์ด๋Ÿฌํ•œ ์ œ์•ฝ ์กฐ๊ฑด๋“ค์€ CREATE TABLE ๋ฌธ์ด๋‚˜ ALTER TABLE ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •ํ•  ๋•Œ ์ •์˜๋ฉ๋‹ˆ๋‹ค. ์ œ์•ฝ ์กฐ๊ฑด์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‹œ์Šคํ…œ์— ์˜ํ•ด ๊ฒ€์ฆ๋˜๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ ์‹œ์Šคํ…œ(DBMS)์€ ์ด๋Ÿฌํ•œ ์ œ์•ฝ ์กฐ๊ฑด์„ ์œ„๋ฐ˜ํ•˜๋Š” ์—ฐ์‚ฐ์„ ๊ฑฐ๋ถ€ํ•ฉ๋‹ˆ๋‹ค.

 

 

 


Relational Database
A relational database is a collection of tables that are related to each other through common fields. It organizes data into structured tables, and the relationships between these tables are defined by keys.


Tuple
A tuple is a single row or record in a table. It represents a single data entry and contains values for each attribute or column in that particular row.


Attribute
An attribute is a characteristic or property of a tuple (row) in a table. It corresponds to a column in the table and represents a specific piece of information.


Relation (Table)
A relation, often referred to as a table, is a two-dimensional structure in a relational database that organizes data into rows and columns. Each row represents a tuple, and each column represents an attribute.


Relation Schema
A relation schema defines the structure of a table, specifying the attributes (columns) and their data types. It serves as a blueprint for creating tables in a relational database.


Primary Key
A primary key is a unique identifier for a tuple within a table. It ensures that each row in a table can be uniquely identified and is used to establish relationships between tables.


Candidate Key
A candidate key is a set of one or more attributes that could potentially serve as a primary key for a table. These are the candidate choices for the primary key before one is selected.


Unique Key
A unique key is a constraint that ensures that values in a particular column (or a set of columns) are unique across all rows in a table. It may or may not be the primary key.


Foreign Key:
A foreign key is a field in a table that refers to the primary key in another table. It establishes a link between the two tables, enforcing referential integrity.


Constraints
Constraints are rules or conditions applied to the data in a database to maintain its integrity. Common types include unique constraints, primary key constraints, foreign key constraints, and check constraints. Constraints ensure that the data in the database remains accurate and consistent.

 

'Computer Science > Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

6 Create(Insert), Update, Delete  (1) 2024.01.11
5 CRUD (Create, Read, Update and Delete) + Practice tables  (0) 2024.01.08
4 Database Languages - DDL, DML, SQL  (0) 2024.01.08
2 Data Model  (0) 2023.12.22
1 Data, Database, DBMS  (0) 2023.12.22

๋Œ“๊ธ€