Database Management Systems Week 4
Database Management Systems Week 4
▪ 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
borrower
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
7
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
8
Division Operation – Example
▪ Relations r, s:
A B B
1 1
2
3 2
1 s
1
1
3
4
6
1
2
r
▪ r s: A
9
Division Operation – Example
▪ 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
10
Division Operation – Example
a a b1 a b2 a b3
11
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.
12
Banking Enterprise Schema Diagram
13
Example Queries
▪ Find the names of all customers who have a loan and an account at
the bank.
▪ Find the names of all customers who have a loan at the bank and the
loan amount.
▪ Query 1
▪ Query 2
customer_name, amount ( borrower.loan_number (borrower x loan) )
=
loan.loan_number
14
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” )})
15
Example Queries
Sample data for “query 2” in the previous slide
▪ Find all customers who have an account from at least the “Downtown”
and the “Uptown” branches.
Result
16
Example Queries
▪ Find all customers who have an account at all branches located in
Brooklyn city.
17
Example Queries
Sample data for the query in the previous slide
Result
18
Extended Relational Algebra Operations
▪ Generalized Projection
▪ Aggregate Functions
▪ Outer Join
19
Generalized Projection
▪ Extends the projection operation by allowing arithmetic functions to be
used in the projection list.
credit_info result
20
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
21
Aggregate Operation – Example
▪ Relation r:
A B C
7
7
3
10
27
22
Aggregate Operation – Example
▪ Relation r:
A B C
7
7
3
10
▪ A
g sum(C) (r) A sum(C)
14
13
23
Aggregate Operation – Example
▪ Relation account grouped by branch-name:
branch_name sum(balance)
Perryridge 1300
Brighton 1500
Redwood 700
24
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
branch_name sum_balance
Perryridge 1300
Brighton 1500
Redwood 700
25
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
26
Outer Join – Example
▪ Relation loan
▪ Relation borrower
customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155
27
Outer Join – Example
▪ Join
loan borrower
28
Outer Join – Example
▪ Right Outer Join
loan borrower
29
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)
30
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
31