Introduction to Relational Calculus
by K. Yue
1. Introduction
-
Non-procedural, declarative, and high level.
-
Two kinds:
- Domain Relational Calculus
(DRC)
- Tuple Relational Calculus (TRC)
- Queries specified by the set builder form: {s | cond(s) }
- cond(s) is known as a formula.
- Constructs:
- Variables:
- TRC: tuples (bound to tuples): e.g. s, t, student, class, etc.
- DRC: attributes (bound to domain value): e.g. a, b, c, stuId, firstName, etc.
- The variables are sometime known as 'dummy variables.' They can assume any names.
- Constants: string, int, etc. E.g. 12, 'csci', 3.7.
- Comparison operators: <, <, =, etc.
- Boolean operators: and (conjunction, ∧ or just ,), or (disjunction ∨), not (¬), implies (⇒), etc.
- Membership functions: belongs to, ∈, not belongs to, ∉, etc.
- Quantifiers: there exists (existential, ∃), not exists (∄), for all (universal ∀).
- An atom can be thought of as a simple Boolean expression: x op y, where x and y are attributes or constants, and op is a comparison operation.
- Example: R.a>=2, sname='BBC', t ∉ T, etc.
- A formula is either an atom or formulas connected by Boolean operators or qualifiers.
- Example: ∃e(e ∈ R), a=1 ∨ b=2, ∃a,b,c((a,b,c) ∈ R) or simply (a,b,c) ∈ R
- A formula that is not an atom can be thought of a compound Boolean expression.
- A variable is bound if it has a finite number of values., such as appearing in qualifier expressions (without negation). A free variable is not bound.
- Relational Calculus expressions need to be safe: results should be a finite set of tuples.
- Care should be taken especially for the negation operation. E.g. {s |¬ (s ∈ Student) } is unsafe.
- For a given implementation of relational calculus:
- There may be restrictions of supported constructs.
- There may be certain canonical requirements: e.g. conjunction of disjunction.
- Relational Calculus and Relational Algebra:
- All RA expressions can be expressed in RC.
- RA and RC have the same expressive power.
- Any query language that can express all RA queries is known to be relational complete.
Example:
{i | i ∈ I ∧ i % 2 =0}
{i | i ∈ I, i % 2 =0} -- set builder form.
{t | ∃r ∈R, r.firstname = t.firstname, r.lastname = t.lastname}
- t is a free variable.
- It will have two attributes: t.firstname and t.lastname.
Alternatively, we can use the set builder form in the LHS before |:
{(r.firstname, r.lastname) | r ∈ R}
R(A,B,C,D) / S(C,D)
{(a,b) | ∀(c,d) ∈ S((a,b,c,d) ∈ R))}
- In this class, with the query is complicated, you may use intermediate relations to construct a sequence of RC expressions to provide the result.
Exercises:
How do you use RC to implement RA operations?
2. TRC
- The variables in TORC are tuples.
- SQL is based on TRC.
Exercise:
Work on some of the query questions listed in the Supplies database example in TRC.
3. DRC
- The variables in DRC are attributes (domain values).
- May use anonymous variable: _ as a placeholder.
- Query By Example (QBE) is based on DRC.
Exercise:
Work on some of the query questions listed in the Supplies database example in DRC.