Skip to content
Part IA Michaelmas Term

Keys and Superkeys: Formal Definitions

Defining a superkey

Let R(X)R(X) be a relational schema, where XX is the set of attributes (columns) of RR. Let ZXZ \subseteq X be a subset of those attributes.

ZZ is a superkey for RR if, for any two records uu and vv in any valid instance of RR:

u[Z]=v[Z]    u[X]=v[X]u[Z] = v[Z] \implies u[X] = v[X]

In words: if two records agree on all attributes in ZZ, they must be the same record. No two distinct rows in a valid instance of RR can have identical values for all attributes in ZZ.

The entire set of attributes XX is trivially a superkey for any relation, since u[X]=v[X]u[X] = v[X] tautologically implies u[X]=v[X]u[X] = v[X].

Defining a key

ZZ is a key (or candidate key) for RR if:

  1. ZZ is a superkey for RR, and
  2. No proper subset of ZZ is a superkey for RR.

The second condition is minimality: you cannot remove any attribute from ZZ and still have a superkey. A key is a minimal superkey.

Notation

The notation R(Z,Y)R(\underline{Z}, Y) indicates that ZZ is a key for RR, and that RR‘s full set of attributes is ZYZ \cup Y. 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)

  • {movie_id}\{\text{movie\_id}\} is a superkey: no two movies share the same movie_id.
  • {movie_id}\{\text{movie\_id}\} is also a key: removing the only element leaves the empty set, which is not a superkey.
  • {movie_id,title}\{\text{movie\_id}, \text{title}\} is a superkey but not a key (it is not minimal: you can drop title).
  • {title,year}\{\text{title}, \text{year}\} 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)

  • Z1={movie_id}Z_1 = \{\text{movie\_id}\}: not a superkey. A film can have multiple directors, so u[movie_id]=v[movie_id]u[\text{movie\_id}] = v[\text{movie\_id}] does not force u=vu = v.
  • Z2={person_id}Z_2 = \{\text{person\_id}\}: not a superkey. A person can direct multiple films.
  • Z3={movie_id,person_id}Z_3 = \{\text{movie\_id}, \text{person\_id}\}: is a superkey. A given (movie, person) pair appears at most once in the Directed relationship.
  • Z3Z_3 is also a key: neither subset alone is a superkey.
  • No proper subset of Z3Z_3 is a superkey, so Z3Z_3 is minimal. This is an all-key table.

Example: People table

Schema: People(person_id, name, birthYear)

  • {person_id}\{\text{person\_id}\} is a key (assuming synthetic keys).
  • {name,birthYear}\{\text{name}, \text{birthYear}\} 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:

  • {ni_number}\{\text{ni\_number}\} (National Insurance number)
  • {person_id}\{\text{person\_id}\} (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 XX is always a superkey, we can always find a key by starting with XX and iteratively removing attributes while the result remains a superkey. The resulting minimal subset is a key. In the worst case, XX itself is the only key (e.g., an all-key relationship table).

Superkey vs. key summary

PropertySuperkeyKey
Uniquely identifies rowsYesYes
MinimalNot necessarilyYes
all attributes\text{all attributes} is one?AlwaysOnly if no proper subset is a superkey
A table can have many?YesYes

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.
  • {all attributes}\{\text{all attributes}\} 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.