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 , each with a finite set of attributes of size . dom is a fixed countably infinite set of constants. An instance gives each a finite relation . The active domain is the set of constants that occur in , and adds the constants that occur in the query .
Domain calculus. Terms are variables and constants. Formulas are built by these rules:
- for a relation name of arity and terms is an atom; is an atom;
- if and are formulas, then , , and are formulas;
- if is a formula and a variable, then and are formulas.
The free variables are defined as usual. A query is an expression
where are distinct variables and . A query with is Boolean: its answer is (true) or (false).
Semantics relative to a domain. Let be a set of constants with . A valuation maps variables to and is the identity on constants. Truth is defined by induction: holds when , holds when , the connectives have their Boolean meaning, and (or ) holds when holds for some (or every) value of in . The answer is
Three readings of a query are in use, and they are not the same:
- natural semantics: . The answer can be infinite.
- active domain semantics: . The answer is always finite, and it can be computed by brute force over the finite domain.
- domain independent queries: for every instance and all that contain . For such a query the two semantics above agree, and one writes . See domain-independent query for this class and its decidable subclass, the safe-range queries.
Tuple calculus. A tuple variable has a sort, a set of attributes. Atoms are with , and or for attributes in the sorts and a constant . Connectives and quantifiers are as above, with quantifiers over tuples of the given sort. A query is , where is the only free variable and is its sort. A tuple variable of sort is the same as 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 " in ". 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 and and the instance
So , five constants.
- A selection. returns on every domain. In algebra it is .
- The same query in tuple form. .
- A universal query. The students who take every course:
The answer is : bob misses os and cal misses db. The first conjunct ties 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 , which is , where is set difference. That expression also gives .
- Non-example: an unrestricted negation. . Under natural semantics the answer is infinite: every constant of dom except ann and bob. Under active domain semantics it is , which contains the two course names, because nothing in the formula says that is a student. The query is not domain independent. The intended query is , with answer on every domain.
- Non-example: a disjunction with different free variables. . For a domain of size that contains the five constants, the answer has pairs: 16 pairs for and 20 pairs for . The answer grows with the domain.
- Non-example: a bare universal quantifier. . On the instance with , the active domain is and the answer is . Add one more constant to the domain and the answer is empty, since .
Related definitions
- 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 and over atoms are the conjunctive queries, the select-project-join queries of the algebra. Adding 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
- 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
- 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)
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag EYRQEM Edit this page History Report an error Markdown