Lecture 77777
Lecture 77777
Lecture 77777
▪ Example 1:
Input Relation: sname,rating (S 2) Output Relation:
A,C (r)
Project Operation
• Notation: (r )
A1 , A2 ,, Ak
• Example of selection:
dept_name=“Physics”(instructor)
Select Operation – Example
Relation r
Example
Operator Composition (1)
Single expression versus sequence of relational operations
(Example)
• To retrieve the first name, last name, and salary of all
employees who work in department number 5, we must
apply a select and a project operation
• We can write a single relational algebra expression as follows:
– FNAME, LNAME, SALARY( DNO=5(EMPLOYEE))
• OR We can explicitly show the sequence of operations, giving
a name to each intermediate relation:
– DEP5_EMPS DNO=5(EMPLOYEE)
– RESULT FNAME, LNAME, SALARY (DEP5_EMPS)
Examples of applying sequence of operations
Relational Algebra
operations from Set theory
UNION, INTERSECTION, and MINUS Operations
The Union Operation
▪ Union: R U S
▪ The two input relations must be union-compatible
▪ Same number of fields
▪ `Corresponding’ fields have the same names and type compatible.
▪ The output relation includes all tuples that occur “in either” R or S “or both”
▪ The schema of the output relation is identical to the schema of R
▪ Duplicate tuples are eliminated
r s:
Union Operation
• Notation: r s
• Defined as:
r s = {t | t r or t s}
• 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 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))
Intersection Operation
▪ Intersection: 𝑹 ∩ 𝑺
▪ The two input relations must be union-compatible
▪ The output relation includes all tuples that occur “in both” R and S
▪ The schema of the output relation is identical to the schema of R
▪ Example: S1 S 2
Input Relations:
Output Relation:
sid sname rating age sid sname rating age sid sname rating age
28 yuppy 9 35.0
22 dustin 7 45.0 31 lubber 8 55.5
31 lubber 8 55.5
31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0
58 rusty 10 35.0 58 rusty 10 35.0
S1 S2
Intersection Operation
• Notation: r s
• Defined as:
• r s = { t | t r and t s }
• Assume:
– r, s have the same arity
– attributes of r and s are compatible
• Note: r s = r – (r – s)
Intersection Operation – Example
• Relation r, s:
• rs
Set-Difference Operation
▪ Set-Difference: 𝑹 − 𝑺
▪ The two input relations must be union-compatible
▪ The output relation includes all tuples that occur in R “but not” in S
▪ The schema of the output relation is identical to the schema of R
▪ Example: S1− S 2
Input Relations: Output Relation:
S1 S2
Set difference of two relations
• Relations r, s:
r – s:
Set Difference Operation
• Notation r – s
• Defined as:
r – s = {t | t r and t s}
r x s:
Cartesian-Product Operation
• Notation r x s
• Defined as:
r x s = {t q | t r and q s}
▪ Example: S1 R1
S1. sid R1. sid
sid sname rating age sid bid day (sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 dustin 7 45.0 58 103 11/12/96
22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96
31 lubber 8 55.5
58 rusty 10 35.0 58 103 11/12/96
S1 R1
▪ Example: S1 R1
S1.sid = R1.sid
Input Relations: Output Relation:
sid sname rating age sid bid day sid sname rating age bid day
22 dustin 7 45.0
22 101 10/10/96 22 dustin 7 45.0 101 10/10/96
31 lubber 8 55.5
58 103 11/12/96 58 rusty 10 35.0 103 11/12/96
58 rusty 10 35.0
S1 R1 ONLY one sid column!
The Join Operation
▪ Equi-Join: R c S = c(RS )
▪ A special case of theta join where the condition c contains only equalities
▪ The schema of the output relation is the same as that of cross-product,
“but only one copy of the fields for which equality is specified”
Natural Join
▪ Example: S1 R1
Input Relations: Output Relation:
sid sname rating age sid bid day sid sname rating age bid day
22 dustin 7 45.0
22 101 10/10/96 22 dustin 7 45.0 101 10/10/96
31 lubber 8 55.5
58 103 11/12/96 58 rusty 10 35.0 103 11/12/96
58 rusty 10 35.0
S1 R1 In this case, same as equi-join!
Natural-Join Operation
Notation: r s
• Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R x 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))
Natural Join Example
• Relations r, s:
r s
Outer Join
• 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
Outer Join – Example
• Relation instructor
ID name dept_name
10101 Srinivasan Comp. Sci.
12121 Wu Finance
15151 Mozart Music
Relation teaches
ID course_id
10101 CS-101
12121 FIN-201
76766 BIO-101
Outer Join – Example
• Join
instructor teaches
ID name dept_name course_id
10101 Srinivasan Comp. Sci. CS-101
12121 Wu Finance FIN-201
As a single expression:
Examples of Queries in Relational Algebra
Tool for practice
• RelaX - relational algebra calculator (dbis-uibk.github.io)
2. Relational Calculus
2.1 Tuple Relational Calculus
2.2 Domain relational calculus
Relational Calculus (in General)
▪ It describes what we want to retrieve (not how).
▪ It is a nonprocedural language.
▪ Examples:
▪ Find all students:
{t | t STUDENT }
▪ Find all sailors with a rating above 7:
{t | t Sailors t.rating 7}
Syntax of RTC Queries
▪ The allowed symbols:
, , ,
, , =, , , ,
(, ),
▪ Quantifiers:
,
Predicate Calculus Formula
1. Set of attributes and constants
2. Set of comparison operators: (e.g., , , =, , , )
3. Set of 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
Syntax of TRC Queries
STUDENT CLASS
Ssn Name Address c-id c-name units
123 smith main str 15-413 s.e. 2
234 jones forbes ave 15-412 o.s. 2
TAKES
SSN c-id grade
123 15-413 A
234 15-413 B
Examples
▪ Find all student records
{t | t STUDENT }
output
tuple of type ‘STUDENT’
Examples
▪ Find the student record with ssn=123
Examples
▪ Find the student record with ssn=123
{t | t FT _ STUDENT
t PT _ STUDENT}
* Assume we maintain tables for PT_STUDENT and FT_STUDENT in our Mini University DB
Examples
▪ Find students that are not staff*
{t | t STUDENT
t STAFF}
This is equivalent to the ‘Difference’ operator in Relational Algebra!
* Assume we maintain a table for STAFF in our Mini University DB and that STUDENT and
STAFF are union-compatible
Cartesian Product: A Reminder
▪ Assume MALE and FEMALE dog tables as follows:
M.Name F.Name
MALE FEMALE spike lassie
name x name = spike shiba
spike lassie spot lassie
spot shiba spot shiba
{t | m MALE
f FEMALE
(t.m − name = m.name
t. f − name = f .name)}
TAKES
2-way Join! SSN c-id grade
123 15-413 A
234 15-413 B
More Examples
▪ Find the names of students taking 15-415
{t | s STUDENT
e TAKES ( s.ssn = e.ssn
t.name = s.name
e.c − id = 15 − 415)}
More Examples
▪ Find the names of students taking 15-415
{t | s STUDENT
e TAKES ( s.ssn = e.ssn join
TAKES
SSN c-id grade
3-way Join!
123 15-413 A
234 15-413 B
More Examples
• Find the names of students taking a 2-unit course
{t | s STUDENT e TAKES
c CLASS ( s.ssn = e.ssn join
e.c − id = c.c − id
t.name = s.name projection
c.units = 2)}
selection
Example Queries
• Find the ID, name, dept_name, salary for
instructors whose salary is greater than $80,000
{t | t instructor t [salary ] 80000}
Find the set of all courses taught in the Fall 2009 semester, or in
the Spring 2010 semester, or both
Find the set of all courses taught in the Fall 2009 semester, but not in
the Spring 2010 semester