Relational Algebra: Chapter 4, Part A
Relational Algebra: Chapter 4, Part A
Gehrke 1
Relational Algebra
Chapter 4, Part A
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2
Relational Query Languages
Query languages: Allow manipulation and retrieval
of data from a database.
Relational model supports simple, powerful QLs:
Strong formal foundation based on logic.
Allows for much optimization.
Query Languages != programming languages!
QLs not expected to be Turing complete.
QLs not intended to be used for complex calculations.
QLs support easy, efficient access to large data sets.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3
Formal Relational Query Languages
Two mathematical Query Languages form
the basis for real languages (e.g. SQL), and
for implementation:
Relational Algebra: More operational, very useful
for representing execution plans.
Relational Calculus: Lets users describe what they
want, rather than how to compute it. (Non-
operational, declarative.)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4
Preliminaries
A query is applied to relation instances, and the
result of a query is also a relation instance.
Schemas of input relations for a query are fixed (but
query will run regardless of instance!)
The schema for the result of a given query is also
fixed! Determined by definition of query language
constructs.
Positional vs. named-field notation:
Positional notation easier for formal definitions,
named-field notation more readable.
Both used in SQL
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5
Example Instances
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5
58 rusty 10 35.0
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
sid bid day
22 101 10/10/96
58 103 11/12/96
R1
S1
S2
Sailors and Reserves
relations for our examples.
Well use positional or
named field notation,
assume that names of fields
in query results are
`inherited from names of
fields in query input
relations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6
Relational Algebra
Basic operations:
Selection ( ) Selects a subset of rows from relation.
Projection ( ) Deletes unwanted columns from relation.
Cross-product ( ) Allows us to combine two relations.
Set-difference ( ) Tuples in reln. 1, but not in reln. 2.
Union ( ) Tuples in reln. 1 and in reln. 2.
Additional operations:
Intersection, join, division, renaming: Not essential, but
(very!) useful.
Since each operation returns a relation, operations
can be composed! (Algebra is closed.)
o
t
. . <
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12
Joins
Equi-Join: A special case of condition join where
the condition c contains only equalities.
Result schema similar to cross-product, but only
one copy of fields for which equality is specified.
Natural Join: Equijoin on all common fields.
sid sname rating age bid day
22 dustin 7 45.0 101 10/10/96
58 rusty 10 35.0 103 11/12/96
S R
sid
1 1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13
Division
Not supported as a primitive operator, but useful for
expressing queries like:
Find sailors who have reserved all boats.
Let A have 2 fields, x and y; B have only field y:
A/B =
i.e., A/B contains all x tuples (sailors) such that for every y
tuple (boat) in B, there is an xy tuple in A.
Or: If the set of y values (boats) associated with an x value
(sailor) in A contains all y values in B, the x value is in A/B.
In general, x and y can be any lists of fields; y is the
list of fields in B, and x y is the list of fields of A.
{ }
x x y A y B | , - e e