0% found this document useful (0 votes)
5 views28 pages

Chapter 4 Relational Algebra

Uploaded by

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

Chapter 4 Relational Algebra

Uploaded by

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

Relational Algebra

Chapter 4
What is it?
• Theoretical Foundations of Queries
• Questions to the Database regarding Data

• Queries in Relational Algebra are formulated using a collection of


operators
• Examples

2
Introduction and Schemas

• Assumptions

Sailors Reserves Boats


Sid Sname Rating Age Bid Bname color

22 Dustin 7 45 Sid Bid Day 101 Interlake Blue

31 Lubber 8 55 22 101 10/10/96 102 Interlake red

58 Rusty 10 35 58 103 11/12/96 103 Clipper Green

32 Andy 8 25 104 Marine red

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

• Works on the horizontal dimension (tuples)


Sid Sname Rating Age

22 Dustin 7 45

31 Lubber 8 55

58 Rusty 10 35

32 Andy 8 25

17 Popeye 10 40

5
Examples on Selection

Sid Sname Rating Age Sid Sname Rating Age

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)

Sid Sname Rating Age Sid Sname Rating Age

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 ()

• Alias for relation names and/or column names

• 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

• Works on the vertical dimension (attributes)


• Will only show unique values.

Sid Sname Rating Age

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

• Remember: Unique values only!

Rating

10

11
Mixing Operators

• More useful when mixing selections and projections


• Example
• Get sailor names whose age is greater than 40.

• ------------------ R’ only sname

• 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

Sid Sname Rating Age

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

Sid Sname Rating Age

¿ 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

Sailors Reserves Boats


Sid Sname Rating Age Bid Bname color

22 Dustin 7 45 Sid Bid Day 101 Interlake Blue

31 Lubber 8 55 22 101 10/10/96 102 Interlake red

58 Rusty 10 35 58 103 11/12/96 103 Clipper Green

32 Andy 8 25 104 Marine red

18
Cross Product:

19
Example:
Sid Sname Rating Age Sid Bid Day

22 Dustin 7 45 22 101 10/10/96

31 Lubber 8 55 × 58 103 11/12/96

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

• A common special case of the join operation R S is when the join


condition consists solely of equalities of the form R.name1 =
S.name2, that is, equalities between two fields in R and S.
• The schema of the result of an equijoin contains the fields of R (with
the same names and domains as in R) followed by the fields of
S that do not appear in the join conditions.

23
Natural Join

• A further special case of the join operation R S is an equijoin in which


equalities are specified on all fields having the same name in R and S.
• In this case, we can simply omit the join condition.

24
Examples

• Find the names of Sailors who have reserved boat id = 103

• Find the names of Sailors who reserved a red boat

• Find the color of boats reserved by Rusty

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

Examples (4) 58 103 11/12/96

22 107 1/2/98

• Find the sids of sailors who reserved at least two different boats

Sid Bid Day Sid’ Bid’ Day’

22 101 10/10/96 22 101 10/10/96

22 101 10/10/96 58 103 11/12/96

22 101 10/10/96 22 107 1/2/98

58 103 11/12/96 22 101 10/10/96

58 103 11/12/96 58 103 11/12/96

58 103 11/12/96 22 107 1/2/98

22 107 1/2/98 22 101 10/10/96

22 107 1/2/98 58 103 11/12/96

22 107 1/2/98 22 107 1/2/98

28

You might also like