Keys and Superkeys: Formal Definitions
Defining a superkey
Let be a relational schema, where is the set of attributes (columns) of . Let be a subset of those attributes.
is a superkey for if, for any two records and in any valid instance of :
In words: if two records agree on all attributes in , they must be the same record. No two distinct rows in a valid instance of can have identical values for all attributes in .
The entire set of attributes is trivially a superkey for any relation, since tautologically implies .
Defining a key
is a key (or candidate key) for if:
- is a superkey for , and
- No proper subset of is a superkey for .
The second condition is minimality: you cannot remove any attribute from and still have a superkey. A key is a minimal superkey.
Notation
The notation indicates that is a key for , and that ‘s full set of attributes is . The underline marks the key.
For a table with multiple candidate keys, one is designated as the primary key. The others remain alternate keys.
Example: Movies table
Schema: Movies(movie_id, title, year)
- is a superkey: no two movies share the same
movie_id. - is also a key: removing the only element leaves the empty set, which is not a superkey.
- is a superkey but not a key (it is not minimal: you can drop
title). - might be a key if we assume no two films share both title and year — but this is a design choice, not a formal guarantee.
Example: Directed table (all-key)
Schema: Directed(movie_id, person_id)
- : not a superkey. A film can have multiple directors, so does not force .
- : not a superkey. A person can direct multiple films.
- : is a superkey. A given (movie, person) pair appears at most once in the Directed relationship.
- is also a key: neither subset alone is a superkey.
- No proper subset of is a superkey, so is minimal. This is an all-key table.
Example: People table
Schema: People(person_id, name, birthYear)
- is a key (assuming synthetic keys).
- might be a key if name+birthYear is unique — but it is not safe to rely on.
Multiple keys
A relation can have more than one key. A table of UK citizens might have both:
- (National Insurance number)
- (synthetic key)
Both are keys (minimal superkeys), and either could be chosen as primary key. In practice, synthetic keys are preferred (see the note on synthetic keys in Lecture 2).
Formal property: every relation has a key
Since the set of all attributes is always a superkey, we can always find a key by starting with and iteratively removing attributes while the result remains a superkey. The resulting minimal subset is a key. In the worst case, itself is the only key (e.g., an all-key relationship table).
Superkey vs. key summary
| Property | Superkey | Key |
|---|---|---|
| Uniquely identifies rows | Yes | Yes |
| Minimal | Not necessarily | Yes |
| is one? | Always | Only if no proper subset is a superkey |
| A table can have many? | Yes | Yes |
Connection to normalisation
The concept of a key becomes critical in normalisation (Lecture 5). A relation is in a normalised form if all non-key attributes are functionally dependent on the key — and no other set of attributes. This is a preview: the key is the anchor for all other data in the row.
Summary
- A superkey is any set of attributes that uniquely identifies rows.
- A key is a minimal superkey.
- is always a superkey.
- All-key tables occur for many-to-many relationship tables.
- Every relation has at least one key.
- The key is the foundation of normalisation: all data should depend on the key, the whole key, and nothing but the key.