# 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

`\theta`

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