Queries and Query Languages: Passive. That Is, They Do Not Modify The
Queries and Query Languages: Passive. That Is, They Do Not Modify The
20061031:slides5: 1 of 27
Query Languages to be Covered in the
Course
20061031:slides5: 2 of 27
The Relational Calculus
20061031:slides5: 3 of 27
The Relational Algebra
20061031:slides5: 4 of 27
The Select Operation:
<cond>(<rel_arg>)
Here:
is the symbol used to denote a select
operation.
<cond> is the condition of the selection.
<rel_arg> is the relation to which the selection
is applied.
R
A B C
a1 b1 c1
a1 b2 c2
a1 b3 c3
a2 b4 c4
20061031:slides5: 5 of 27
The same notation may be used to represent the
query on a schema. For example,
σ A=a R
1
S
A B C
1 b1 c1
3 b2 c2
3 b3 c3
4 b4 c4
A B C
3 b2 c2
3 b3 c3
4 b4 c4
20061031:slides5: 6 of 27
Disjunctive formulas are also possible. Construct
the answer to the following query.
σ A= 1∨ A= 4 s
20061031:slides5: 7 of 27
The Project Operation:
R
A B C
a1 b1 c1
a1 b2 c1
a2 b3 c3
a2 b4 c3
A C
a1 c1
a2 c3
20061031:slides5: 8 of 27
Join:
The join operation is much more complex than the
previous two. We begin with the simplest, yet most
widely used version.
Natural Join:
It is easiest to explain with an example. Let r and
s be the instances associated with the following
database.
R S
A B B C
a1 b1 b1 c1
a2 b1 b1 c2
a2 b3 b3 c3
a3 b3 b4 c3
T
A B C
a1 b1 c1
a1 b1 c2
a2 b1 c1
a2 b1 c2
a2 b3 c3
a3 b3 c3
20061031:slides5: 9 of 27
The idea is to match the tables on their common
attributes.
A join is said to be lossless if the original
relations may be recovered from the appropriate
projections on the join. In this example, this
means that
AB(rs) = r and BC(rs) = s.
R S
A B B C
a1 b1 b1 c1
a2 b1 b1 c2
a2 b3 b3 c3
a3 b3
20061031:slides5: 10 of 27
Other Forms of Join:
General equality join: Even if attribute names do
not match, a join may be constructed by
specifying which columns to match. In this case,
the duplicate columns are often retained.
Example: R {B,D}S
R S
A B D C
a1 b1 b1 c1
a2 b1 b1 c2
a2 b3 b3 c3
a3 b3
Result:
T
A B C D
a1 b1 c1 b1
a1 b1 c2 b1
a2 b1 c1 b1
a2 b1 c2 b1
a2 b3 c3 b3
a3 b3 c3 b3
20061031:slides5: 11 of 27
Cartesian product: In this context, the join on an
empty set of columns (i.e., R S) yields the
Cartesian product of the two relations.
20061031:slides5: 12 of 27
Join Conventions and Notation
20061031:slides5: 13 of 27
Conventions and notation for other types of join:
20061031:slides5: 14 of 27
Other Relational Algebra Operators:
In addition to the “SPJ” trio, it is often the case
that set-based operations are allowed in the
relational algebra. The most common operations
are the following:
Union ()
Set difference ()
Intersection (), which can in fact be
constructed from union and difference.
Note that, for queries to make sense, these set
operations may only be performed on relations over
identical attribute sets, or at least over relations
with a bijective correspondence between the
domains of their attribute sets.
Format of queries:
Officially, the correct format is to write queries in
functional composition format.
In a less elegant representation intermediate
variables are introduced and queries are written
in an imperative program style.
Examples will be given later.
20061031:slides5: 15 of 27
The Relational Calculus
20061031:slides5: 16 of 27
The Domain Calculus
20061031:slides5: 17 of 27
Example: Consider the problem of computing the
AC-projection of the join of the following two
relational instances (i.e., AC(RS)):
R S
A B B C
a1 b1 b1 c1
a2 b1 b1 c2
a2 b3 b3 c3
a3 b3 b4 c3
{(xA,xC) | (xB)(R(xA,xB)S(xB,xC))}
20061031:slides5: 18 of 27
Equality and even arithmetic comparisons may also
be used in a limited fashion within the logic. Here
is an example of a selection query revisited:
S
A B C
1 b1 c1
3 b2 c2
3 b3 c3
4 b4 c4
A B C
3 b2 c2
3 b3 c3
4 b4 c4
20061031:slides5: 19 of 27
These queries are safe, in the sense that the
answer only involves values extracted from the
relations in the database. In formulating a
precise notion of safe queries, we must be
careful to ensure this condition. An example of
an unsafe query is:
{ xA | (xB)(R(xA,xB))}
20061031:slides5: 20 of 27
The Tuple Calculus
The tuple calculus differs from the domain
calculus in that the variables range over entire
tuples from relations, rather than over single
domain values.
The idea is best illustrated by example: Suppose
that we have the following simple relational
database schema.
R S
A B B C
20061031:slides5: 21 of 27
Division – a Special Operator in the
Relational Algebra
Employee Project
Emp_Num Name Emp_Num Proj_Num
{x.Name | Employee(x)
(y)(z)(Project(y) (Project(z)
(y.Proj_Num = z.Proj_Num)
(x.Emp_Num = z.Emp_Num)))}
20061031:slides5: 22 of 27
At first glance it seems impossible within the
relational algebra. However, there is an operation,
called division, which makes it possible.
R S = A\B(R) \ A\B((A\B(R ) S) \ R)
r s = { t[A\B] |
( tS s)( tR r)(( tR[B] = tS) (tR[A\B] = t))}
20061031:slides5: 23 of 27
Renaming – Another Useful Operator in
the Relational Algebra
Employee Project
Emp_Num Name Emp_Num Proj_Num
{x.Name | Employee(x)
(y)(Project(y) (x.Emp_Num = y.Emp_Num))
(z)(w)((Project(z) Project(w)
(x.Emp_Num = z.Emp_Num)
(x.Emp_Num = w.Emp_Num))
(z.Proj_Num = w.Proj_Num))}
20061031:slides5: 24 of 27
Again, at first glance it seems impossible with the
relational algebra. However, it is easily realized
with the aid of renaming. In renaming, a copy of a
relation schema is made, with the name of one or
more attributes changed.
Copy-with-Rename(
Project, Project',{(Proj_Num, Proj_Num')})
creates the following relation schema:
Project'
Emp_Num Proj_Num'
R1 Project Project'.
R1
Emp_Num Proj_Num Proj_Num'
20061031:slides5: 25 of 27
Now, restrict this relation to those tuples with
different values for Proj_Num and Proj_Num':
R2 (Proj_Num Proj_Num')(R1)
R3 Emp_Num(R2)
R4 Emp_Num(Employees) Emp_Num(Project).
R6 R5 Employee.
Finally, project the names:
R7 Name(R6).
20061031:slides5: 26 of 27
As a single expression in the relational algebra, this
becomes:
Name(
(Emp_Num(Employee)
Emp_Num(
(Proj_Num Proj_Num')(Project Project'))
(Emp_Num(Employee) Emp_Num(Project))
Employee)
20061031:slides5: 27 of 27