# Relational Calculus (RC)

Below are mainly confusing because of notation. In practice, just look at the examples, and it will be a lot clearer. However, it’s still good to know theory, see Theory vs. Practice.

#### Query Conditions

The idea of query **conditions** is to use **variables** and **valuations** to generalize conditions.

- For example:
`AUTHOR(x, y)`

will be true of any valuation ${x↦v_{1},y↦v_{2},...}$ exactly when the 2-tuple of values $(v_{1},v_{2})$ occurs in`AUTHOR`

.

Valuation $θ$

A valuation is a function $θ$ that maps variable names to values in the universe: $θ:{x_{1},x_{2},...}→D$ To denote a modification to $θ$ in which variable $x$ is instead mapped to value $v$, one writes: $θ[x↦v]$

These allow more complex conditions to be built from simpler conditions with logical connectives ($∧,∨,¬$) and quantifiers ($∃,∀$).

#### Queries

Relational Calculus (RC) Query

A query in the relational calculus is a set comprehension of the form ${(x_{1},...,x_{k})∣φ},$ where ${x_{1},...,x_{k}}=Fv(φ)$ (are the free variables of $φ$). Also:

- a
conjunctive queryis where $φ$ is a conjunctive formula, and- a
positive queryis where $φ$ is a positive formula.

Query Answers

The answers to a query ${(x_{1},...,x_{k})∣φ}$ over $DB$ is the relation ${(θ(x_{1}),...,θ(x_{k}))∣DB,θ⊨φ}$

In other words, answers to queries are valuations applied to tuples of variables that make the formula true with respect to a database.

Some basic examples

### Safety

Integrity Constraints aren’t enough for a RDBMS. We also need to have following properties:

- The extension of any relation in a signature should be finite
- Queries should be safe: their answers should be finite when database instances are finite

Look at the following queries, which would have infinite answers:

Domain Independence

An RC query ${(x_{1},...,x_{k})∣φ}$ is domain independent when, for any pair of instances $DB_{1}=(D_{1},≈,R_{1},...,R_{k})$ and $DB_{2}=(D_{2},≈,R_{1},...,R_{k})$ and any $θ,DB_{1},θ⊨φ$ if and only if $DB_{2},θ⊨φ$

Theorem

Let $(R_{1},...,R_{k})$ be the signature of a relational database. Answers to domain independent queries contain only values that occur in the extension Ri of any relation $R_{i}$.

safety ⇔ domain independence and finite database instances

Range-Restricted RC