-1

Let's have a relation $R = (name, surname, age)$. I want to obtain a new relation with only the $name$ attribute. In relational algebra I would simply do $\Pi_{\mathrm{name}}(R)$ but in relational calculus the general way of doing that is $$ \newcommand{\Set}[2]{% \{\, #1 \mid #2 \, \}% } \Set{t}{\exists z \; (R(z) \land t.\mathrm{name} = z.\mathrm{name}}. $$

How does it work? I thought that relation is basically a table and tuple is a row from that table. How does this expression retrieve the original relation with only the name as an attribute?

If I don't specify where the tuple variable $t$ belongs to, we're ranging over all tuples $t$ from the schema right?

  1. How can a tuple be just a slice of a table?

  2. Take a tuple from $R$ that contains $name$, $surname$ and $age$. It also follows the rule that $t.\mathrm{name} = z.\mathrm{name}$ doesn't it? How come it doesn't show up in the result? I'm so confused.

tomashauser
  • 127
  • 4

2 Answers2

0

Note: in the following I assume that a tuple has a fixed set of values, and its order is significant: in other words, if R(a, b) has a tuple t1 = (2, 4), then t1.a = 2 and t1.b = 4.

The basic idea behind the tuple relational calculus is that each relation represents a certain predicate, and a relation extension (the tuples) represents a model (i.e. a set of objects) that makes true such predicate. So for instance if R in your example has only three tuples,

("John", "Rean", 19)
("Mary", "Rean", 24)
("Mary", "June", 18)

this means that it is true that John Rean, Mary Rean, and Mary June satisfy the predicate R, and only them.

A query is expressed as a (well-formed) first order formula, and its “result” is obtained by finding in the model the objects that, substituted to the free variables of the formula, make it true.

So the query:

{t | P(t) }

means: the set of tuples t that satisfy the predicate P (remember that we want always, as result, a set of tuples, that is a relation).

So, in your example, the formula:

{ ∣ ∃(() ∧ .name = .name}

means: “all the tuples t for which exists at least a tuple true for R with the same name of t”; in other words, all the tuples that have a name among the names of the tuples model of R. So, note that if we have more than one tuple with the same name, in the result we will have only a tuple with such name.

Remember that we are looking for all the possible tuples that can be retrieved by the model that makes the predicate true: this means that we are looking for all the tuples such that:

∃(() ∧ .name = .name)

is true. And so there are two and only two tuples that satisfy this predicate:

("John")
("Mary")

so this is the result of our query (note that we assume that a tuple returned has only the "fields" mentioned in its predicate, so t has only the “field” name).

Renzo
  • 829
  • 6
  • 10
-1

Equation for projection:

${ t ∣ ∃z ( R(z) ∧ t.name = z.name }$

Now, here we don't specify anywhere that $R(t)$ or $t \ belongs \ to \ relation \ R$. So, $t$ is basically just empty tuple with no specified attributes. The only thing we specified here is that $R(z)$ which makes $z$ a tuple with all other attributes.

And other way around we say that $z.name = t.name$, which makes $t$ a new tuple with only $name$ attribute. And that $name$ only is the output with $t$.

In simple words, $z$ has all attributes, but we gave $t$ only $name$ attribute ($t$ was basically empty before). And that's what we want to project here.