Week 4: Relational Algebra (Part II) : Database System Concepts
Week 4: Relational Algebra (Part II) : Database System Concepts
▪ Additional Operations
▪ Set intersection
▪ Natural join
▪ Division
▪ Assignment
2
Set-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)
3
Set-Intersection Operation – Example
▪ Relation r, s: A B A B
1 2
2 3
1
s
r
▪ rs
A B
2
4
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 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))
5
Natural Join Operation – Example
▪ Relations r, s:
A B C D B D E
1 a 1 a
2 a 3 a
4 b 1 a
1 a 2 b
2 b 3 b
r s
▪ r s
A B C D E
1 a
1 a
1 a
1 a
2 b
6
Division Operation
▪ Notation: rs
▪ Suited to queries that include the phrase “for all”.
▪ Let r and s be relations on schemas R and S respectively
where
▪ R = (A1, …, Am , B1, …, Bn )
▪ S = (B1, …, Bn)
The result of r s is a relation on schema
R – S = (A1, …, Am)
r s = { t | t R-S (r) u s ( tu r ) }
Where tu means the concatenation of tuples t and u to
produce a single tuple
7
Division Operation – Example I
▪ Relations r, s:
A B B
1 1
2
3 2
1 s
1
1
3
4
6
1
2
r
▪ r s: A
8
Division Operation – Example II
▪ Relations r, s:
A B C D E D E
a a 1 a 1
a a 1 b 1
a b 1 s
a a 1
a b 3
a a 1
a b 1
a b 1
r
▪ r s: A B C
a
a
9
Division Operation – Example III
a a b1 a b2 a b3
10
10
Assignment Operation
▪ 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.
▪ Example: Write r s as
temp1 R-S (r )
temp2 R-S ((temp1 x s ) – R-S,S (r ))
result = temp1 – temp2
▪ The result to the right of the is assigned to the relation variable on
the left of the .
▪ May use variable in subsequent expressions.
11
Banking Enterprise Schema Diagram
12
12
Bank Example Queries
▪ Find the names of all customers who have a loan and an account at
bank.
▪ Find the name of all customers who have a loan at the bank and the
loan amount.
13
Bank Example Queries
▪ Find all customers who have an account from at least the “Downtown”
and the “Uptown” branches.
▪ Query 1
▪ Query 2
customer_name, branch_name (depositor account)
temp(branch_name) ({(“Downtown” ), (“Uptown” )})
14
Bank Example Queries
▪ Find all customers who have an account at all branches located in
Brooklyn city.
15
Extended Relational Algebra Operations
▪ Generalized Projection
▪ Aggregate Functions
▪ Outer Join
16
Generalized Projection
▪ Extends the projection operation by allowing arithmetic functions to be
used in the projection list.
credit_info result
17
Aggregate Functions and Operations
▪ Aggregation function takes a collection of values and returns a single
value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
▪ Aggregate operation in relational algebra
18
Aggregate Operation – Example
▪ Relation r:
A B C
7
7
3
10
27
19
Aggregate Operation – Example
▪ Relation r:
A B C
7
7
3
10
14
13
20
Aggregate Operation – Example
▪ Relation account grouped by branch-name:
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
21
Aggregate Functions (Cont.)
▪ Result of aggregation does not have a name
▪ Can use rename operation to give it a name
▪ For convenience, we permit renaming as part of aggregate
operation
22
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
23
Outer Join – Example
▪ Relation loan
▪ Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
24
Outer Join – Example
▪ Join
loan borrower
25
Outer Join – Example
▪ Right Outer Join
loan borrower
26
Null Values
▪ It is possible for tuples to have a null value, denoted by null, for some
of their attributes
▪ null signifies an unknown value or that a value does not exist.
▪ The result of any arithmetic expression involving null is null.
▪ Aggregate functions simply ignore null values (as in SQL)
▪ For duplicate elimination and grouping, null is treated like any other
value, and two nulls are assumed to be the same (as in SQL)
27
Null Values
▪ Comparisons with null values return the special truth value: unknown
▪ If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5
▪ Three-valued logic using the truth value unknown:
▪ OR: (unknown or true) = true,
(unknown or false) = unknown
(unknown or unknown) = unknown
▪ AND: (true and unknown) = unknown,
(false and unknown) = false,
(unknown and unknown) = unknown
▪ NOT: (not unknown) = unknown
▪ In SQL “P is unknown” evaluates to true if predicate P evaluates to
unknown
▪ Result of select predicate is treated as false if it evaluates to unknown
28
Exercises
Given relational schema:
Sailor (sid, sname, city, birthdate)
Boat (bid, bname, color)
SailorBoat (sid, bid, reservationdate)
30
1) Find the names of sailors who’ve reserved boat #103
▪ Can identify all red or green boats, then find sailors who’ve reserved one
of these boats:
▪ Then find the intersection (note that sid is a key for Sailor relation).