The Primary Key is Selected from the Set of ___________
Correct Answer: (C) Candidate Keys
Explanation:
What is a Primary Key?
A primary key is a special column (or a combination of columns) in a database table that uniquely identifies each row. The primary key ensures:
- Every row in the table has a unique value for the primary key.
- The primary key column cannot contain
NULL
values.
For example:
-- Example Table: Students | StudentID | Name | Age | |-----------|---------|-----| | 1 | Alice | 20 | | 2 | Bob | 22 | | 3 | Charlie | 21 |
Here, StudentID
can be the primary key because it uniquely identifies each student.
How is a Primary Key Selected?
A primary key is selected from the set of candidate keys. Let’s break down the key concepts:
1. Candidate Keys:
A candidate key is any column (or combination of columns) in a table that can uniquely identify each row. A table can have multiple candidate keys.
For example, in the Students
table, both StudentID
and a combination of Name
and Age
could act as candidate keys (assuming unique entries).
2. Alternate Keys:
If there are multiple candidate keys, the ones not chosen as the primary key are called alternate keys.
3. Foreign Keys:
A foreign key is a column in one table that establishes a relationship with the primary key of another table. It is not used for selecting a primary key.
4. Composite Keys:
A composite key is a candidate key that consists of two or more columns. If chosen, it can act as a primary key, but it must still come from the set of candidate keys.
Example:
Consider a table:
-- Example Table: Orders | OrderID | CustomerID | OrderDate | |---------|------------|------------| | 101 | C001 | 2024-12-01 | | 102 | C002 | 2024-12-02 |
In this table:
OrderID
is a candidate key and can be chosen as the primary key.CustomerID
alone is not unique, so it is not a candidate key.
The primary key is always selected from the set of candidate keys. Alternate keys, composite keys, or foreign keys do not directly qualify as primary keys unless they are also part of the candidate key set.