Relational Calculus

Relational Query Languages

A query language in which a user requests information from the database.

Query languages:

  • imperative
  • functional
  • declarative

Pure languages:

  • relational algebra (functional)
  • tuple relational calculus (declarative)
  • domain relational calculus (declarative)

Tuple Relational Calculus

Each query is of the form tP(t){t | P(t)}. It is the set of all tuples tt such that predicate PP is true for tt. tt is a tuple variable. t[A]t[A] denotes the value of tuple tt on attribute AA. trt \in r denotes that tuple tt is in relation rr. PP is a formula similar to that of the predicate calculus.

Predicate Calculus formula

  • Set of attributes and constants
  • Set of comparison operators
  • set of connectives
  • Implication, if-then
  • Set of quantifiers, (there exists, for all)

Safety of Expressions

It is possible to write tuple calculus expressions that generate infinite relations. For example, t¬tr{t | \neg t \in r} results in an infinite relation if the domain of any attribute of relation rr is infinite. To guard against the problem, we restrict the set of allowable expressions to safe expressions. An expression {t|P(t)} in the tuple relational calculus is safe if every component of tt appears in one of the relations, tuples or constants that appear in PP.

The expression <x1,x2,...,xn>P(x1,x2,...,xn){ < x_1, x_2, ..., x_n > | P (x_1, x_2, ..., x_n)} is safe if all of the following hold:

  1. All values that appear in tuples of the expression are values from dom(PP)
  2. For every "there exists" sub formula of the form x(P1(x))\exists x (P_1 (x)), the subformula is true if and only if there is a value of xx in dom(P1P_1) such that P1(x)P_1(x) is true.
  3. For every "for all" subformula of the form x(P1(x))\forall_x (P_1 (x)), the subformula is true if and only if P1(x)P_1(x) is true for all values xx from dom(P1P_1)

Domain Relational Calculus

  • a nonprocedural query language equivalent in power to the tuple relational calculus
  • Each query in an expression of the form: <x1,x2,...,xn>P(x1,x2,...,xn){ < x_1, x_2, ..., x_n > | P (x_1, x_2, ..., x_n)}
    • xix_i represents domain variables
    • PP represents a formula similar to that of the predicate calculus