Sub topic 7
Superkeys, Candidate Keys, and Primary Keys
This chapter assumes you know what a relation, tuple, and attribute are, and that you have written a basic CREATE TABLE. It covers the key vocabulary that GATE questions use. It also covers how to count keys, and the mistakes that cost marks.
Why keys matter
A relation is a set of tuples, so no two tuples may be identical. A key says which attribute values pick out exactly one tuple. Entity integrity, foreign keys, and every normal form later in this tutorial rest on this idea.
The definitions
Let R be a relation schema with attribute set U.
- Superkey: a set of attributes K such that no two distinct tuples in any legal instance of R agree on all attributes of K. Equivalently, K functionally determines every attribute of R. U itself is always a superkey.
- Candidate key: a minimal superkey. Removing any attribute from it makes it stop being a superkey.
- Primary key: the one candidate key the designer chooses to identify tuples. It cannot contain NULL.
- Alternate key: a candidate key that was not chosen as the primary key.
- Foreign key: an attribute set in one relation that refers to a candidate key (usually the primary key) of another relation. Referential integrity is covered in a later chapter.
- Prime attribute: an attribute that belongs to at least one candidate key. All other attributes are non-prime. Normal forms depend on this distinction.
Keys belong to the schema, not the data
A key is a constraint on every legal instance. You cannot prove it by looking at the rows you currently have. The code below lists the minimal attribute sets that happen to be unique in one small instance.
from itertools import combinations
attrs = ["roll_no", "email", "name", "dept"]
rows = [
{"roll_no": 1, "email": "[email protected]", "name": "Asha", "dept": "CS"},
{"roll_no": 2, "email": "[email protected]", "name": "Ravi", "dept": "CS"},
{"roll_no": 3, "email": "[email protected]", "name": "Asha", "dept": "EE"},
]
def is_unique(attr_set):
seen = set()
for r in rows:
t = tuple(r[a] for a in attr_set)
if t in seen:
return False
seen.add(t)
return True
found = []
for size in range(1, len(attrs) + 1):
for combo in combinations(attrs, size):
if any(set(f) <= set(combo) for f in found):
continue # a smaller unique set is already inside it
if is_unique(combo):
found.append(combo)
print(found) # [('roll_no',), ('email',), ('name', 'dept')]In this instance (name, dept) looks like a key. Nothing in the real world stops two students named Asha from sharing a department, so it is not a key of the schema. In GATE, keys come from stated functional dependencies, not from sample rows.
Counting superkeys
A relation R(A, B, C, D) has 2^4 = 16 attribute subsets. Count the superkeys in two cases.
- One candidate key of size 1, say A. Every superkey contains A, and the other n − 1 attributes are free. The count is 2^(n−1) = 2^3 = 8.
- Candidate keys A and BC. Count the sets containing A, then the sets containing B and C, then subtract the overlap (sets containing A, B, and C). This gives 8 + 4 − 2 = 10.
For several candidate keys, use inclusion-exclusion over the sets "contains key i". The maximum number of candidate keys a relation with n attributes can have is C(n, ⌊n/2⌋). The reason is that candidate keys form an antichain, meaning none contains another, and Sperner's theorem bounds the size of an antichain.
Composite keys and minimality
A composite key has more than one attribute. Minimality is checked per attribute. In Enrollment(studentid, courseid, grade), the set {studentid, courseid} is a candidate key. {studentid, courseid, grade} is a superkey but not a candidate key, because grade can be removed.
Two relations can have different candidate keys under different FDs even if they have the same attributes. Always read the FDs before declaring a key.
Choosing the primary key
All candidate keys are equally valid logically. The primary key is a design decision. Good choices are short, stable, and never NULL. An email address is unique, but it can change, so a surrogate such as roll_no is usually the better primary key.
CREATE TABLE student (
roll_no INT PRIMARY KEY,
email VARCHAR(80) NOT NULL UNIQUE,
name VARCHAR(50) NOT NULL,
dept VARCHAR(20)
);
CREATE TABLE enrollment (
roll_no INT REFERENCES student(roll_no),
course_id INT,
grade CHAR(2),
PRIMARY KEY (roll_no, course_id)
);Here roll_no is the primary key and email is an alternate key. In enrollment, the composite primary key is also a candidate key. roll_no alone is a foreign key and not a key of enrollment.
Common errors
- Treating "unique" as "key". A UNIQUE column can be a candidate key, but only if the schema requires it.
- NULL in alternate keys. The standard lets a UNIQUE column hold NULLs, and several systems allow more than one NULL in it. Primary keys cannot be NULL (entity integrity). In SQL Server, only one NULL is allowed in a UNIQUE column.
- Forgetting that a foreign key need not be unique. Many
enrollmentrows may carry the sameroll_no. - Confusing "a candidate key" with "the candidate key". A relation can have several candidate keys of different sizes.
- Mixing up superkey and key. GATE often asks for the number of superkeys, and the count includes non-minimal sets.
| Term | Minimal? | NULL allowed? | How many per relation |
|---|---|---|---|
| Superkey | No | Not defined by itself | Many |
| Candidate key | Yes | Depends on declaration | One or more |
| Primary key | Yes | No | Exactly one |
| Alternate key | Yes | Depends on declaration | Zero or more |
Exercise
R(A, B, C, D) has candidate keys A and BC. How many superkeys does R have, and is {A, B, C} a candidate key?
Show answer
There are 10 superkeys. Sets containing A number 2^3 = 8. Sets containing B and C number 2^2 = 4. Sets containing A, B, and C number 2. So 8 + 4 − 2 = 10. {A, B, C} is a superkey but not a candidate key, because removing B and C leaves A, which is already a superkey.
Exercise
A table holds three rows and no two share an email. Can you conclude that email is a candidate key of the schema?
Show answer
No. A key is a constraint on all legal instances, so you need a rule (an FD or a UNIQUE declaration) that guarantees it. Three rows that happen to differ prove nothing about future inserts.