Algorithm Search⌘K

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 {⟨A:c⟩}\{\langle A : c \rangle\}, and the operators σA=c\sigma_{A = c}, σA=B\sigma_{A = B}, πX\pi_X, ρA→B\rho_{A \to B}, ∪\cup, −- (set difference) and ⋈\bowtie (natural join, which is the product ×\times 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:

  1. the queries of the relational algebra;
  2. the domain-independent queries of the calculus;
  3. the safe-range queries of the calculus;
  4. 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 adom=adom(Q,I)\mathrm{adom} = \mathrm{adom}(Q, I) for the constants of the instance II and of the query QQ. 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 EE with attributes A1,…,AkA_1, \ldots, A_k, use one variable xAx_A per attribute and build φE\varphi_E with free(φE)={xA1,…,xAk}\mathrm{free}(\varphi_E) = \{x_{A_1}, \ldots, x_{A_k}\}:

  • RR: R(xA1,…,xAk)R(x_{A_1}, \ldots, x_{A_k}); {⟨A:c⟩}\{\langle A : c \rangle\}: xA=cx_A = c;
  • σA=c(E)\sigma_{A = c}(E): φE∧xA=c\varphi_E \land x_A = c; σA=B(E)\sigma_{A = B}(E): φE∧xA=xB\varphi_E \land x_A = x_B;
  • πX(E)\pi_X(E): ∃yˉ φE\exists \bar y\, \varphi_E, where yˉ\bar y are the variables of the attributes not in XX; ρA→B(E)\rho_{A \to B}(E): φE\varphi_E with xAx_A renamed to xBx_B;
  • E1∪E2E_1 \cup E_2: φE1∨φE2\varphi_{E_1} \lor \varphi_{E_2}; E1−E2E_1 - E_2: φE1∧¬φE2\varphi_{E_1} \land \neg \varphi_{E_2}; E1⋈E2E_1 \bowtie E_2: φE1∧φE2\varphi_{E_1} \land \varphi_{E_2}, where shared attributes share a variable.

Same answer. By induction on EE: for every d⊇adom\mathbf{d} \supseteq \mathrm{adom} and every valuation ν\nu into d\mathbf{d}, I⊨dφE[ν]I \models_{\mathbf{d}} \varphi_E[\nu] if and only if the tuple ν(xA1),…,ν(xAk)\nu(x_{A_1}), \ldots, \nu(x_{A_k}) is in E(I)E(I). The atom, constant, selection, renaming, union, difference and join cases follow from the definitions of the operators and of the connectives. For πX(E)\pi_X(E): a tuple tt over XX is in πX(E)(I)\pi_X(E)(I) if and only if some tuple s∈E(I)s \in E(I) extends tt. Every value of ss is in adom⊆d\mathrm{adom} \subseteq \mathbf{d}, so the witness for ∃yˉ\exists \bar y exists in d\mathbf{d}, and by the induction hypothesis the two conditions agree. So φE\varphi_E gives E(I)E(I) on every domain, and the query is domain independent.

Safe-range. By induction, rr(SRNF(φE))=free(φE)\mathrm{rr}(\mathrm{SRNF}(\varphi_E)) = \mathrm{free}(\varphi_E), with the rules of domain-independent query. An atom restricts its variables, xA=cx_A = c restricts xAx_A, and φE∧xA=xB\varphi_E \land x_A = x_B adds both variables because xAx_A is already restricted. In ∃yˉ φE\exists \bar y\, \varphi_E each yy is restricted in φE\varphi_E, so the rule for ∃\exists succeeds. In a union both sides have the same free variables, all restricted, so the intersection keeps them all. For φE1∧¬φE2\varphi_{E_1} \land \neg \varphi_{E_2}, the SRNF only moves the negation in front of atoms and of ∃\exists subformulas of φE2\varphi_{E_2}, which already pass the rule for ∃\exists; so no ⊥\bot appears, and the conjunction keeps rr(φE1)\mathrm{rr}(\varphi_{E_1}), 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 φ\varphi be in SRNF with rr(φ)=free(φ)\mathrm{rr}(\varphi) = \mathrm{free}(\varphi). Two claims hold for each subformula ψ\psi and each pair d,d′⊇adom\mathbf{d}, \mathbf{d}' \supseteq \mathrm{adom}.

Claim A. If ν\nu maps free(ψ)\mathrm{free}(\psi) into d\mathbf{d} and I⊨dψ[ν]I \models_{\mathbf{d}} \psi[\nu], then ν(x)∈adom\nu(x) \in \mathrm{adom} for each x∈rr(ψ)x \in \mathrm{rr}(\psi). Induction: an atom of II holds only on values of II; x=cx = c forces ν(x)=c∈adom\nu(x) = c \in \mathrm{adom}; in ψ∧x=y\psi \land x = y the equality copies a value in adom\mathrm{adom} 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 rr\mathrm{rr} keeps only the variables that both sides restrict; rr(¬ψ)=∅\mathrm{rr}(\neg \psi) = \emptyset claims nothing; for ∃x ψ\exists x\, \psi, take the witness value aa and apply the hypothesis to ν\nu extended by x↦ax \mapsto a.

Claim B. If ν\nu maps free(ψ)\mathrm{free}(\psi) into d∩d′\mathbf{d} \cap \mathbf{d}', then I⊨dψ[ν]I \models_{\mathbf{d}} \psi[\nu] if and only if I⊨d′ψ[ν]I \models_{\mathbf{d}'} \psi[\nu]. Induction: atoms and equalities do not look at the domain, and the connectives keep the property. For ∃x ψ\exists x\, \psi, suppose it is true in d\mathbf{d} with witness a∈da \in \mathbf{d}. The rule for ∃\exists succeeded, so x∈rr(ψ)x \in \mathrm{rr}(\psi), and Claim A gives a∈adom⊆d′a \in \mathrm{adom} \subseteq \mathbf{d}'. The extended valuation maps into d∩d′\mathbf{d} \cap \mathbf{d}', so by the hypothesis ψ\psi is true in d′\mathbf{d}' and aa is a witness there. The other direction is the same.

