ArticleslgStudy

science

Candidate key

Candidate key is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Candidate key rather than just read about it. In short: 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, i.e., a superkey that does not contain a smaller one.

Key takeaways

  • Candidate key belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Candidate key to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Candidate key from memory before moving on to harder problems.

Reference excerpt

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, i.e., a superkey that does not contain a smaller one. Therefore, a relation can have multiple candidate keys, each with a different number of attributes. Specific candidate keys are sometimes called primary keys, secondary keys or alternate keys. The columns in a candidate key are called prime attributes, 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 is not 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:

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:

{A,B}, {A,C}, {B,C}, {A,B,C}, {A,B,D}, {A,C,D}, {B,C,D}, {A,B,C,D} For r2 the uniqueness property holds for the following sets;

{B,C}, {B,D}, {C,D}, {A,B,C}, {A,B,D}, {A,C,D}, {B,C,D}, {A,B,C,D} 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:

{B,C}, {A,B,C}, {A,B,D}, {A,C,D}, {B,C,D}, {A,B,C,D} Finally we need to select those sets for which there is no proper subset in the list, which are in this case:

{B,C}, {A,B,D}, {A,C,D} 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 {A,B} 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 {A,D} 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 computed e.g. from the set of functional dependencies. To this end we need to define the attribute closure α+ for an attribute set α. The set α+ contains all attributes that are functionally implied by α. It is quite simple to find a single candidate key. We start with a set α 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(α). If α is the set of all attributes, then minimize(α) is a candidate key. Actually we can detect every candidate key with this procedure by 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 {Ai→Bi:i∈{1,…,n}}∪{Bi→Ai:i∈{1,…,n}} which yields 2n candidate keys: {A1,B1}×⋯×{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:

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 α→β, the reverse application of the functional dependency yields the set α∪(Ki∖β), 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 Alternate key, a key that is not selected as a primary key among candidate keys for a relationship Compound key Database normalization Primary key Relational database Superkey Prime implicant is the corresponding notion of a candidate key in boolean logic

References

Date, Christopher (2003). "5: Integrity". An Introduction to Database Systems. Addison-Wesley. pp. 268–276. ISBN 978-0-321-18956-1.

External links Relational Database Management Systems - Database Design - Terms of Reference - Keys: An overview of the different types of keys in the RDBMS (Relational Database Management System).

Worked examples

Example 1 — a first encounter with Candidate key

Start with the simplest possible case. Write down what Candidate key claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Candidate key before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Candidate key ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Candidate key

In research
Candidate key appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Candidate key in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Candidate key is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data modeling, Database management systems, Relational model, so understanding it makes those chapters shorter.
In everyday life
Look for Candidate key outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Candidate key” →

How to study Candidate key in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Candidate key means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Candidate key out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Candidate key in simple terms?

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, i.e., a superkey that…

Why does Candidate key matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Candidate key?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Candidate key.

Tags

  • Data modeling
  • Database management systems
  • Relational model

Keep exploring