Theoremcore
Codd's theorem
Also called equivalence of relational algebra and calculus.
Relational algebra and the domain-independent (or safe-range) relational calculus express exactly the same queries, under set semantics, and each translates into the other.
Codd's theorem says that relational algebra and the relational calculus, restricted to domain-independent queries, express exactly the same queries. Each algebra expression translates into a calculus formula with the same answer on every database, and each domain-independent formula translates into an algebra expression. Codd proved a form of it in 1972 for his own tuple calculus, and called a language relationally complete when it expresses all these queries. This page proves the form of Abiteboul, Hull and Vianu (AHV), which also brings in the safe-range formulas and the active domain semantics.
Statement
Fix a database schema, a countably infinite set of constants dom, and set semantics. The relational algebra has the relation names, the constant relations , and the operators , , , , , (set difference) and (natural join, which is the product when the attribute sets are disjoint). The calculus is the domain calculus with the three readings of the relational calculus page. Two queries are equivalent when they have the same answer on every instance (for calculus queries, under the stated reading). A tuple of a calculus query is read as a tuple over attributes named by its free variables.
Theorem (Codd 1972; AHV, Section 5.4). The following four classes of queries are the same:
- the queries of the relational algebra;
- the domain-independent queries of the calculus;
- the safe-range queries of the calculus;
- the queries of the calculus under active domain semantics.
The translations are effective: there are algorithms that map a query of each class to an equivalent query of each other class.
In plain words
There are two ways to write a database query. One is a recipe: take this table, keep these rows, join with that table, remove these columns. The other is a description: give me every student such that there is a course the student takes and no required course the student skips. The theorem says that the two ways are equally strong. Every recipe can be written as a description, and every sensible description can be turned into a recipe.
"Sensible" matters. A description such as "every value that is not a student" has an answer that depends on the set of all possible values, not on the data. Such descriptions are excluded. The ones that remain can be checked by a simple syntax rule, and a database system can turn each of them into a plan that it runs.
Proof
Write for the constants of the instance and of the query . The proof shows (1) to (3), (3) to (2), (2) to (4) and (4) to (1).
(1) to (3): algebra to safe-range calculus. For an expression with attributes , use one variable per attribute and build with :
- : ; : ;
- : ; : ;
- : , where are the variables of the attributes not in ; : with renamed to ;
- : ; : ; : , where shared attributes share a variable.
Same answer. By induction on : for every and every valuation into , if and only if the tuple is in . The atom, constant, selection, renaming, union, difference and join cases follow from the definitions of the operators and of the connectives. For : a tuple over is in if and only if some tuple extends . Every value of is in , so the witness for exists in , and by the induction hypothesis the two conditions agree. So gives on every domain, and the query is domain independent.
Safe-range. By induction, , with the rules of domain-independent query. An atom restricts its variables, restricts , and adds both variables because is already restricted. In each is restricted in , so the rule for succeeds. In a union both sides have the same free variables, all restricted, so the intersection keeps them all. For , the SRNF only moves the negation in front of atoms and of subformulas of , which already pass the rule for ; so no appears, and the conjunction keeps , which is all the free variables. A join is a union of the two restricted sets.
(3) to (2): safe-range queries are domain independent. Let be in SRNF with . Two claims hold for each subformula and each pair .
Claim A. If maps into and , then for each . Induction: an atom of holds only on values of ; forces ; in the equality copies a value in from one variable to the other; a conjunction is true only if each conjunct is; a disjunction is true only if one side is, and keeps only the variables that both sides restrict; claims nothing; for , take the witness value and apply the hypothesis to extended by .
Claim B. If maps into , then if and only if . Induction: atoms and equalities do not look at the domain, and the connectives keep the property. For , suppose it is true in with witness . The rule for succeeded, so , and Claim A gives . The extended valuation maps into , so by the hypothesis is true in and is a witness there. The other direction is the same.
Now let , given by . All free variables are in , so by Claim A, maps into , and by Claim B, . By symmetry .
(2) to (4). A domain-independent has , which is the active domain answer of the same formula.
(4) to (1): active domain calculus to algebra. First an expression for the active domain, with one attribute :
. Write for . Replace by and by . For each subformula with free variables , build with one attribute per variable of , such that
- : apply to each position that holds a constant and to each two positions with the same variable, then project on one position per variable and rename it to the variable.
- : . : . : .
- : .
- : extend each side to the variables by a product with for each missing , then take the union.
- : for , with for .
- : if is free in , and if not; the factor is if the active domain is not empty and if it is, as the semantics of requires.
Each case follows from the definition of truth with the variables ranging over : the join matches valuations on the shared variables, the padding and the complement are taken inside , and the projection is the existential quantifier over . For the query , the expression gives the active domain answer. So each class is contained in the next one around the cycle, and the four classes are equal. Each step is an algorithm on the syntax.
Consequences
- An example of both directions. Take with the tuples , , , . The safe-range query has the algebra form , and both give . The formula alone is not domain independent. Step (4) to (1) turns it into , whose answer is : the active domain answer, which includes the two course names.
- Relational completeness. Codd used the theorem as a yardstick: a query language is relationally complete if it expresses every algebra query. The SELECT FROM WHERE core of SQL with NOT EXISTS, UNION and EXCEPT is relationally complete under set semantics.
- Compilation of declarative queries. A system can accept a declarative query, check that it is safe-range, and translate it into an algebra plan. The plan is then rewritten by algebraic laws during query optimization.
- Transfer of results. Results proved for one language hold for the other. The calculus is first-order logic, so the undecidability of finite satisfiability (Trakhtenbrot) makes equivalence and containment of algebra expressions undecidable. Logical bounds on expressive power apply to the algebra too: the transitive closure of a binary relation is not expressible (Aho and Ullman 1979), which is why Datalog adds recursion. The data complexity bounds of first-order logic also hold for the algebra; see data complexity and combined complexity.
- Sizes of the translations. Step (1) to (3) gives a formula with one connective or quantifier per operator. Step (4) to (1) gives one operator per connective, plus copies of , whose size is linear in the schema and the number of constants; so the output size is polynomial in the input.
- Fragments. The same construction, restricted, gives smaller equivalences: the SPJR algebra (selection, projection, join, renaming) and the calculus with only and over atoms both express the conjunctive queries, and adding union on one side and on the other gives the unions of conjunctive queries.
Tightness
- Domain-dependent queries have no algebra form. Under natural semantics, has an infinite answer, while an algebra expression always has a finite answer made of active domain values. So the restriction to domain-independent queries in (2) cannot be dropped. Under active domain semantics the formula has an algebra form, but with another meaning.
- Set semantics. The theorem is about sets. Under bag semantics the algebra keeps multiplicities: can hold one value twice, and a calculus whose answers are sets cannot express that answer. SQL works on bags, and its relation to the calculus needs a separate treatment.
- Nulls. The theorem assumes two-valued logic and no nulls. SQL with nulls uses a three-valued logic, so a translation between SQL and a calculus query must handle UNKNOWN; see three-valued logic of SQL nulls.
- Aggregates, order and recursion. Counting, sums, comparisons with a built-in order and recursion are outside both languages. An equivalence for an extended language must be proved again for that extension.
- Codd's calculus. Codd's 1972 calculus binds each tuple variable to a relation, and his proof is a reduction algorithm for that syntax. It is not the textbook domain calculus, and his notion of completeness was relative to his calculus. The modern form above is the one of AHV and of Ullman.
History
Codd introduced the relational model in 1970 and defined the relational algebra and a tuple calculus in the 1972 paper "Relational completeness of data base sublanguages". There he gave an algorithm that reduces each calculus query to an algebra expression, and he defined relational completeness. Later textbooks, among them Ullman (1988) and Abiteboul, Hull and Vianu (1995), restated the result for the domain calculus with domain independence, safe formulas and the active domain semantics, and gave the four-way equivalence proved above. Di Paola had shown in 1969 that definite formulas, the same idea as domain independence, are undecidable, which is why a syntactic class such as the safe-range formulas is needed.
References
- E. F. Codd, Relational completeness of data base sublanguages, in R. Rustin (ed.), Data Base Systems, Courant Computer Science Symposia 6, Prentice-Hall (1972), 65 to 98, the relational algebra, the tuple calculus with range-coupled quantifiers, the reduction algorithm from the calculus to the algebra, and the definition of relational completeness
- Serge Abiteboul, Richard Hull and Victor Vianu, Foundations of Databases, Addison-Wesley (1995), Section 5.1 (the relational algebras), Section 5.3 (the calculus and domain independence), Section 5.4 (safe-range formulas and the equivalence of the algebra, the domain-independent calculus, the safe-range calculus and the active domain calculus)
- Jeffrey D. Ullman, Principles of Database and Knowledge-Base Systems, Volume I, Computer Science Press (1988), Chapter 3 (the relational algebra, the domain and tuple calculus, safe formulas and the equivalence proofs)
- Alfred V. Aho and Jeffrey D. Ullman, The universality of data retrieval languages, Conference Record of the 6th ACM Symposium on Principles of Programming Languages (POPL), 110 to 120 (1979), the transitive closure is not expressible in relational algebra, a limit of relational completeness
- E. F. Codd, A relational model of data for large shared data banks, Communications of the ACM 13(6), 377 to 387 (1970), the relational model and its first operators
- Robert A. Di Paola, The recursive unsolvability of the decision problem for the class of definite formulas, Journal of the ACM 16(2), 324 to 327 (1969), definite (domain-independent) formulas are undecidable
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 TP5RJX Edit this page History Report an error Markdown