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 . It is the set of all tuples such that predicate is true for . is a tuple variable. denotes the value of tuple on attribute . denotes that tuple is in relation . 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, results in an infinite relation if the domain of any attribute of relation 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 appears in one of the relations, tuples or constants that appear in .
The expression is safe if all of the following hold:
- All values that appear in tuples of the expression are values from dom()
- For every "there exists" sub formula of the form , the subformula is true if and only if there is a value of in dom() such that is true.
- For every "for all" subformula of the form , the subformula is true if and only if is true for all values from dom()
Domain Relational Calculus
- a nonprocedural query language equivalent in power to the tuple relational calculus
- Each query in an expression of the form:
- represents domain variables
- represents a formula similar to that of the predicate calculus