relational algebra shiraj mohamed m | mis 1. relational algebra notation shiraj mohamed m | mis 2

Post on 25-Dec-2015

241 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Relational algebra

SHIR

AJ M

OH

AMED

M |

MIS

1

Relational algebra Notation

SHIR

AJ M

OH

AMED

M |

MIS

2

Unary Operations

Selection course = ‘Computing’ StudentsIn SQL:Select *From StudentsWhere course = ‘Computing’;

Projection stud#, name Students

In SQL:Select stud#, nameFrom Students;

Selection & Projection stud#, name ( course = ‘Computing’ Students)

In SQL:Select stud#, nameFrom studentsWhere course = ‘Computing’;

3

SHIR

AJ M

OH

AMED

M |

MIS

Binary Operations/Joins

Cartesian Product: Students X CoursesIn SQL: Select * From Students, Courses;

4

SHIR

AJ M

OH

AMED

M |

MIS

Rename

• RENAME operator (): Renames the input relation and attributes with a new relation name & attributes specified.

S(B1, B2, …, BN) (R)

Example,TEMP NAME, MAJOR(STUDENT)

STUD_INFO (FULL_NAME,M_DEPT ) TEMP

SHIR

AJ M

OH

AMED

M |

MIS

5

Renaming

TEMP DNO=5(EMPLOYEE)R(FIRSTNAME, LASTNAME, SALARY) FNAME, LNAME, SALARY (TEMP)

Example

SHIR

AJ M

OH

AMED

M |

MIS

6

Union and Set-Difference

• All of these operations take two input relations, which must be union-compatible:• Same number of fields.• Corresponding’ fields have the same type.

7SHIRAJ MOHAMED M | MIS

Set Operators

•Given two relations R1, R2 that are union-compatible, we have that R1 R2 returns the set of tuples that

are in R1 or R2. [UNION]∆R1 R2 returns the set of tuples that

are both in R1 and R2. [INTERSECTION]∆R1 - R2 returns the set of tuples that are

in R1, but not in R2. [SET DIFFERENCE]8

SHIR

AJ M

OH

AMED

M |

MIS

Set Operators

Name (FACULY) Name (STUDENT) Address (FACULY) Address (STUDENT) CrsCode (CLASS) - CrsCode (TRANSCRIPT)

9

SHIR

AJ M

OH

AMED

M |

MIS

Union

S S1 2

S1

S2

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.5 58 rusty 10 35.0

sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0

sid sname rating age

22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 44 guppy 5 35.0 28 yuppy 9 35.0

10SHIRAJ MOHAMED M | MIS

Set Difference

S1

S2

S S1 2

S2 – S1

sid sname rating age

22 dustin 7 45.0

31 lubber 8 55.5 58 rusty 10 35.0

sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0

sid sname rating age

22 dustin 7 45.0

sid sname rating age 28 yuppy 9 35.0 44 guppy 5 35.0

11SHIRAJ MOHAMED M | MIS

Joins

Three new join operators are introduced:• Left Outer Join (denoted as )

• Right Outer Join (denoted as )

• Full Outer Join (denoted as )

SHIRAJ MOHAMED M | MIS 12

Join…

SHIR

AJ M

OH

AMED

M |

MIS

13

Students ⋈ <stud# =200> Courses

In SQL: Select * From Students, CoursesWhere stud# = 200;

Left Outer Join

Left Outer Join : A <join condition> B

• ensures that all tuples in the in the relation A are present in the result set.

• The tuples in A without matching tuples in B are filled with null values for B’s attributes

SHIRAJ MOHAMED M | MIS 14

Left Outer Join - ExampleStudents Coursesstud# name course course# name100 Fred PH PH Pharmacy200 Dave CM CM Computing 400 Peter EN CH Chemistry

Students <course = course#> Courses

stud# Students.name course course# Courses.name

100 Fred PH PH Pharmacy200 Dave CM CM Computing400 Peter EN NULL NULL

15

SHIR

AJ M

OH

AMED

M |

MIS

Right Outer Join

Right Outer Join: A <join condition> B

• Reverse of left outer join. Retrieves all tuples of B and null values for attributes of A in non-matching tuples of B

SHIR

AJ M

OH

AMED

M |

MIS

16

Right Outer Join - ExampleStudents Coursesstud# name course course# name100 Fred PH PH Pharmacy200 Dave CM CM Computing 400 Peter EN CH Chemistry

Students <course = course#> Courses