Now let t∈Qd(I)t \in Q_{\mathbf{d}}(I), given by ν\nu. All free variables are in rr(φ)\mathrm{rr}(\varphi), so by Claim A, ν\nu maps into adom⊆d∩d′\mathrm{adom} \subseteq \mathbf{d} \cap \mathbf{d}', and by Claim B, t∈Qd′(I)t \in Q_{\mathbf{d}'}(I). By symmetry Qd(I)=Qd′(I)Q_{\mathbf{d}}(I) = Q_{\mathbf{d}'}(I).

(2) to (4). A domain-independent QQ has Q(I)=Qadom(I)Q(I) = Q_{\mathrm{adom}}(I), 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 DD:

ADOM=⋃R⋃A∈att(R)ρA→D(πA(R))  ∪  ⋃c∈Q{⟨D:c⟩}.\mathrm{ADOM} = \bigcup_{R} \bigcup_{A \in \mathrm{att}(R)} \rho_{A \to D}(\pi_A(R)) \;\cup\; \bigcup_{c \in Q} \{\langle D : c \rangle\}.

ADOM(I)=adom\mathrm{ADOM}(I) = \mathrm{adom}. Write ADOMx\mathrm{ADOM}_x for ρD→x(ADOM)\rho_{D \to x}(\mathrm{ADOM}). Replace ∀x ψ\forall x\, \psi by ¬∃x ¬ψ\neg \exists x\, \neg \psi and ψ→χ\psi \rightarrow \chi by ¬ψ∨χ\neg \psi \lor \chi. For each subformula ψ\psi with free variables YY, build EψE_\psi with one attribute per variable of YY, such that

Eψ(I)={ ν∣ν:Y→adom, I⊨adomψ[ν] }.E_\psi(I) = \{\, \nu \mid \nu : Y \to \mathrm{adom},\ I \models_{\mathrm{adom}} \psi[\nu] \,\}.
  • R(e1,…,ek)R(e_1, \ldots, e_k): apply σAi=c\sigma_{A_i = c} to each position ii that holds a constant cc and σAi=Aj\sigma_{A_i = A_j} to each two positions with the same variable, then project on one position per variable and rename it to the variable.
  • x=cx = c: {⟨x:c⟩}\{\langle x : c \rangle\}. x=yx = y: σx=y(ADOMx×ADOMy)\sigma_{x = y}(\mathrm{ADOM}_x \times \mathrm{ADOM}_y). x=xx = x: ADOMx\mathrm{ADOM}_x.
  • ψ∧χ\psi \land \chi: Eψ⋈EχE_\psi \bowtie E_\chi.
  • ψ∨χ\psi \lor \chi: extend each side to the variables Yψ∪YχY_\psi \cup Y_\chi by a product with ADOMy\mathrm{ADOM}_y for each missing yy, then take the union.
  • ¬ψ\neg \psi: (ADOMy1×⋯×ADOMym)−Eψ(\mathrm{ADOM}_{y_1} \times \cdots \times \mathrm{ADOM}_{y_m}) - E_\psi for Y={y1,…,ym}Y = \{y_1, \ldots, y_m\}, with {()}\{()\} for m=0m = 0.
  • ∃x ψ\exists x\, \psi: πY∖{x}(Eψ)\pi_{Y \setminus \{x\}}(E_\psi) if xx is free in ψ\psi, and Eψ×π∅(ADOMx)E_\psi \times \pi_{\emptyset}(\mathrm{ADOM}_x) if not; the factor is {()}\{()\} if the active domain is not empty and ∅\emptyset if it is, as the semantics of ∃\exists requires.

Each case follows from the definition of truth with the variables ranging over adom\mathrm{adom}: the join matches valuations on the shared variables, the padding and the complement are taken inside adom\mathrm{adom}, and the projection is the existential quantifier over adom\mathrm{adom}. For the query {(x1,…,xk)∣φ}\{(x_1, \ldots, x_k) \mid \varphi\}, the expression EφE_\varphi 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 Takes(student,course)\mathrm{Takes}(\mathit{student}, \mathit{course}) with the tuples (ann,db)(\mathit{ann}, \mathit{db}), (ann,os)(\mathit{ann}, \mathit{os}), (bob,db)(\mathit{bob}, \mathit{db}), (cal,os)(\mathit{cal}, \mathit{os}). The safe-range query {(s)∣∃c Takes(s,c)∧¬Takes(s,db)}\{(s) \mid \exists c\, \mathrm{Takes}(s, c) \land \neg \mathrm{Takes}(s, \mathit{db})\} has the algebra form ρstudent→s(πstudent(Takes)−πstudent(σcourse=db(Takes)))\rho_{\mathit{student} \to s}(\pi_{\mathit{student}}(\mathrm{Takes}) - \pi_{\mathit{student}}(\sigma_{\mathit{course} = \mathit{db}}(\mathrm{Takes}))), and both give {cal}\{\mathit{cal}\}. The formula ¬Takes(s,db)\neg \mathrm{Takes}(s, \mathit{db}) alone is not domain independent. Step (4) to (1) turns it into ADOMs−ρstudent→s(πstudent(σcourse=db(Takes)))\mathrm{ADOM}_s - \rho_{\mathit{student} \to s}(\pi_{\mathit{student}}(\sigma_{\mathit{course} = \mathit{db}}(\mathrm{Takes}))), whose answer is {cal,db,os}\{\mathit{cal}, \mathit{db}, \mathit{os}\}: 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 ADOM\mathrm{ADOM}, 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 ∃\exists and ∧\land over atoms both express the conjunctive queries, and adding union on one side and ∨\lor on the other gives the unions of conjunctive queries.

Tightness

  • Domain-dependent queries have no algebra form. Under natural semantics, {(s)∣¬Takes(s,db)}\{(s) \mid \neg \mathrm{Takes}(s, \mathit{db})\} 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: πA(R)\pi_A(R) 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

  1. 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
  2. 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)
  3. 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)
  4. 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
  5. 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
  6. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/TP5RJX/text.yaml