Algorithm Search⌘K

Definitioncore

Relational calculus

Also called tuple relational calculus, domain relational calculus.

First-order logic over relation names, in tuple or domain form, used as the declarative query language of the relational model. Its answers depend on the domain unless the query is domain independent.

The relational calculus is first-order logic used as a query language: a query is a formula over the relation names of a schema, and its answer is the set of tuples of values that make the formula true in the database. Codd proposed it in 1972, in a tuple form whose variables range over whole tuples, as the declarative standard for relational algebra; the domain form, whose variables range over single values, has the same power. A formula can mention values that are not in the database, for example with a negation, so the definition must say over which domain the variables range. The queries whose answer does not depend on that choice are the domain-independent queries, and Codd's theorem says that they are exactly the queries of relational algebra.

Definition

The notation is that of Abiteboul, Hull and Vianu (AHV), named perspective, under set semantics. A schema is a finite set of relation names R,S,…R, S, \ldots, each with a finite set of attributes att(R)\mathrm{att}(R) of size arity(R)\mathrm{arity}(R). dom is a fixed countably infinite set of constants. An instance II gives each RR a finite relation I(R)I(R). The active domain adom(I)\mathrm{adom}(I) is the set of constants that occur in II, and adom(Q,I)\mathrm{adom}(Q, I) adds the constants that occur in the query QQ.

Domain calculus. Terms are variables x,y,…x, y, \ldots and constants. Formulas are built by these rules:

  • R(e1,…,ek)R(e_1, \ldots, e_k) for a relation name RR of arity kk and terms eie_i is an atom; e=e′e = e' is an atom;
  • if φ\varphi and ψ\psi are formulas, then φ∧ψ\varphi \land \psi, φ∨ψ\varphi \lor \psi, ¬φ\neg \varphi and φ→ψ\varphi \rightarrow \psi are formulas;
  • if φ\varphi is a formula and xx a variable, then ∃x φ\exists x\, \varphi and ∀x φ\forall x\, \varphi are formulas.

The free variables free(φ)\mathrm{free}(\varphi) are defined as usual. A query is an expression

Q={ (x1,…,xk)∣φ },Q = \{\, (x_1, \ldots, x_k) \mid \varphi \,\},

where x1,…,xkx_1, \ldots, x_k are distinct variables and free(φ)={x1,…,xk}\mathrm{free}(\varphi) = \{x_1, \ldots, x_k\}. A query with k=0k = 0 is Boolean: its answer is {()}\{()\} (true) or ∅\emptyset (false).

Semantics relative to a domain. Let d\mathbf{d} be a set of constants with adom(Q,I)⊆d⊆dom\mathrm{adom}(Q, I) \subseteq \mathbf{d} \subseteq \mathbf{dom}. A valuation ν\nu maps variables to d\mathbf{d} and is the identity on constants. Truth I⊨dφ[ν]I \models_{\mathbf{d}} \varphi[\nu] is defined by induction: R(e1,…,ek)R(e_1, \ldots, e_k) holds when (ν(e1),…,ν(ek))∈I(R)(\nu(e_1), \ldots, \nu(e_k)) \in I(R), e=e′e = e' holds when ν(e)=ν(e′)\nu(e) = \nu(e'), the connectives have their Boolean meaning, and ∃x φ\exists x\, \varphi (or ∀x φ\forall x\, \varphi) holds when φ\varphi holds for some (or every) value of xx in d\mathbf{d}. The answer is

Qd(I)={ (ν(x1),…,ν(xk))∣ν:free(φ)→d, I⊨dφ[ν] }.Q_{\mathbf{d}}(I) = \{\, (\nu(x_1), \ldots, \nu(x_k)) \mid \nu : \mathrm{free}(\varphi) \to \mathbf{d},\ I \models_{\mathbf{d}} \varphi[\nu] \,\}.

