Candidate key explained

A candidate key, or simply a key, of a relational database is any set of columns that have a unique combination of values in each row, with the additional constraint that removing any column could produce duplicate combinations of values.

A candidate key is a minimal superkey,[1] i.e., a superkey that doesn't contain a smaller one. Therefore, a relation can have multiple candidate keys, each with a different number of attributes.[2]

Specific candidate keys are sometimes called primary keys, secondary keys or alternate keys.The columns in a candidate key are called prime attributes,[3] and a column that does not occur in any candidate key is called a non-prime attribute.

Every relation without NULL values will have at least one candidate key: Since there cannot be duplicate rows, the set of all columns is a superkey, and if that isn't minimal, some subset of that will be minimal.

There is a functional dependency from the candidate key to all the attributes in the relation.

The superkeys of a relation are all the possible ways we can identify a row. The candidate keys are the minimal subsets of each superkey and as such, they are an important concept for the design of database schema.

Example

The definition of candidate keys can be illustrated with the following (abstract) example. Consider a relation variable (relvar) R with attributes (A, B, C, D) that has only the following two legal values r1 and r2:

r1
ABCD
a1b1c1d1
a1b2c2d1
a2b1c2d1
r2
ABCD
a1b1c1d1
a1b2c2d1
a1b1c2d2

Here r2 differs from r1 only in the A and D values of the last tuple.

For r1 the following sets have the uniqueness property, i.e., there are no two distinct tuples in the instance with the same attribute values in the set:

,,,,,,, For r2 the uniqueness property holds for the following sets;

,,,,,,, Since superkeys of a relvar are those sets of attributes that have the uniqueness property for all legal values of that relvar and because we assume that r1 and r2 are all the legal values that R can take, we can determine the set of superkeys of R by taking the intersection of the two lists:

,,,,, Finally we need to select those sets for which there is no proper subset in the list, which are in this case:

,, These are indeed the candidate keys of relvar R.

We have to consider all the relations that might be assigned to a relvar to determine whether a certain set of attributes is a candidate key. For example, if we had considered only r1 then we would have concluded that is a candidate key, which is incorrect. However, we might be able to conclude from such a relation that a certain set is not a candidate key, because that set does not have the uniqueness property (example for r1). Note that the existence of a proper subset of a set that has the uniqueness property cannot in general be used as evidence that the superset is not a candidate key. In particular, note that in the case of an empty relation, every subset of the heading has the uniqueness property, including the empty set.

Determining candidate keys

The set of all candidate keys can be computede.g. from the set of functional dependencies.To this end we need to define the attribute closure

\alpha+

for an attribute set

\alpha

.The set

\alpha+

contains all attributes that are functionally implied by

\alpha

.

It is quite simple to find a single candidate key.We start with a set

\alpha

of attributes and try to remove successively each attribute.If after removing an attribute the attribute closure stays the same,then this attribute is not necessary and we can remove it permanently.We call the result

minimize(\alpha)

.If

\alpha

is the set of all attributes,then

minimize(\alpha)

is a candidate key.

Actually we can detect every candidate key with this procedureby simply trying every possible order of removing attributes.However there are many more permutations of attributes (

n!

)than subsets (

2n

).That is, many attribute orders will lead to the same candidate key.

There is a fundamental difficulty for efficient algorithms for candidate key computation:Certain sets of functional dependencies lead to exponentially many candidate keys.Consider the

2 ⋅ n

functional dependencies

\{AiBi:i\in\{1,...,n\}\}\cup\{BiAi:i\in\{1,...,n\}\}

which yields

2n

candidate keys:

\{A1,B1\} x ... x \{An,Bn\}

.That is, the best we can expect is an algorithm that is efficient with respect to the number of candidate keys.

The following algorithm actually runs in polynomial time in the number of candidate keys and functional dependencies:[4]

function find_candidate_keys(A, F) /* A is the set of all attributes and F is the set of functional dependencies */ K[0] := minimize(A); n := 1; /* Number of Keys known so far */ i := 0; /* Currently processed key */ while i < n do for each α → β ∈ F do /* Build a new potential key from the previous known key and the current FD */ S := α ∪ (K[i] − β); /* Search whether the new potential key is part of the already known keys */ found := false; for j := 0 to n-1 do if K[j] ⊆ S then found := true; /* If not, add it */ if not found then K[n] := minimize(S); n := n + 1; i := i + 1 return K

The idea behind the algorithm is that given a candidate key

Ki

and a functional dependency

\alpha\beta

,the reverse application of the functional dependency yieldsthe set

\alpha\cup(Ki\setminus\beta)

,which is a key, too.It may however be covered by other already known candidate keys.(The algorithm checks this case using the 'found' variable.)If not, then minimizing the new key yields a new candidate key.The key insight is that all candidate keys can be created this way.

See also

References

. Christopher J. Date. An Introduction to Database Systems. Addison-Wesley. 978-0-321-18956-1. 2003. 268–276. 5: Integrity.

External links

Notes and References

  1. Web site: Codd's First Relational Papers: A Critical Analysis . Date . Christopher . 2015 . warwick.ac.uk . 2020-01-04 . Note that the extract allows a “relation” to have any number of primary keys, and moreover that such keys are allowed to be “redundant” (better: reducible). In other words, what the paper calls a primary key is what later (and better) became known as a superkey, and what the paper calls a nonredundant (better: irreducible) primary key is what later became known as a candidate key or (better) just a key..
  2. Web site: database - Can a relation have Candidate Keys with different lengths? . 2023-03-23 . Stack Overflow . en.
  3. Saiedian. H.. 1996-02-01. An Efficient Algorithm to Compute the Candidate Keys of a Relational Database Schema. The Computer Journal. en. 39. 2. 124–132. 10.1093/comjnl/39.2.124. 0010-4620.
  4. L. Lucchesi . Cláudio . Osborn . Sylvia L. . Candidate keys for relations . Journal of Computer and System Sciences . 17 . 2 . 270–279 . October 1978 . 10.1016/0022-0000(78)90009-0.