stud# Students.name course course# Courses.name

100 Fred PH PH Pharmacy200 Dave CM CM ComputingNULL NULL NULL CH Chemistry

17

SHIR

AJ M

OH

AMED

M |

MIS

Combination of Unary and Join Operations

Students Coursesstud# name address course course# name100 Fred Aberdeen PH PH Pharmacy200 Dave Dundee CM CM Computing 300 Bob Aberdeen CM

Show the names of students (from Aberdeen) and the names Show the names of students (from Aberdeen) and the names of their coursesof their courses

R1= Students ⋈ <course=course#> CoursesR2= <address=“Aberdeen”> R1R3= <Students.name, Course.name> R2

Students.name Courses.nameFred PharmacyBob Computing

18

SHIR

AJ M

OH

AMED

M |

MIS

Full Outer Join

Full Outer Join: A <join condition> B

• ensures that all tuples of A and B are present in the result set

SHIR

AJ M

OH

AMED

M |

MIS

19

Exercise 1

• Query 1: List customers whose cred_lim is greater than £500.• Query 2: List customers whose cred_lim is greater than £500

and lives in London.

Example: Customer

SHIR

AJ M

OH

AMED

M |

MIS

20

Answers• Query 1: List customers whose cred_lim is greater than £500.

(cred_lim > 500)(customer)

• Query 2: List customers whose cred_lim is greater than £500 and lives

in London.(cred_lim>500) AND (city=London) (customer)

SHIR

AJ M

OH

AMED

M |

MIS

21

Exercise 2 Reserves

SailorsBoats

sid bid day

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

sid sname rating age

22 Jesly 7 45.0

31 Mishail 8 55.5 58 Raj 10 35.0

bid bname color 101 Interlake Blue 102 Interlake Red 103 Clipper Green 104 Marine Red

1.Find names of sailors who’ve reserved boat #1032.Find names of sailors who’ve reserved a red boat3.Find sailors who’ve reserved a red or a green boat4.Find sailors who’ve reserved a red and a green boat5. Find the names of sailors who’ve reserved all boats

SHIRAJ MOHAMED M | MIS 22

1.Find names of sailors who’ve reserved boat #103

• Solution 1: sname bidserves Sailors(( Re ) )

103

• Solution 2: sname bidserves Sailors( (Re ))

103

SHIR

AJ M

OH

AMED

M |

MIS

23

2.Find names of sailors who’ve reserved a red boat• Information about boat color only available in Boats; so need

an extra join:

sname color redBoats serves Sailors((

' ') Re )

A more efficient (???) solution:

sname(sid

((bid

(color'red '

Boats))Res)Sailors)

SHIR

AJ M

OH

AMED

M |

MIS

24

3.Find sailors who’ve reserved a red or a green boat• Can identify all red or green boats, then find sailors who’ve

reserved one of these boats:

( , (' ' ' '

))Tempboatscolor red color green

Boats

sname Tempboats serves Sailors( Re )

SHIR

AJ M

OH

AMED

M |

MIS

25

4.Find sailors who’ve reserved a red and a green boat• Previous approach won’t work! Must identify sailors who’ve

reserved red boats, sailors who’ve reserved green boats, then find the intersection (note that sid is a key for Sailors):

( , ((' '

) Re ))Tempredsid color red

Boats serves

sname Tempred Tempgreen Sailors(( ) )

( , ((' '

) Re ))Tempgreensid color green

Boats serves

SHIR

AJ M

OH

AMED

M |

MIS

26

5. Find the names of sailors who’ve reserved all boats• Uses division; schemas of the input relations to / must be

carefully chosen:

( , (,

Re ) / ( ))Tempsidssid bid

servesbid

Boats

sname Tempsids Sailors( )

To find sailors who’ve reserved all ‘Interlake’ boats:

/ (' '

) bid bname Interlake

Boats

.....

SHIR

AJ M

OH

AMED

M |

MIS

27

Aggregate Functions and Operations• Aggregation function takes a collection of values and returns a

single value as a result.avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values

• Aggregate operation in relational algebra G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E)

• 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

• Relation account grouped by branch-name:

branch-name g sum(balance) (account)

branch-name account-number balance

PerryridgePerryridgeBrightonBrightonRedwood

A-102A-201A-217A-215A-222

400900750750700

branch-name balance

PerryridgeBrightonRedwood

13001500700

Aggregate Functions • 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)

End… SHIR

AJ M

OH

AMED

M |

MIS

31

top related