Chapter 4 Relational Algebra
Chapter 4 Relational Algebra
Chapter 4
What is it?
• Theoretical Foundations of Queries
• Questions to the Database regarding Data
2
Introduction and Schemas
• Assumptions
3
Relational Algebra
• Foundations of Queries
• Fundamental Properties
• Every operator accepts a relations (one or two) as input
• Every operator returns a relation as output
• This property allows formation of complex queries.
• Operators
• Set operators:
4
Select Operator
• Formal notation
22 Dustin 7 45
31 Lubber 8 55
58 Rusty 10 35
32 Andy 8 25
17 Popeye 10 40
5
Examples on Selection
22 Dustin 7 45 22 Dustin 7 45
31 Lubber 8 55 31 Lubber 8 55
58 Rusty 10 35
32 Andy 8 25
6
Examples on Selection (2)
22 Dustin 7 45 31 Lubber 8 55
31 Lubber 8 55
58 Rusty 10 35
32 Andy 8 25
• Can apply multiple conditions: AND OR ()
• Can apply multiple selections instead
7
Alias Operator ()
• Examples
• Returns a relation S with the same data as Sailors. Now we can use S instead of Sailors
• Returns a relation S with the same data as Sailors, with same columns, except sid is now
sid1 and rating is now rate.
8
Project Operator ()
• Formal notation
22 Dustin 7 45
31 Lubber 8 55
58 Rusty 10 35
32 Andy 8 25
9
Sid Sname Rating Age
Examples on Projection 22 Dustin 7 45
31 Lubber 8 55
58 Rusty 10 35
32 Andy 8 25
Sname Age
Dustin 45
Lubber 55
Rusty 35
Andy 25
10
Sid Sname Rating Age
Examples on Projection(2)
22 Dustin 7 45
31 Lubber 8 55
58 Rusty 10 35
32 Andy 8 25
Rating
10
11
Mixing Operators
• Can we do this?!!
• Correct result:
12
Set Operations: Union
13
Set Operations: Intersection and Set
Difference
14
Examples on Set Operators
Sid Sname Rating Age
Sid Sname Rating Age
28 yuppy 9 35
22 Dustin 7 45
31 Lubber 8 55
31 Lubber 8 55
44 guppy 5 35
58 Rusty 10 35
58 Rusty 10 35
22 Dustin 7 45
28 yuppy 9 35
¿ 31
44
58
Lubber
guppy
Rusty
8
10
55
35
35
15
Examples on Set Operators (2)
Sid Sname Rating Age
Sid Sname Rating Age
28 yuppy 9 35
22 Dustin 7 45
31 Lubber 8 55
31 Lubber 8 55
44 guppy 5 35
58 Rusty 10 35
58 Rusty 10 35
¿ 31
58
Lubber
Rusty
8
10
55
35
16
Examples on Set Operators (3)
Sid Sname Rating Age
Sid Sname Rating Age
28 yuppy 9 35
22 Dustin 7 45
31 Lubber 8 55
− 31 Lubber 8 55
44 guppy 5 35
58 Rusty 10 35
58 Rusty 10 35
¿
Sid Sname Rating Age
22 Dustin 7 45
17
Introduction and Schemas
• Assumptions
18
Cross Product:
19
Example:
Sid Sname Rating Age Sid Bid Day
58 Rusty 10 35
20
Condition Join
• The most general version of the join operation accepts a join condition c
and a pair of relation instances as arguments, and returns a relation
instance
21
Condition Join Example
22
Equijoin
23
Natural Join
24
Examples
25
Examples (2)
• Find the names of sailors who have reserved at least one boat
• Find the names of Sailors who reserved a red boat or a green boat
• Find the names of Sailors who reserved a red AND a green boat
26
Examples (3)
• Find the sids of sailors with age over 20 who have not reserved a red
boat
27
Sid Bid Day
22 101 10/10/96
22 107 1/2/98
• Find the sids of sailors who reserved at least two different boats
28