Sie sind auf Seite 1von 18

CS 338: Computer Applications in Business: Databases (Fall 2014)

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. )

CS 338: Computer Applications in Business: Databases


Rice University Data Center

Formal Languages for Relational Model

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

Formal Languages for Relational Model

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

<selection condition> is applied independently to each individual tuple t in R

If condition evaluates to TRUE, tuple selected


All selected tuples appear in the result of the SELECT operation

SELECT operation is commutative

A sequence of SELECT operations can be applied in any order.

The fraction of tuples selected by a selection condition is referred to as the


selectivity of the condition
5

SELECT Operation

Examples
Example 1
A

12

23 10

23 10

(A=B ^ D > 5) (R)

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Note: Duplicates are


removed in relational
algebra but not in SQL
statements

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Write operations as a single relational algebra expression


by nesting the operations

Apply one operation at a time and create intermediate


result relations (we need to give names to relations that
hold intermediate results)
Give a name to each
intermediate relation

RENAME Operation
Rename attributes in intermediate results
RENAME operation
Notation:
new relation
name

rho (denotes the


RENAME operator)

new attribute
names

argument relation

renames both relation


and attributes

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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, PROJECT, and RENAME

SELECT
SELECT
FROM
WHERE

*
EMPLOYEE
Dno=4 AND Salary > 25000

Fname, Salary(EMPLOYEE)

PROJECT

SELECT DISTINCT
FROM

Fname, Salary
EMPLOYEE

(Fname,Salary)( (E.Dno=5)( E(First_name, ESalary)(EMPLOYEE)))

RENAME

SELECT
FROM
WHERE

E.Fname As First_name, E.Salary As ESalary


EMPLOYEE As E
E.Dno = 5

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

UNION Operation
Denoted by: R

Purpose: Result of this operation includes all tuples that


are either in R or in S or in both R and S
Duplicate tuples are eliminated
Example 1

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

INTERSECTION and SET DIFFERENCE


Operations
INTERSECTION
Denoted by R

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)

SET DIFFERENCE (or MINUS)


Denoted by R S
Purpose: Result of this operation includes all tuples that are in R but
not in S
15

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

SET DIFFERENCE Operation

Example
Example

RS

17

DIVISION Operation
Denoted by: R

Purpose: Suited for queries that include the phrase for all
queries
Example 1

Let TABLE1 have 2 columns (A and B); TABLE2 have column


B:
TABLE1 TABLE2 contains all A tuples such that for every B tuple in
TABLE2, there is an AB tuple in TABLE1

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. )

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

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.

Assignment must always be made to a temporary relation variable.


The result to the right of the

is assigned to the relation variable

on the left of the

21

Assignment Operation

Example 1
Example 1

Retrieve a list of all female employees in the


EMPLOYEE table
FEMALE_EMPS
EMPNAMES

(EMPLOYEE)

Fname, Lname, Ssn

The result to the right of the


on the left of the

Sex=F

(FEMALE_EMPS)

is assigned to the relation variable

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

Cartesian Product Operation


Examples
Example 1

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

Characteristics of Cartesian Product


Degree
degree(R x S) = degree(R) + degree(S)

Cardinality
cardinality(R x S) = cardinality(R)

cardinality(S)

The result of Cartesian Product returns all possible


combinations
It would not be useful unless it is followed by a SELECT operation
This is also called JOIN
26

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

What are the names and salaries of all department managers?

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 {=, , < , >, , }
{ =, , >, , <,

Purpose: Serves as a general join condition


Combines two tuples into a single combined tuple satisfying a
given condition

Also known as the inner join


28

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

Types of JOIN
Natural JOIN
Denoted by: R * S
Purpose: Removes the extra attribute in an
EQUIJOIN

Both join attributes must have the same name in


both relations
If this is not the case, a renaming operation is applied
first
EmployeeID

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

Outer JOIN is an extension of the join operation that avoids loss of


information
That is, keep all tuples in R, or all those in S, or all those in both relations
regardless of whether or not they have matching tuples in the other relation

Three main Outer JOIN types:


LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
32

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

Types of JOIN
OUTER JOIN Operation: LEFT OUTER JOIN
Denoted by: R

Purpose: Keeps every tuple in the first (or left) relation R


If no matching tuple is found in S, then the attributes of S in the join
result are filled with NULL values
Example 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

444

Victor

NULL

Manager

52000

NULL

NULL

33

Types of JOIN
OUTER JOIN Operation: RIGHT OUTER JOIN
Denoted by: R

Purpose: Keeps every tuple in the second (or right) relation S


If no matching tuple is found in R, then the attributes of R in the join
result are filled with NULL values
Example

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

CS 338: Computer Applications in Business: Databases (Fall 2014)

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

Das könnte Ihnen auch gefallen