Database Ch.2.1 - About Keys

chezze·2023년 4월 17일
0

Database

목록 보기
3/5
post-thumbnail

What is Key?

key는 데이터베이스에서 조건에 만족하는 Record를 찾거나 순서대로 Sorting할 때 다른 튜플들과 구별할 수 있는 유일한 기준이 되는 Attribute이다.

Categorization of keys

key는 다음과 같이 7가지로 분류된다.

  • Candidate Key (후보 키)
  • Primary Key (기본 키)
  • Alternate Key (대체 키)
  • Super Key (슈퍼 키)
  • Unique Key (유일 키)
  • Composite Key (복합 키)
  • Foreign Key (외래 키)

Candidate Key

Candidate Key는 데이터베이스에서 Relation을 구성하는 Attributes 중에서,
각각의 Tuple을 고유하게 식별할 수 있는 Attribute 혹은 Set of Attributes를 뜻한다.

Primary Key가 될 수 있는 후보이다.

중복된 Tuple을 방지하기 위해, 모든 Relation은 반드시 하나 이상의 Candidate Key를 가져야 한다.

하나의 Candidate Key의 value를 통해 단 하나의 Tuple을 특정할 수 있어야 한다.
(Candidate Key의 유일성)

Candidate Key로 선택된 Attribute의 개수는 최소가 되어야 한다. (Candidate Key의 최소성)

※ 최소성에 대한 예시

Relation StudentInfo
-------------
- Attributes -
Student_ID
Student_Name
Student_Major

위 테이블에서, Candidate Key가 될 수 있는 Attribute 혹은 Set of Attributes :

  • Student_ID
  • Set (Student_Name, Student_Major)

Set (Student_ID, Student_Name, Student_Major) 도 각각의 Tuple을 고유하게 식별할 수 있는
Set of Attributes 이다.
그러나 Set (Student_Name, Student_Major) 만으로도 Candidate Key의 조건을 만족할 수 있으므로,
최소성 원칙에 의거하여 Set (Student_Name, Student_Major)는 Candidate Key가 될 수 없다.

Primary Key

Candidate Key 중 선택된 하나의 Key로, 하나의 Relation에는 단 하나의 Primary Key가 존재한다.

Candidate Key의 모든 특징을 가지며, NULL 값을 가질 수 없다.

Alternate Key

Alternate Key는 Candidate Key 중 Primary Key로 선택되지 않은 나머지 Key들을 의미한다.

Primary Key가 없어지게 되면 Alternate Key 중 하나가 Primary Key를 대체할 수 있다.

※ 어떤 Relation에서 Candidate Key의 조건을 만족시키는 Attribute 혹은 Set of Attributes가 단 하나라면,
해당 Attribute 혹은 Set of Attributes가 Primary Key로 선택되므로 Alternate Key가 존재하지 않는다.

Super Key

Super Key는 각 Tuple을 구분할 수 있는 Attribute 혹은 Set of Attributes이다.

Super Key는 유일성은 가지지만, 최소성은 가지지 않는다.
즉, 어떤 Key를 통해 각각의 Tuples를 구분할 수 있기만 하다면, 해당 Key는 Super Key이다.

모든 Candidate Key는 Super Key의 일부이다.

Super Key, Candidate Key, Primary Key, Alternate Key의 관계

Super Key, Candidate Key, Primary Key, Alternate Key의 관계를 Venn Diagram으로 나타내면 다음과 같다.

위의 그림과 같이 Candidate Key는 Super Key의 일부이며,
Primary Key는 Candidate Key에서 선택된 하나의 Key이고,
나머지 Candidate Key는 Alternate Key라고 불린다.

Unique Key

Unique Key는 유일성을 만족시키는 key를 의미한다.

Unique Key에는 Super key, Candidate key, Primary Key, Alternate Key가 포함된다.

Composite Key

Composite Key는 두 개 이상의 Attributes를 조합해 지정한 Key를 의미한다.

Relation StudentInfo
-------------
- Attributes -
Student_ID
Student_Name
Student_Major

위 Relation에서, (Student_ID, Student_Name) 은 두 개의 Attributes를 조합한 Composite Key이다.

Foreign Key

Foreign Key는 하나 또는 여러 개의 다른 Relation의 Primary Key Attribute를 참조하는 data의
Referential integrity(참조 무결성)를 확인하기 위해 사용되는 Key이다.

한 Relation에 저장된 정보가 다른 Relation과 링크되었을 때,
한 쪽이 수정되면 데이터의 일관성을 유지하기 위해 다른 테이블도 필수적으로 점검되어야 하는데,
DBMS가 이러한 점검을 수행하기 위한 가장 일반적인 방법이 Foreign Key이다.

Foreign Key는 참조되는 Relation에서 Unique Key를 참조하여야 한다.
(Unique Key가 아닌 Attribute를 참조하게 되면, 중복된 Data가 삽입되거나 무결성 위반이 발생할 수 있음)

Example)

Relation StudentInfo
--------------------------
- Attributes -
Student_ID		Student_Name		Student_Major						Course_Code (FK)
1001			Lucas				Architectural Engineering			ARCH301
1002			Haley				Computer Software Engineering		CSE101
1003			Jenna				Neuroscience						NEUR101
1004			Harry				Chemistry							CHEM201
1005			Anna				Computer Software Engineering		CSE101


Relation CourseInfo
--------------------------
Course_Code		Course_Name								Course_Credit
CSE101			Introduction to Computer Science		3
CHEM201			Organic Chemistry I						4
ARCH301			Architectural Design Studio III			5
NEUR101			Introduction to Neuroscience			3

위의 Relations에서 StudentInfo에서의 Course_Code를 Foreign Key로 지정하여 CourseInfo
참조할 수 있다.

profile
주니어 컴공학부생🌱

0개의 댓글