0% found this document useful (0 votes)
61 views18 pages

INT 306 Database Management System: Relational Algebra Part 3

This document provides examples and explanations of relational algebra operations including natural join, theta join, outer join, division, and extended operations like generalized projection and aggregation. It defines each operation formally and provides sample relations and results to demonstrate how each operation works. Examples include computing the natural join of two relations, performing left, right, and full outer joins, and using division to find sailors who have reserved all boats.

Uploaded by

Anuj Singh Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views18 pages

INT 306 Database Management System: Relational Algebra Part 3

This document provides examples and explanations of relational algebra operations including natural join, theta join, outer join, division, and extended operations like generalized projection and aggregation. It defines each operation formally and provides sample relations and results to demonstrate how each operation works. Examples include computing the natural join of two relations, performing left, right, and full outer joins, and using division to find sailors who have reserved all boats.

Uploaded by

Anuj Singh Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

INT 306

Database Management System


Lecture 9
Relational Algebra Part 3
Example
• Schema Example:
• Sailors(sid : integer, sname : string ,rating : integer, age: real)
• Boats(bid: integer, bname: string, color: string)
• Reserves(sid: integer, bid: integer, day: date)
• Instance s1 of sailors Instance s2 of sailors Instance r1 of reserves
sid sname rating age sid sname rating age sid bid day
22 Lia 8 40 23 Ruby 8 37 22 101 30/07/2018
23 Ruby 8 37 25 Lee 6 35.5 23 104 2/08/2018
24 John 6 34 27 Robin 9 40
29 Jack 7 35.5
Natural Join
• 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))
Example
R S
sid sname rating age sno sname age
22 Lia 8 40 1 Lia 40
23 Ruby 8 37 2 Ruby 37
24 John 6 34 3 John 34
25 Jack 8 45 4 Lee 45
26 Robin 6 35 5 Robin 30
Compute R ⋈ S
sid sname rating age sno
22 Lia 8 40 1
23 Ruby 8 37 2
24 John 6 34 34
Theta Join
• Condition join is also called as Theta join.
• The theta join operation r ⋈ s is defined as
• r ⋈  s =  (r x 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.
Example Outer Join
Instance s1 Join s1⋈r1
sid sname rating age sid sname rating age bid day
22 Lia 8 40 22 Lia 8 40 101 30/07/218
23 Ruby 8 37 23 Ruby 8 37 104 2/08/2018
24 John 6 34

Instance r1 Left Outer Join s1 r1


sid bid day sid sname rating age bid day
22 101 30/07/2018 22 Lia 8 40 101 30/07/218
23 104 2/08/2018 23 Ruby 8 37 104 2/08/2018
45 107 3/08/2018 24 John 6 34 null null
34 112 7/08/2018
Example Outer Join
Instance s1 Right Outer Join s1 r1
sid sname rating age sid sname rating age bid day
22 Lia 8 40 22 Lia 8 40 101 30/07/218
23 Ruby 8 37 23 Ruby 8 37 104 2/08/2018
24 John 6 34 45 null null null 107 3/08/2018
34 null null null 112 7/08/2018
Instance r1
sid bid day Full Outer Join s1 r1
22 101 30/07/2018
sid sname rating age bid day
23 104 2/08/2018
22 Lia 8 40 101 30/07/218
45 107 3/08/2018 23 Ruby 8 37 104 2/08/2018
34 112 7/08/2018 24 John 6 34 null null
45 null null null 107 3/08/2018
34 null null null 112 7/08/2018
Division
• What would you do to “find the names of sailors who have reserved
all boats”?
• Instance S3 of Sailors Instance R2 of Reserves Instance B1 of Boats
sid sname rating age sid bid day bid bname color

22 Dustin 7 45.0 22 101 10/10/98 101 Interlake blue

29 Brutus 1 33.0 22 102 10/10/98 102 Interlake red

31 Lubber 8 55.5 22 103 10/8/98 103 Clipper green

32 Andy 8 25.5 22 104 10/7/98 104 Marine red

58 Rusty 10 35.0 31 102 11/10/98

64 Horatio 7 35.0 31 103 11/6/98

71 Zorba 10 16.0 31 104 11/12/98

74 Horatio 9 35.0 64 101 9/5/98

85 Art 3 25.5 64 102 9/8/98

95 Bob 3 63.5 74 103 9/8/98


Division (cont..)
• Let A have 2 fields, x and y; B have only field y:
A/B = { <x> | ꓱ <x,y> ∈ A ꓯ <y> ∈ B}
• Two ways to interpret:
• A/B contains all x tuples such that for every y tuple in B, there is an xy
tuple in A
• If the set of y values associated with an x value in A contains all y
values in B, the x value is in A/ B
Division (cont..)
• Analogy with integer division:
For integers A and B, A/B is the largest integer Q such that
Q*B <= A
For relation instances A and B, A/B is the largest relation
instance Q such that
QxB⊆A
Example of A/B
sno pno pno sno
A s1 p1 B1 p2 A/B1 s1

s1 p2 s2

s1 p3 s3
pno
s1 p4 B2 s4
p2
s2 p1
p4
s2 p2 sno

s3 p2
A/B2 s1

s4 p2 pno s4

s4 p4 B3 p1

p2 sno
A/B3 s1
p4
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)
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
Extended Relational-Algebra-Operations
• Generalized Projection
• Aggregate Functions
Generalized Projection
• Extends the projection operation by allowing arithmetic functions to be
used in the projection list.
F , F , …, F (E)
1 2 n

• 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 instructor(ID, name, dept_name, salary) where salary is
annual salary, get the same information but with monthly salary
ID, name, dept_name, salary/12 (instructor)
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
Aggregate Operation Example
• Find the average salary in each department
dept_name avg(salary) (instructor)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy