Tuple relational calculus


Tuple calculus is a calculus that was created and introduced by Edgar F. Codd as part of the relational model, in order to provide a declarative database-query language for data manipulation in this data model. It formed the inspiration for the database-query languages QUEL and SQL, of which the latter, although far less faithful to the original relational model and calculus, is now the de facto standard database-query language; a dialect of SQL is used by nearly every relational-database-management system. Michel Lacroix and Alain Pirotte proposed domain calculus, which is closer to first-order logic and together with Codd showed that both of these calculi are equivalent in expressive power. Subsequently, query languages for the relational model were called relationally complete if they could express at least all of these queries.

Definition of the calculus

Relational database

Since the calculus is a query language for relational databases we first have to define a relational database. The basic relational building block is the domain. A tuple is a finite sequence of attributes, which are ordered pairs of domains and values. A relation is a set of tuples. Although these relational concepts are mathematically defined, those definitions map loosely to traditional database concepts. A table is an accepted visual representation of a relation; a tuple is similar to the concept of a row.
We first assume the existence of a set C of column names, examples of which are "name", "author", "address", etcetera. We define headers as finite subsets of C. A relational database schema is defined as a tuple S = where D is the domain of atomic values, R is a finite set of relation names, and
a function that associates a header with each relation name in R. Given a domain D we define a tuple over D as a partial function that maps some column names to an atomic value in D. An example would be.
The set of all tuples over D is denoted as TD. The subset of C for which a tuple t is defined is called the domain of t and denoted as dom.
Finally we define a relational database given a schema S = as a function
that maps the relation names in R to finite subsets of TD, such that for every relation name r in R and tuple t in db it holds that
The latter requirement simply says that all the tuples in a relation should contain the same column names, namely those defined for it in the schema.

Atoms

For the construction of the formulas we will assume an infinite set V of tuple variables. The formulas are defined given a database schema S = and a partial function type : V ⇸ 2C, called at type assignment, that assigns headers to some tuple variables. We then define the set of atomic formulas A with the following rules:
  1. if v and w in V, a in type and b in type then the formula v.a = w.b is in A,
  2. if v in V, a in type and k denotes a value in D then the formula v.a = k is in A, and
  3. if v in V, r in R and type = h then the formula r is in A.
Examples of atoms are:
The formal semantics of such atoms is defined given a database db over S and a tuple variable binding val : VTD that maps tuple variables to tuples over the domain in S:
  1. v.a = w.b is true if and only if val = val
  2. v.a = k is true if and only if val = k
  3. r is true if and only if val is in db

    Formulas

The atoms can be combined into formulas, as is usual in first-order logic, with the logical operators ∧, ∨ and ¬, and we can use the existential quantifier and the universal quantifier to bind the variables. We define the set of formulas F inductively with the following rules:
  1. every atom in A is also in F
  2. if f1 and f2 are in F then the formula f1f2 is also in F
  3. if f1 and f2 are in F then the formula f1f2 is also in F
  4. if f is in F then the formula ¬ f is also in F
  5. if v in V, H a header and f a formula in F] then the formula ∃ v : H is also in F, where type denotes the function that is equal to type except that it maps v to H,
  6. if v in V, H a header and f a formula in F] then the formula ∀ v : H is also in F
Examples of formulas:
Note that the last formula states that all books that are written by C. J. Date have as their subject the relational model. As usual we omit brackets if this causes no ambiguity about the semantics of the formula.
We will assume that the quantifiers quantify over the universe of all tuples over the domain in the schema. This leads to the following formal semantics for formulas given a database db over S and a tuple variable binding val : V -> TD:
  1. f1f2 is true if and only if f1 is true and f2 is true,
  2. f1f2 is true if and only if f1 is true or f2 is true or both are true,
  3. ¬ f is true if and only if f is not true,
  4. v : H is true if and only if there is a tuple t over D such that dom = H and the formula f is true for val, and
  5. v : H is true if and only if for all tuples t over D such that dom = H the formula f is true for val.

    Queries

Finally we define what a query expression looks like given a schema S = :
where v is a tuple variable, H a header and f a formula in F where type = and with v as its only free variable. The result of such a query for a given database db over S is the set of all tuples t over D with dom = H such that f is true for db and val =.
Examples of query expressions are:

Domain-independent queries

Because the semantics of the quantifiers is such that they quantify over all the tuples over the domain in the schema it can be that a query may return a different result for a certain database if another schema is presumed. For example, consider the two schemas S1 = and S2 = with domains D1 =, D2 =, relation names R = and headers h =. Both schemas have a common instance:
If we consider the following query expression
then its result on db is either under S1 or under S2. It will also be clear that if we take the domain to be an infinite set, then the result of the query will also be infinite. To solve these problems we will restrict our attention to those queries that are domain independent, i.e., the queries that return the same result for a database under all of its schemas.
An interesting property of these queries is that if we assume that the tuple variables range over tuples over the so-called active domain of the database, which is the subset of the domain that occurs in at least one tuple in the database or in the query expression, then the semantics of the query expressions does not change. In fact, in many definitions of the tuple calculus this is how the semantics of the quantifiers is defined, which makes all queries by definition domain independent.

Safe queries

In order to limit the query expressions such that they express only domain-independent queries a syntactical notion of safe query is usually introduced. To determine whether a query expression is safe we will derive two types of information from a query. The first is whether a variable-column pair t.a is bound to the column of a relation or a constant, and the second is whether two variable-column pairs are directly or indirectly equated.
For deriving boundedness we introduce the following reasoning rules:
  1. in " v.a = w.b " no variable-column pair is bound,
  2. in " v.a = k " the variable-column pair v.a is bound,
  3. in " r " all pairs v.a are bound for a in type,
  4. in " f1f2 " all pairs are bound that are bound either in f1 or in f2,
  5. in " f1f2 " all pairs are bound that are bound both in f1 and in f2,
  6. in " ¬ f " no pairs are bound,
  7. in " ∃ v : H " a pair w.a is bound if it is bound in f and w <> v, and
  8. in " ∀ v : H " a pair w.a is bound if it is bound in f and w <> v.
For deriving equatedness we introduce the following reasoning rules :
  1. in " v.a = w.b " it holds that v.a w.b,
  2. in " v.a = k " no pairs are equated,
  3. in " r " no pairs are equated,
  4. in " f1f2 " it holds that v.a w.b if it holds either in f1 or in f2,
  5. in " f1f2 " it holds that v.a w.b if it holds both in f1 and in f2,
  6. in " ¬ f " no pairs are equated,
  7. in " ∃ v : H " it holds that w.a x.b if it holds in f and w<>v and x<>v, and
  8. in " ∀ v : H " it holds that w.a x.b if it holds in f and w<>v and x<>v.
We then say that a query expression is safe if
The restriction to safe query expressions does not limit the expressiveness since all domain-independent queries that could be expressed can also be expressed by a safe query expression. This can be proven by showing that for a schema S =, a given set K of constants in the query expression, a tuple variable v and a header H we can construct a safe formula for every pair v.a with a in H that states that its value is in the active domain. For example, assume that K=, R= and h = then the corresponding safe formula for v.b is:
This formula, then, can be used to rewrite any unsafe query expression to an equivalent safe query expression by adding such a formula for every variable v and column name a in its type where it is used in the expression. Effectively this means that we let all variables range over the active domain, which, as was already explained, does not change the semantics if the expressed query is domain independent.

Systems