17
Relational Algebra Instructor: Mohamed Eltabakh [email protected] 1 Part II

Relational Algebra

  • Upload
    conley

  • View
    27

  • Download
    3

Embed Size (px)

DESCRIPTION

Relational Algebra. Part II. Instructor: Mohamed Eltabakh [email protected]. Announcements. Homework 1 is due NOW !!! Homework 2 will be out today (Nov. 8) and due on Nov. 15 Things to remember Phase 2 of the project is due Nov. 11 - PowerPoint PPT Presentation

Citation preview

Page 1: Relational Algebra

Relational Algebra

Instructor: Mohamed Eltabakh [email protected]

1

Part II

Page 2: Relational Algebra

Announcements

Homework 1 is due NOW !!!

Homework 2 will be out today (Nov. 8) and due on Nov. 15

Things to remember Phase 2 of the project is due Nov. 11 Make sure your Oracle account is working (if did not do so

yet) -- You will need it soon

CS3431 2

Page 3: Relational Algebra

Relational-Algebra Operators (Recap)

Set operators Union, Intersection, Difference

Selection & Projection & Extended Projection

Joins Natural, Theta, Outer join

Rename & Assignment

Duplicate elimination

Grouping & Aggregation

3

Page 4: Relational Algebra

Examples ofRelationships Among Operators

4

Page 5: Relational Algebra

Relationships Among Operators (I)

Intersect as Difference R ∩ S = R– (R–S)

Join as Cartesian Product + Select R ⋈C S = (σC (R X S))

Select is commutative σC2 (σC1 (R)) = σC1 (σC2 (R)) = σC1^C2 (R)

Order between Select & Project σC (πlist (R)) πlist (σC (R))

πlist (σC (R)) σC (πlist (R)) Only if “list” contains all columns needed by conditions C

5

Page 6: Relational Algebra

Relationships Among Operators (II)

Join is commutative R ⋈C S = S ⋈C R

Left and Right outer joins are not commutative

Order between Select & Join σR.x=“5” (R ⋈R.a = S.b S ) ((σR.x=“5” (R)) ⋈R.a = S.b S)

6

Page 7: Relational Algebra

Operations On Bags

7

Page 8: Relational Algebra

Operations on Bags

Most DBMSs allow relations to be bags (not limited to sets)

All previous relational algebra operators apply to both sets and bags Bags allow duplicates

Duplicate elimination operator converts a bag into a set

Some properties may hold for sets but not bags Example: R U R = R (True for sets, False for bags)

8

Page 9: Relational Algebra

Example Operations on Bags: Union: Consider two relations R and S that are union-

compatible

A B

1 2

3 4

1 2

RA B

1 2

3 4

5 6

S A B

1 2

1 2

1 2

3 4

3 4

5 6

R S

Suppose a tuple t appears in R m times, and in S n times.

Then in the union, t appears m + n times.

9

Page 10: Relational Algebra

Example Operations on Bags: Intersection: ∩

Consider two relations R and S that are union-compatible

A B

1 2

3 4

1 2

R

A B

1 2

3 4

5 6

S

A B

1 2

3 4

R ∩ S

Suppose tuple t appears in R m times, and in S n times. Then in intersection, t appears min (m, n) times.

10

Page 11: Relational Algebra

Example Operations on Bags: Difference: -

Suppose tuple t appears in R m times & in S n times. Then in R – S, t appears max (0, m - n) times.

A B

1 2

3 4

1 2

R

A B

1 2

3 4

5 6

S

A B

1 2

R – S

11

Page 12: Relational Algebra

cs3431

Project: πA1, A2, …, An (R)

πA1, A2, …, An (R) returns tuples in R, but only columns A1, A2, …, An.

A B C

1 2 5

3 4 6

1 2 7

1 2 8

R πA, B (R)

A B

1 2

3 4

1 2

1 2

R is a set, but πA, B (R) is a bag

Page 13: Relational Algebra

Exercise Query?

Find customer name with the largest loan from a branch in “NY” city

13

Page 14: Relational Algebra

Some Basic Rules for Algebraic Expressions

(For Better Performance)

14

Page 15: Relational Algebra

1- Joins vs. Cartesian Product

Use Joins instead of Cartesian products (followed by selection) R ⋈C S = (σC (R X S)) -- LHS is better

Intuition: There are efficient ways to do the L.H.S without going through the two-steps R.H.S

CS3431 15

Page 16: Relational Algebra

2- Push Selection Down Whenever possible, push the selection down

Selection is executed as early as possible

Intuition: Selection reduces the size of the data

Examples σC (πlist (R)) πlist (σC (R)) -- RHS is better

σR.x=“5” (R ⋈R.a = S.b S ) ((σR.x=“5” (R)) ⋈R.a = S.b S) -- RHS is better

CS3431 16

Page 17: Relational Algebra

3- Avoid Un-necessary Joins

Intuition: Joins can dramatically increase the size of the data

17

Find customers having account balance below 100 and loans above 10,000

R1 πcustomer_name (depositor ⋈ πaccount_number(σbalance <100 (account)))

R2 πcustomer_name (borrower ⋈ πloan_number(σamount >10,000 (loan)))

Result R1 ∩ R2

Better than joining the 4 relations and then selecting