In a relational database, keys are special columns used to uniquely identify records (rows) in a table and to create links between different tables. They are crucial for ensuring data integrity and for building relationships between data.
Let’s use a simple Students
table to understand each type of key.
Students
Table Example:
StudentID | AdmissionNo | AadharNo | FirstName | LastName |
101 | 2025A | 1111… | Rahul | Sharma |
102 | 2025B | 2222… | Priya | Gupta |
103 | 2025C | 3333… | Amit | Singh |
Super Key
A Super Key is any column or a combination of columns whose values can uniquely identify a row in a table.
- It’s the most general type of key.
- A table can have many super keys.
- Example:
{StudentID}
is a super key (it’s unique).{AdmissionNo}
is a super key.{AadharNo}
is a super key.{StudentID, FirstName}
is also a super key (even thoughFirstName
is not needed, the combination is still unique).{AdmissionNo, FirstName, LastName}
is also a super key.
Candidate Key
A Candidate Key is a minimal super key. It’s a super key with no redundant (unnecessary) columns. It’s a “candidate” to become the primary key.
- It must uniquely identify a row.
- It must contain no extra columns.
- Example: From the super keys listed above, the minimal ones are:
{StudentID}
{AdmissionNo}
{AadharNo}
These are all candidate keys because you can’t remove any column from them and still have a unique identifier.{StudentID, FirstName}
is not a candidate key becauseFirstName
is redundant.
Primary Key
The Primary Key is the specific candidate key that the database designer chooses to be the main identifier for the table.
- It must be unique for each row.
- It cannot contain NULL (empty) values.
- There can be only one primary key per table.
- Example: A designer would choose one of the candidate keys to be the primary key. Let’s choose
StudentID
. It’s the most common choice as it’s a simple, unique number created specifically to identify students.
Composite Key
A Composite Key is a key that consists of two or more columns combined to uniquely identify a row. It is used when a single column is not enough to create a unique identifier.
- It’s a type of primary key or candidate key that has multiple columns.
- Example: Consider a new
Enrollment
table.
CourseID | StudentID | Grade |
C10 | 101 | A |
C12 | 101 | B |
C10 | 102 | A |
Here, CourseID
alone is not unique, and StudentID
alone is not unique. But the combination {CourseID, StudentID}
is unique and would serve as the composite primary key.
Foreign Key
A Foreign Key is a column or a set of columns in one table that refers to the primary key of another table. It’s used to link two tables together.
- It establishes a relationship between tables.
- The value of a foreign key must match an existing value in the primary key of the referenced table.
- Example: Let’s use the
Enrollment
table again.- The
StudentID
column in theEnrollment
table is a foreign key. - It refers to the primary key (
StudentID
) of theStudents
table. - This link ensures that you cannot enroll a student in a course if that student does not exist in the
Students
table.
- The
MCQ
Which key uniquely identifies a row in a table and cannot contain NULL values?
A) Candidate Key
B) Foreign Key
C) Primary Key
D) Super Key
Answer: C
Which key is used to link two tables in a relational database?
A) Primary Key
B) Composite Key
C) Candidate Key
D) Foreign Key
Answer: D
What is the purpose of a foreign key in a database?
A) To ensure data integrity within a single table
B) To link two tables and maintain referential integrity
C) To store large amounts of data efficiently
D) To generate unique values for each record
Answer: B
in a relational database, which key ensures that no duplicate values exist in a column or set of columns?
A) Foreign Key
B) Candidate Key
C) Primary Key
D) Composite Key
Answer: C
Which of the following is an example of a composite key?
A) Employee_ID
B) {Order_ID, Product_ID}
C) Email_ID
D) Department_Name
Answer: B
What is the main difference between a candidate key and a primary key?
A) Candidate keys cannot be NULL, but primary keys can.
B) Candidate keys are unique identifiers, but only one is selected as the primary key.
C) Candidate keys enforce referential integrity, while primary keys do not.
D) Candidate keys must include all columns in a table, but primary keys do not.
Answer: B
Which key can contain redundant attributes in addition to the minimum required attributes to uniquely identify a row?
A) Primary Key
B) Composite Key
C) Super Key
D) Foreign Key
Answer: C
How many primary keys can a table have?
A) One
B) Two
C) Unlimited
D) None
Answer: A
What is a super key that contains no unnecessary attributes called?
A) Composite Key
B) Candidate Key
C) Primary Key
D) Foreign Key
Answer: B
Which of the following is true about a foreign key?
A) It must always match the primary key in the same table.
B) It must always match the primary key in a related table.
C) It can have duplicate values within the same table.
D) Both B and C.
Answer: D
In a banking system, which key is best suited to link a customer table with an accounts table?
A) Primary Key
B) Candidate Key
C) Composite Key
D) Foreign Key
Answer: D
Which key is most often selected as the primary key from a set of candidate keys?
A) The key with the longest name
B) The key that best ensures uniqueness and simplicity
C) The key with the highest number of attributes
D) The key with foreign relationships
Answer: B