Beruflich Dokumente
Kultur Dokumente
Relational Algebra
Chapter 6
Fall 2014
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Relational Algebra
In previous lectures, we studied SQL (practical language for the relational
model)
Relational Algebra and Calculus were developed before SQL language
SQL is based on concepts from both Algebra and Calculus
Relational Algebra
Basic set of operations for the relational model
These operations enable a user to perform specific basic retrieval requests as
relational algebra expressions (sequence of relational algebra operations)
Relational calculus
Higher-level declarative language for specifying relational queries
2
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Relational Algebra
There are six basic operators in relational algebra:
select
project
union
set difference
Cartesian product x
rename
Two types of relational operations
Unary: operate on one relation
Binary: operate on two tables (or pairs of relations) by combining related records (rows)
The operators take one or two relations as inputs and produce a new relation as a
result
SELECT Operation
used to choose a subset of tuples from a relation that
satisfies a selection condition
different from SELECT clause of SQL
SELECT is simply a filter that keeps only those tuples that satisfy a
qualifying condition
Notation
Sigma (denotes the
SELECT operator)
argument relation
predicate
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
SELECT Operation
The predicate or selection condition consists of terms or Boolean expressions
connected by Boolean conditions: (and), (or), (not)
Each term or Boolean expression is made up of a number of clauses of the form
predicate or selection condition
attribute name
comparison operator
constant value or
attribute name
SELECT Operation
Examples
Example 1
A
12
23 10
23 10
R
Example 2
corresponds to the following SQL query
SELECT *
FROM EMPLOYEE
WHERE (Dno = 4 AND Salary>25000) OR (Dno = 5 AND Salary>30000)
6
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
PROJECT Operation
SELECT operation chooses certain rows and discards other
rows
PROJECT operation on the other hand chooses certain
columns from the table and discards the other columns
If we are interested in only selecting certain attributes of a relation, then
we use PROJECT
No duplicates: result of PROJECT operation is a set of distinct tuples
Notation
pi (denotes the
PROJECT operator)
argument relation
degree = number of attributes in
attribute list
7
PROJECT Operation
Example
SQL:
SELECT DISTINCT(A,C) FROM R
Example 1
10
20
30
40
A,C
(R)
R
Duplicates Removed
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
RENAME Operation
In general, several relational algebra operations are
applied for most queries
We can write these queries in two ways:
SQL:
SELECT DISTINCT
Fname, Lname, Salary
FROM EMPLOYEE
WHERE Dno=5
RENAME Operation
Rename attributes in intermediate results
RENAME operation
Notation:
new relation
name
new attribute
names
argument relation
renames relation
only
renames
attributes only
10
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
RENAME Operation
Example
Example 1
just relation
A
x
x
x
a
a
a
x
z
z
SELECT *
FROM R as S
S (R)
x
x
x
a
a
a
x
z
z
just attributes
(D, E, F )
S(D, E, F )(R)
relation and
attributes
(S)
x
x
x
a
a
a
x
z
z
SELECT A as D, B
as E, C as F,
FROM S
11
Summary
SELECT
SELECT
FROM
WHERE
*
EMPLOYEE
Dno=4 AND Salary > 25000
Fname, Salary(EMPLOYEE)
PROJECT
SELECT DISTINCT
FROM
Fname, Salary
EMPLOYEE
RENAME
SELECT
FROM
WHERE
12
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
UNION Operation
Denoted by: R
To find all course IDs for courses taught in the Fall 2009
semester, or in the Spring 2010 semester, or in both
course_id (
semester=Fall
year=2009 (section))
course_id (
semester=Spring
year=2010 (section))
13
UNION Operation
Example 2
No Duplicates
14
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
intersection: R
S = R - (R - S)
Purpose: Result of this operation includes all tuples that are in both
R and S
Intersection is:
Commutative: R S = S R
Associative: R (S
W) = (R
S)
INTERSECTION Operation
Example
Example
x
x
y
1
2
1
x
y
2
3
A
x
B
2
16
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Example
Example
RS
17
DIVISION Operation
Denoted by: R
Purpose: Suited for queries that include the phrase for all
queries
Example 1
18
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
DIVISION Operation
Example 1
Example
A
x
x
x
y
z
m
m
m
n
n
y
1
2
3
1
1
1
3
4
6
1
2
R
19
DIVISION Operation
Example 2
Example
x
x
x
y
y
z
z
z
a
a
a
a
a
a
a
a
x
z
z
z
z
z
z
y
a
a
b
a
b
a
b
b
1
1
1
1
3
1
1
1
a
b
1
1
x
z
a
a
z
z
20
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
10
Assignment Operation
Denoted by
Purpose: 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.
21
Assignment Operation
Example 1
Example 1
(EMPLOYEE)
Sex=F
(FEMALE_EMPS)
22
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
11
Assignment Operation
Example 2
Example 1
10
20
30
40
Result1
A,C
(R)
Result1
23
Cartesian Product
(Cross Product) Operation
Denoted by R S
Purpose: Combines every member (tuple) from one
relation (set) with every member (tuple) from the other
relation (set)
Also known as Cross Product or Cross Join
This is also a binary set operation but the relations on
which it is applied do not have to be union compatible
24
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
12
x
y
y
z
10
10
20
10
a
a
b
b
RxS
x
x
x
x
y
y
y
y
1
1
1
1
2
2
2
2
x
y
y
z
x
y
y
z
10
10
20
10
10
10
20
10
a
a
b
b
a
a
b
b
Example 2
Course
dept
TA
name
major
cnum
instructor
term
CS
338
Jones
Spring
Ashley
CS
CS
330
Smith
Winter
Lee
STATS
STATS
330
Wong
Winter
Course TA
dept
cnum
CS
338
CS
330
STATS
330
CS
338
CS
330
STATS
330
instructor
Jones
Smith
Wong
Jones
Smith
Wong
term
Spring
Winter
Winter
Spring
Winter
Winter
name
Ashley
Ashley
Ashley
Lee
Lee
Lee
major
CS
CS
CS
STATS
STATS
STATS
25
Cardinality
cardinality(R x S) = cardinality(R)
cardinality(S)
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
13
JOIN Operation
Denoted by R
S
Purpose: Like cross product, a JOIN combines tuples from
two relations into single longer tuples, but only those that
satisfy matching condition
Formally, a combination of cross product and select
<join condition>
Example 1
Example 2
Who can TA courses offered by their own department?
Course
TA
Course TA
dept
cnum
instructor
term
name
major
dept
cnum
instructor
term
name
CS
338
Jones
Spring
Ashley
CS
CS
338
Jones
Spring
Ashley
CS
CS
330
Smith
Winter
Lee
STATS
CS
330
Smith
Winter
Ashley
CS
STATS
330
Wong
Winter
STATS
330
Wong
Winter
Lee
STATS
major
27
Types of JOIN
Theta JOIN
Denoted by: R
<r.A s.B>
where
A is an attribute of R, B is an attribute of S
A and B have the same domain
is one of the comparison operators {=, , < , >, , }
{ =, , >, , <,
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
14
Types of JOIN
Theta JOIN Example
EmployeeID
Name
DeptID
Position
Salary
111
Sue
Manager
65000
222
John
Accountant
42000
333
Mary
Clerk
28000
444
Victor
NULL
Manager
52000
Employee
Department
Example
Employee
DeptID = DeptID
DeptID
DeptName
Location
Sales
Room 245
Purchase
Room 430
Marketing
Room 212
Department
EmployeeID
Name
DeptID
Position
Salary
DeptID
DeptName
Location
111
Sue
Manager
65000
Sales
Room 245
222
John
Accountant
42000
Purchase
Room 430
333
Mary
Clerk
28000
Sales
Room 245
29
Types of JOIN
EQUIJOIN
EQUIJOIN is the most common join condition that
involves an equality comparisons where is {=}
This special type of Theta JOIN is called EQUIJOIN
Denoted by: R
Example
Employee
<r.A = s.B>
DeptID = DeptID
Department
EmployeeID
Name
DeptID
Position
Salary
DeptID
DeptName
Location
111
Sue
Manager
65000
Sales
Room 245
222
John
Accountant
42000
Purchase
Room 430
333
Mary
Clerk
28000
Sales
Room 245
Disadvantage: redundancy
30
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
15
Types of JOIN
Natural JOIN
Denoted by: R * S
Purpose: Removes the extra attribute in an
EQUIJOIN
Name
DeptID
Position
Salary
DeptName
Location
Room 245
111
Sue
Manager
65000
Sales
222
John
Accountant
42000
Purchase
Room 430
333
Mary
Clerk
28000
Sales
Room 245
31
Types of JOIN
OUTER JOIN Operation
Natural JOIN eliminates tuples that have no relation or do not
match
This includes tuples with NULL values
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
16
Types of JOIN
OUTER JOIN Operation: LEFT OUTER JOIN
Denoted by: R
Name
Department
DeptID
Position
Salary
DeptName
Location
111
Sue
Manager
65000
Sales
Room 245
222
John
Accountant
42000
Purchase
Room 430
333
Mary
Clerk
28000
Sales
Room 245
444
Victor
NULL
Manager
52000
NULL
NULL
33
Types of JOIN
OUTER JOIN Operation: RIGHT OUTER JOIN
Denoted by: R
Employee
EmployeeID
Name
Department
DeptID
Position
Salary
DeptName
Location
111
Sue
Manager
65000
Sales
Room 245
222
John
Accountant
42000
Purchase
Room 430
333
Mary
Clerk
28000
Sales
Room 245
NULL
NULL
NULL
NULL
Marketing
Room 212
34
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
17
Types of JOIN
OUTER JOIN Operation: FULL OUTER JOIN
Denoted by: R
Purpose: Keeps all tuples in both the left and right relations
when no matching tuples are found
Add NULL values as required
Example
Employee
Department
EmployeeID
Name
DeptID
Position
Salary
DeptName
Location
111
Sue
Manager
65000
Sales
Room 245
222
John
Accountant
42000
Purchase
Room 430
333
Mary
Clerk
28000
Sales
Room 245
444
Victor
NULL
Manager
52000
NULL
NULL
NULL
NULL
NULL
NULL
Marketing
Room 212
35
Summary
Select
Project
Rename
Union
Difference
Join
Natural Join
Left Outer Join
Right Outer Join
Full Outer Join
Intersection
Division
Assignment
Cartesian Product
36
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
18