๐Ÿ”‘ Database Keys: Superkey, Candidate Key, Primary Key, Foreign Key | ๐Ÿš€ Includes Problems for Information Processing Engineer Exam

Information Processing EngineerDatabaseDBKeyPrimary KeyForeign Key
Read in about 3 min read
Published: 2025-07-12
Last modified: 2025-07-12
View count: 59

Summary

Explore everything about Keys, a core concept in databases. This guide easily explains the characteristics of keys (uniqueness and minimality), the concepts and relationships of Superkeys, Candidate Keys, Primary Keys, Alternate Keys, and Foreign Keys with examples, and concludes with practice questions for the Information Processing Engineer certification exam.

๐Ÿ’ก Database Keys are a core concept in relational databases, used to uniquely identify rows (Tuples) in a table and define relationships between tables. They are frequently tested in the Information Processing Engineer exam, so it's crucial to clearly understand the characteristics and relationships of each key.

Key TypeCharacteristicsDescription
Primary KeyChosen from candidate keys, Not NullThe key selected from candidate keys to represent the table.
Alternate KeyRemaining candidate keysCandidate keys that were not selected as the primary key.
Candidate KeyUniqueness O, Minimality OA minimal set of attributes that can uniquely identify a tuple.
SuperkeyUniqueness O, Minimality XAn attribute or set of attributes that can uniquely identify a tuple.
Foreign KeyReferences another table's primary keyDefines relationships between tables and ensures referential integrity.

๐ŸŽฏ What is a Database Key?

A Key is an attribute or a set of attributes that serves as a unique identifier to find or sort tuples (rows) that meet a condition, distinguishing them from other tuples. Keys ensure data integrity and enable efficient data management.


โœจ Key Characteristics

Not all keys always satisfy these two properties, but they are essential for defining a candidate key.

  • Uniqueness: A key value must be able to uniquely identify a single tuple. For example, in a 'Student' table, the 'student_id' uniquely identifies each student, thus satisfying uniqueness.
  • Minimality: It must be composed of the minimum number of attributes that satisfy uniqueness. This means if any attribute is removed from the key, it no longer satisfies uniqueness.

๐Ÿ”‘ Types of Keys and Their Relationships

There are several types of keys in a database, which have an inclusive relationship with each other.

Mnemonic: APCS

Alternate Key + Primary Key = Candidate Key โŠ‚ Superkey.

A mnemonic for remembering the relationship between database keys
The Korean mnemonic '๊ธฐ๋Œ€์Šˆ' (Ki-Dae-Shu) is a popular way to remember this hierarchy.

Alternate Key + Primary Key = Candidate Key โŠ‚ Superkey.

1. Primary Key (PK)

  • Definition: A single key selected from the candidate keys. It represents the table.
  • Characteristics:
    • Cannot have a NULL value.
    • Cannot have duplicate values.
    • Only one primary key exists per table.
  • Selection Criteria: A candidate key that is short, does not change frequently, and does not have NULL values is usually selected as the primary key.
  • Example: Between the two candidate keys {student_id} and {resident_registration_number}, student_id is generally chosen as the primary key.

2. Alternate Key

  • Definition: The remaining candidate keys that were not selected as the primary key.
  • Characteristics: They retain the properties of a candidate key (uniqueness, minimality).
  • Example: If student_id is chosen as the primary key, resident_registration_number becomes an alternate key.

3. Candidate Key

  • Definition: A key composed of the minimum attributes required to uniquely identify each row in a table.
  • Characteristics: Must satisfy both uniqueness and minimality.
  • Example: In the 'Student' table example above:
    • {student_id}: Candidate key (Uniqueness O, Minimality O)
    • {resident_registration_number}: Candidate key (Uniqueness O, Minimality O)
    • {student_id, name}: Not a candidate key (Minimality X - uniqueness is satisfied by student_id alone).

4. Superkey

  • Definition: An attribute or a set of attributes that can uniquely identify each row in a table.
  • Characteristics: It satisfies uniqueness, but not necessarily minimality.
  • Example: Assuming a 'Student' table has attributes (student_id, resident_registration_number, name):
    • {student_id}: Superkey (Uniqueness O)
    • {resident_registration_number}: Superkey (Uniqueness O)
    • {student_id, name}: Superkey (Uniqueness O)
    • {student_id, resident_registration_number}: Superkey (Uniqueness O)

5. Foreign Key (FK)

  • Definition: An attribute in one table that references the primary key (PK) of another table.
  • Purpose: Used to establish a Relationship between tables and ensure referential integrity.
  • Characteristics:
    • Can only contain values that exist in the referenced primary key or NULL values.
    • The foreign key must have the same domain as the primary key it references.
  • Example: In an 'Enrollment' table, the student_id attribute is a foreign key that references the primary key student_id in the 'Student' table. This establishes a relationship showing which student is enrolled in which course.

๐Ÿ“ Practice Problems for the Engineer Exam

ProblemWhat is the key that can uniquely identify a tuple, satisfying uniqueness but not necessarily minimality?
Your Answer
Correct AnswerReveal Answer
ProblemWhat is the key that satisfies both uniqueness and minimality, making it a candidate to become the primary key?
Your Answer
Correct AnswerReveal Answer
ProblemWhat is the key selected from the candidate keys to represent the table? This key cannot have NULL values.
Your Answer
Correct AnswerReveal Answer
ProblemWhat are the remaining candidate keys that are not selected as the primary key called?
Your Answer
Correct AnswerReveal Answer
ProblemWhat is the key used to establish relationships between tables and ensure referential integrity by referencing the primary key of another table?
Your Answer
Correct AnswerReveal Answer