Upload
kristian-robertson
View
214
Download
1
Embed Size (px)
Citation preview
Relational AlgebraRelational Calculus
Objectives Fundamental operations in RA Union Set difference Select Project Cartesian Product Relational Calculus
Chapter Five
2
Query Languages
1. Procedural Relational Algebra (RA)
2. Non-Procedural Tuple Relational Calculus Domain Relational Calculus
3
Fundamental Operations in RA Binary Operations
UNION MINUS CARTESIAN PRODUCT
Unary Operations SELECT PROJECT
4
Union of R S
1. Union of R Sa. All tuples in R or Sb. Union Compatible
Some degree Attributes of R&S must be the same
5
Union of R S
Faculty Name
ID Salary
Smith 1 70,000
Nelson
3 85,000
Larson
6 62,000
Staff Name ID Salary
Smith 1 70,000
Anderson 3 45,000
Faculty U Staff
Name ID Salary
Smith 1 70,000
Nelson 3 85,000
Larson 6 62,000
Anderson 3 45,000
6
Union of R S
name (faculty) name (staff)
7
Set Difference (MINUS) R - S Set of tuples in R but not in S Union Compatible
Faculty - Staff
Name ID Salary
Nelson 3 85,000
Larson 6 62,000
8
Intersection: R S Set of of tuples belong to both R & S Union CompatibleFind the list of faculty members who are also
staff
70,0001Smith
SalaryIDNameFaculty Staff
R S = R – (R – S)
9
Cartesian Product R x S Set of (K1 + K2) tuples: The first K1 tuples are
from R. The last K2 tuples are from S
Semester
S_Num
Year Season
1 99 F
2 99 S
3 00 FSemester_Course
C_Num S_Num
200 1
250 1
300 2
10
Cartesian Product R x SRxS S_Num Year Season C_Num S_Num
1 99 F 200 1
1 99 F 250 1
1 99 F 300 2
2 99 S 200 1
2 99 S 250 1
2 99 S 300 2
3 00 F 200 1
3 00 F 250 1
3 00 F 300 2
List of courses offered in year 99?
11
Selection (Unary Relation) Select tuples that satisfy a given
predicate major = ‘COSC’ (Student)
Result is another relation Conditions are relational operator
(,, , , ) Logical operators AND (), OR(),
NOT()
12
Selection (Unary Relation) Find all Faculty members which make
less than $45,000 salary < 45,000(Faculty)
Find all staff who make less than $40,000 and ID > 100
salary < 40,000 AND ID > 100 (Staff) List of number of courses offered in year
99 S_Num = S_Num AND year = 99 (Semester x
Semester_Course)
13
Projection (Unary) Select attributes (Pi) Find the number of faculty that
teach COSC courses name( course=‘COSC’(Faculty))
14
R S Theta join allows us to combine the selection & the
cartesian product into an operation
Theta Join
R A B
a b
B c
b c
R C D E
a b c
a b d
b e c
a c d
R S A B C D E
a b a b c
a b a b d
a c a c d
b c a c d
If is = It is called EquijoinIf the attributes have the same name, the join is called Natural Join
DB
ji
15
Relational Calculus (RC)
Non-procedural Most commercial query language Types:
Tuple RC: Variables represent tuples Domain RC: Variables represent
values of domain
Part Two
16
Tuple Relational Calculus:{ t | P(t)}
Examples List of students with GPA > 3 { t | t Є student ^ t[GPA] > 3 } List of students name with GPA > 3 { t | s Є student (t[NAME] = s[NAME]
^ t[GPA] > 3)}