Three readings of a query are in use, and they are not the same:

  • natural semantics: d=dom\mathbf{d} = \mathbf{dom}. The answer can be infinite.
  • active domain semantics: d=adom(Q,I)\mathbf{d} = \mathrm{adom}(Q, I). The answer is always finite, and it can be computed by brute force over the finite domain.
  • domain independent queries: Qd(I)=Qd′(I)Q_{\mathbf{d}}(I) = Q_{\mathbf{d}'}(I) for every instance II and all d,d′\mathbf{d}, \mathbf{d}' that contain adom(Q,I)\mathrm{adom}(Q, I). For such a query the two semantics above agree, and one writes Q(I)Q(I). See domain-independent query for this class and its decidable subclass, the safe-range queries.

Tuple calculus. A tuple variable tt has a sort, a set of attributes. Atoms are R(t)R(t) with sort(t)=att(R)\mathrm{sort}(t) = \mathrm{att}(R), and t.A=s.Bt.A = s.B or t.A=ct.A = c for attributes in the sorts and a constant cc. Connectives and quantifiers are as above, with quantifiers over tuples of the given sort. A query is { t:X∣φ }\{\, t : X \mid \varphi \,\}, where tt is the only free variable and XX is its sort. A tuple variable of sort {A1,…,Ak}\{A_1, \ldots, A_k\} is the same as kk domain variables, one per attribute, so each tuple calculus query translates into a domain calculus query with the same answer under each of the three readings, and back. The two forms differ only in notation.

Codd's own calculus of 1972 is a tuple calculus in which every tuple variable is bound to a relation by a range term, such as "tt in RR". That restriction makes every query of his calculus domain independent. The free form above is the one of the textbooks, and AHV use it.

Examples and non-examples

Take the schema Takes(student,course)\mathrm{Takes}(\mathit{student}, \mathit{course}) and Course(course)\mathrm{Course}(\mathit{course}) and the instance

I(Takes)={(ann,db),(ann,os),(bob,db),(cal,os)},I(Course)={db,os}.I(\mathrm{Takes}) = \{(\mathit{ann}, \mathit{db}), (\mathit{ann}, \mathit{os}), (\mathit{bob}, \mathit{db}), (\mathit{cal}, \mathit{os})\}, \qquad I(\mathrm{Course}) = \{\mathit{db}, \mathit{os}\}.

So adom(I)={ann,bob,cal,db,os}\mathrm{adom}(I) = \{\mathit{ann}, \mathit{bob}, \mathit{cal}, \mathit{db}, \mathit{os}\}, five constants.

  • A selection. {(s)∣Takes(s,db)}\{(s) \mid \mathrm{Takes}(s, \mathit{db})\} returns {ann,bob}\{\mathit{ann}, \mathit{bob}\} on every domain. In algebra it is πstudent(σcourse=db(Takes))\pi_{\mathit{student}}(\sigma_{\mathit{course} = \mathit{db}}(\mathrm{Takes})).
  • The same query in tuple form. { t:{student}∣∃u (Takes(u)∧u.course=db∧t.student=u.student) }\{\, t : \{\mathit{student}\} \mid \exists u\, (\mathrm{Takes}(u) \land u.\mathit{course} = \mathit{db} \land t.\mathit{student} = u.\mathit{student}) \,\}.
  • A universal query. The students who take every course:
{ (s)∣∃c Takes(s,c)∧∀c (Course(c)→Takes(s,c)) }.\{\, (s) \mid \exists c\, \mathrm{Takes}(s, c) \land \forall c\, (\mathrm{Course}(c) \rightarrow \mathrm{Takes}(s, c)) \,\}.

The answer is {ann}\{\mathit{ann}\}: bob misses os and cal misses db. The first conjunct ties ss to the database, and the implication restricts the universal quantifier to the courses, so the answer is the same on every domain. In algebra it is the division Takes÷Course\mathrm{Takes} \div \mathrm{Course}, which is πstudent(Takes)−πstudent((πstudent(Takes)×Course)−Takes)\pi_{\mathit{student}}(\mathrm{Takes}) - \pi_{\mathit{student}}((\pi_{\mathit{student}}(\mathrm{Takes}) \times \mathrm{Course}) - \mathrm{Takes}), where −- is set difference. That expression also gives {ann}\{\mathit{ann}\}.

  • Non-example: an unrestricted negation. {(s)∣¬Takes(s,db)}\{(s) \mid \neg \mathrm{Takes}(s, \mathit{db})\}. Under natural semantics the answer is infinite: every constant of dom except ann and bob. Under active domain semantics it is {cal,db,os}\{\mathit{cal}, \mathit{db}, \mathit{os}\}, which contains the two course names, because nothing in the formula says that ss is a student. The query is not domain independent. The intended query is {(s)∣∃c Takes(s,c)∧¬Takes(s,db)}\{(s) \mid \exists c\, \mathrm{Takes}(s, c) \land \neg \mathrm{Takes}(s, \mathit{db})\}, with answer {cal}\{\mathit{cal}\} on every domain.
  • Non-example: a disjunction with different free variables. {(x,y)∣Takes(x,db)∨Course(y)}\{(x, y) \mid \mathrm{Takes}(x, \mathit{db}) \lor \mathrm{Course}(y)\}. For a domain d\mathbf{d} of size nn that contains the five constants, the answer has 2n+2n−4=4n−42n + 2n - 4 = 4n - 4 pairs: 16 pairs for n=5n = 5 and 20 pairs for n=6n = 6. The answer grows with the domain.
  • Non-example: a bare universal quantifier. {(s)∣∀c Takes(s,c)}\{(s) \mid \forall c\, \mathrm{Takes}(s, c)\}. On the instance JJ with J(Takes)={(a,a)}J(\mathrm{Takes}) = \{(a, a)\}, the active domain is {a}\{a\} and the answer is {a}\{a\}. Add one more constant bb to the domain and the answer is empty, since (a,b)∉J(Takes)(a, b) \notin J(\mathrm{Takes}).
  • Relational algebra. Relational algebra is the procedural language of the model. By Codd's theorem, the algebra, the domain-independent calculus, the safe-range calculus and the calculus under active domain semantics express the same queries.
  • Domain independence and safety. Domain independence is a semantic property, and it is undecidable. The safe-range formulas are a syntactic class with the same power. See domain-independent query.
  • Fragments. The formulas with only ∃\exists and ∧\land over atoms are the conjunctive queries, the select-project-join queries of the algebra. Adding ∨\lor gives the positive existential queries, which are the unions of conjunctive queries. Containment is decidable for these fragments and undecidable for the full calculus (see query containment).
  • Recursion. The calculus cannot express the transitive closure of a binary relation (Aho and Ullman 1979). Datalog adds recursion to the positive fragment.
  • Complexity. A fixed calculus query is evaluated in polynomial time in the size of the instance, and the problem is much harder when the query is part of the input. See data complexity and combined complexity.
  • SQL. The WHERE clause of SQL with EXISTS and NOT EXISTS is a tuple calculus over bags. With nulls, SQL uses a three-valued logic instead of the two-valued logic above; see three-valued logic of SQL nulls.

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 tuple calculus with range-coupled quantifiers (Codd's calculus), and the definition of relational completeness
  2. Serge Abiteboul, Richard Hull and Victor Vianu, Foundations of Databases, Addison-Wesley (1995), Section 5.3 (the relational calculus, the natural and active domain semantics, and domain independence) and Section 5.4 (safe-range formulas)
  3. Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, third edition, McGraw-Hill (2003), Chapter 4 (relational algebra and calculus), the sections on the tuple and the domain relational calculus
  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 of a binary relation is not expressible in relational algebra, so not in the domain-independent calculus

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.

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/EYRQEM/text.yaml