Chapter 3
Chapter 3
Relational algebra:
Algebra of relations -> set of operators that take relations as input and
produce relations as output
-> composable: the output of evaluating an expression in relational
algebra can be used as input to another relational algebra expression
Now: First introduction to operators of the relational algebra
Procedural language
Six basic operators
select:
project:
union:
set difference: –
Cartesian product: x
rename:
The operators take one or two relations
as inputs and produce a new relation as a
result.
composable
Notation: p(r)
p is called the selection predicate
Defined as:
For r s to be valid.
1. r, s must have the same arity (same number
of attributes)
2. The attribute domains must be compatible
(example: 2nd column
of r deals with the same type of values as
does the 2nd column of s)
Example: to find all courses taught in the Fall semester, or in
the Spring semester, or in both. First get all the section
offered in the spring and fall semester:
semester=“Fall” (section) semester=“Spring” (section)
2- Than show the course_ID attribute
course_id ( semester=“Fall” (section) semester=“Spring” (section))
Notation r – s
Defined as:
- 95000
We define additional operations that do not
add any expressive power to the relational
algebra, but that simplify common queries.
Natural join
Assignment
Outer join
Notation: r s
Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R S obtained as
follows:
Consider each pair of tuples tr from r and ts from s.
If tr and ts have the same value on each of the attributes in R S,
add a tuple t to the result, where
t has the same value as tr on r
t has the same value as ts on s
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as:
r.A, r.B, r.C, r.D, s.E (r.B = s.B r.D = s.D (r x s))
Let r and s be relations on schemas R and S
respectively.
Then, r s is defined as:
Relations r, s:
n r s
Find the names of all instructors in the Comp. Sci.
department together with the course titles of all the
courses that the instructors teach
name, title ( dept_name=“Comp. Sci.” (instructor teaches
course))
Natural join is associative
(instructor teaches) course is equivalent to
instructor (teaches course)
Natural join is commutative (we ignore attribute order)
instruct teaches is equivalent to teaches instructor
The theta join operation r s is defined as
The assignment operation () provides a
convenient way to express complex queries.
Write query as a sequential program consisting of
a series of assignments
followed by an expression whose value is displayed as a result
of the query.
Assignment must always be made to a temporary relation
variable.
An extension of the join operation that
avoids loss of information.
Computes the join and then adds tuples
form one relation that does not match
tuples in the other relation to the result of
the join.
Uses null values:
null signifies that the value is unknown or does
not exist
All comparisons involving null are (roughly
speaking) false by definition.
We shall study precise meaning of comparisons with
nulls later
Relation instructor1
ID name dept_name
10101 Srinivasan Comp. Sci.
12121 Wu Finance
15151 Mozart Music
Relation teaches1
ID course_id
10101 CS-101
12121 FIN-201
76766 BIO-101
Join
instructor teaches
ID name dept_name course_id
10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
A B C
7
7
3
10
sum(c )
sum(c) (r)
27
Find the average salary in each department
dept_name avg(salary) (instructor)
avg_salar
y
The content of the database may be
modified using the following
operations:
Deletion
Insertion
Updating
All these operations can be expressed
using the assignment operator
Example: Delete instructors with salary
over $1,000,000
A nonprocedural query language, where each
query is of the form
{t | P (t ) }
It is the set of all tuples t such that predicate
P is true for t
t is a tuple variable, t [A ] denotes the value
of tuple t on attribute A
t r denotes that tuple t is in relation r
P is a formula similar to that of the predicate
calculus
1. Set of attributes and constants
2. Set of comparison operators: (e.g., , , ,
, , )
3. Set of logical connectives: and (), or (v)‚
not ()
4. Implication (): x y, if x if true, then y is
true
x y x v y
5. Set of quantifiers:
t r (Q (t )) ”there exists” a tuple in t in relation r
such that predicate Q (t ) is true
t r (Q (t )) Q is true “for all” tuples t in relation r
Find the ID, name, dept_name, salary for
instructors whose salary is greater than $80,000
{t | t instructor t [salary ] 80000}
n Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
n Find the set of all courses taught in the Fall 2009 semester, but not
in
the Spring 2010 semester
{t | s section (t [course_id ] = s [course_id ]
s [semester] = “Fall” s [year] = 2009)
u section (t [course_id ] = u [course_id ]
u [semester] = “Spring” u [year] = 2010)}
It is possible to write tuple calculus expressions
that generate infinite relations.
For example, { t | t r } results in an infinite
relation if the domain of any attribute of relation r
is infinite
To guard against the problem, we restrict the set
of allowable expressions to safe expressions.
An expression {t | P (t )} in the tuple relational
calculus is safe if every component of t appears in
one of the relations, tuples, or constants that
appear in P
◦ NOTE: this is more than just a syntax condition.
E.g. { t | t [A] = 5 true } is not safe --- it defines an infinite
set with attribute values that do not appear in any relation or
tuples or constants in P.
Find all students who have taken all courses offered in the
Biology department
◦ {t | r student (t [ID] = r [ID])
( u course (u [dept_name]=“Biology”
s takes (t [ID] = s [ID ]
s [course_id] = u [course_id]))}
◦ Note that without the existential quantification on student,
the above query would be unsafe if the Biology department
has not offered any courses.
A nonprocedural query language equivalent in
power to the tuple relational calculus
Each query is an expression of the form: