0% found this document useful (0 votes)
8 views

Database Management Systems Week 4

Database Management Systems

Uploaded by

matt.0.porter
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Database Management Systems Week 4

Database Management Systems

Uploaded by

matt.0.porter
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Adapted for SEN2104 – DBMS

Week 4: Relational Algebra (Part II)

Database System Concepts


©Silberschatz, Korth and Sudarshan
Additional Operations

▪ Additional Operations

▪ Set intersection
▪ Natural join
▪ Division
▪ Assignment

▪ All above, other than aggregation, can be expressed using basic


operations we have seen earlier

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

▪ rs
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

loan borrower x loan

borrower loan ≡ customer-name, borrower.loan_number, (borrower.loan_number (borrower x loan))


branch_name, amount = loan.loan_number
6
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 

7
Division Operation

▪ Notation: rs
▪ 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

sno pno pno pno pno


s1 p1 p2 p2 p1
s1 p2 b1 p4 p2
s1 p3 b2 p4
s1 p4 b3
s2 p1 sno
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1

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

branch (branch-name, branch-city, assets)


customer (customer-name, customer-street, customer-city)
account (account-number, branch-name, balance)
loan (loan-number, branch-name, amount)
depositor (customer-name, account-number)
borrower (customer-name, loan-number)

13
Example Queries
▪ Find the names of all customers who have a loan and an account at
the bank.

customer_name (borrower)  customer_name (depositor)

▪ Find the names of all customers who have a loan at the bank and the
loan amount.

▪ Query 1

customer_name, amount (borrower loan)

▪ 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

customer_name (branch_name = “Downtown” (depositor account )) 

customer_name (branch_name = “Uptown” (depositor account))

▪ Query 2
customer_name, branch_name (depositor account)
 temp(branch_name) ({(“Downtown” ), (“Uptown” )})

Note that Query 2 uses a constant relation.

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.

customer_name, branch_name (depositor account)  temp(branch_name) ({(“Downtown” ), (“Uptown” )})

Result

16
Example Queries
▪ Find all customers who have an account at all branches located in
Brooklyn city.

customer_name, branch_name (depositor account)


 branch_name (branch_city = “Brooklyn” (branch))

17
Example Queries
Sample data for the query in the previous slide

▪ Find all customers who have an account at all branches located in


Brooklyn city.

customer_name, branch_name (depositor account)  branch_name (branch_city = “Brooklyn” (branch))

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.

F1, F2, …, Fn (E)


▪ E is any relational-algebra expression
▪ Each of F1, F2, …, Fn are are arithmetic expressions involving constants
and attributes in the schema of E.
▪ Given relation credit_info(customer_name, limit, credit_balance), find
how much more each person can spend:
customer_name, limit – credit_balance as credit_available (credit_info)

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

E is any relational-algebra expression


▪ G1, G2 …, Gn is a list of attributes on which to group (can be empty)
▪ Each Fi is an aggregate function
▪ Each Ai is an attribute name

21
Aggregate Operation – Example
▪ Relation r:
A B C

  7
  7
  3
  10

▪ g sum(C) (r) sum(C)

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 account_number balance


Perryridge A-102 400
Perryridge A-201 900
Brighton A-217 750
Brighton A-215 750
Redwood A-222 700

branch_name g sum(balance) (account)

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 g sum(balance) as sum_balance (account)

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

loan_number branch_name amount


L-170 Downtown 3000
L-230 Redwood 4000
L-260 Perryridge 1700

▪ Relation borrower

customer_name loan_number
Jones L-170
Smith L-230
Hayes L-155

27
Outer Join – Example
▪ Join

loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith

▪ Left Outer Join


loan borrower
loan_number branch_name amount customer_name
L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null

28
Outer Join – Example
▪ Right Outer Join
loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-155 null null Hayes

▪ Full Outer Join


loan borrower

loan_number branch_name amount customer_name


L-170 Downtown 3000 Jones
L-230 Redwood 4000 Smith
L-260 Perryridge 1700 null
L-155 null null Hayes

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

You might also like