Definitionintro
Candidate key
Also called superkey, minimal key.
A set of attributes that functionally determines every attribute of a relation and has no proper subset that does.
A candidate key of a schema is a set of attributes that determines all the other attributes under the functional dependencies of the schema, and that is minimal with this property. This page defines keys, superkeys, prime attributes and primary keys, and gives their basic properties. The attribute closure algorithm tests whether a set is a superkey, the dependency implication problem page finds one key and lists all keys, and the prime attribute page proves that asking whether an attribute is in some key is NP-complete.
Definition
Let be a finite set of attributes and a finite set of FDs over . For , let be the closure of under , the set of attributes with .
- A superkey is a set with , that is . In a relation that satisfies , under set semantics, two distinct tuples never agree on a superkey.
- A candidate key, or simply a key, is a superkey such that no proper subset of is a superkey.
- A prime attribute is an attribute that belongs to at least one candidate key. The others are non-prime.
- A primary key is one candidate key that the designer chooses as the main identifier of the tuples. The other candidate keys are alternate keys.
Superkeys are closed upward: if is a superkey and , then is a superkey. So a superkey is a candidate key exactly when is not a superkey for each . This gives a test with closures.
Every schema has a key. is a superkey. Go through the attributes of in any order, and drop each attribute whose removal leaves a superkey. The result is a superkey. For each , the set from which was not dropped contains , and removing from it gave no superkey, so by upward closure is not a superkey either. So is a candidate key, found with closures.
Keys form an antichain. No candidate key contains another, by minimality. By Sperner's theorem on antichains of subsets, a schema with attributes has at most candidate keys, and this bound is reached. With , each set of attributes is a key and no smaller set is a superkey. For this gives the six keys , , , , , , and for it gives 20 keys.
Keys of a schema, not of an instance. A key is a property of . One relation can happen to have distinct values in some column without that column being a key, and adding a tuple can then make two tuples agree on it. In SQL, the PRIMARY KEY and UNIQUE constraints declare keys so that the system rejects such tuples.
Examples and non-examples
- One key. with employee, department and department name, and . Here , so is a key. , so is not a superkey. is a superkey but not a candidate key, because alone is one.
- Two keys of the same size. with city, street and zip, and . The keys are and , so every attribute is prime.
- Minimal is not minimum. and . The keys are and . Both are minimal under set inclusion, but they have different sizes. The key with the fewest attributes is a different question, and deciding whether a key of at most attributes exists is NP-complete.
- A key with two attributes. with student, course, grade and student name, and . The only key is . is not a key, because .
- No FDs. If , then for each , and the only key is . Every attribute is prime.
- The empty key. If , then is the only key, and every relation that satisfies has at most one tuple.
- Non-example: unique by chance. In a relation over in which no two employees share a department, the column has distinct values, but is not a key of the schema, because does not imply .
Related definitions
- Superkey. Every candidate key is a superkey, but a superkey need not be minimal. Some texts use the word loosely for a key. Here a superkey is any set with , minimal or not.
- Prime attribute problem. Whether a given attribute is prime is NP-complete, and there can be exponentially many keys. See the prime attribute problem.
- Normal forms. Second normal form forbids a non-prime attribute that depends on part of a key. Third normal form and BCNF ask that the left side of each nontrivial FD is a superkey, with an exception for prime attributes in 3NF.
- Foreign keys. A foreign key is a set of attributes whose values must appear as values of a key of another relation. In dependency theory it is an inclusion dependency whose right side is a key.
- Keys in the ER model. An entity set of the entity-relationship model has a key, and a weak entity set has a partial key that becomes a key only together with the key of its owner.
References
- E. F. Codd, A relational model of data for large shared data banks, Communications of the ACM 13(6), 377 to 387 (1970), primary keys of relations
- E. F. Codd, Further normalization of the data base relational model, in R. Rustin (ed.), Data Base Systems, Courant Computer Science Symposia 6, Prentice-Hall (1972), 33 to 64, candidate keys and prime attributes, used to define second and third normal form
- Cláudio L. Lucchesi and Sylvia L. Osborn, Candidate keys for relations, Journal of Computer and System Sciences 17(2), 270 to 279 (1978), finding one key, listing all keys, and the hardness of the prime attribute and bounded key problems
- Jeffrey D. Ullman, Principles of Database and Knowledge-Base Systems, Volume I, Computer Science Press (1988), Chapter 7 (design theory for relational databases), keys of a relation scheme
- Serge Abiteboul, Richard Hull and Victor Vianu, Foundations of Databases, Addison-Wesley (1995), Chapter 8 (functional and join dependency) and Chapter 11 (design and dependencies), keys and superkeys
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag BYCHZG Edit this page History Report an error Markdown