07.04 joins

14

Click here to load reader

Upload: bishal-ghimire

Post on 06-Jul-2015

55 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: 07.04 joins
Page 2: 07.04 joins

NATURAL JOIN

Notation : ⋈

Result schema same as that of cross-product.

Fewer tuples than cross-product.

Filters tuples not satisfying the join condition.

Enforces equality on all attributes with same name

Eliminate one copy of duplicate attributes

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

Page 3: 07.04 joins

NATURAL-JOIN OPERATION: MOTIVATION

Joins are compound operators involving cross product, selection, and (sometimes) projection.

Most common type of join is a “natural join” (often just called “join”). R ⋈ S conceptually is: Compute R X S

Select rows where attributes that appear in both relations have equal values

Project all unique atttributes and one copy of each of the common ones.

Note: Usually done much more efficiently than this.

Useful for putting “normalized” relations back together.

Page 4: 07.04 joins

NATURAL-JOIN OPERATION: MOTIVATION

Very often, we have a query and the answer is not

contained in a single relation. For example, I might

wish to know where Amol banks. The classic

relational algebra way to do such queries is a cross

product, followed by a selection which tests for

equality on some pair of fields.

σ borrower.lnumber = loan.lnumber (borrower x loan)

While this works…

• it is unintuitive

• it requires a lot of memory

• the notation is cumbersome

Page 5: 07.04 joins

R1 S1 =

Cust_name Lnumber

Ravi 1234

Amol 3421

Lnumber Branch

1234 Kathmandu

3421 Pokhara

Cust_name Lnumber Branch

Ravi 1234 Kathmandu

Amol 3421 Pokhara

σ borrower.lnumber = loan.lnumber (borrower x loan)

Page 6: 07.04 joins

sID sName GPA HS sID sName major dec

Student Apply

Q : Names and GPAs of students with HS >1000 who

applied to CS and were rejected

cName State Enr

College

π sName , GPA

( σ HS > 1000 ^ Major =„cs‟ ^ dec = „R‟

( Student ⋈ Apply ) )

Page 7: 07.04 joins

sID sName GPA HS sID sName major dec

Student Apply

Q : Names and GPAs of students with HS >1000 who

applied to CS at collage with Enr > 2000 and were

rejected

cName State enr

College

π sName , GPA

( σ HS > 1000 ^ Major =„cs‟ ^ dec = „R‟ ^ Enr > 2000

( Student ⋈ Apply ⋈ College ) )

Page 8: 07.04 joins

Exp1 ⋈ Exp2 ≡

∏ Schema (E1) U Schema(E2)

(σ E1.A1 = E2.A1 ^ E1.A2=E2.A2 ^ …

( Exp1 x Exp2 ) )

Page 9: 07.04 joins

Name EmpId DeptName

Harry 3415 Finance

Sally 2241 Sales

George 3401 Finance

Harriet 2202 Sales

DeptName Manager

Finance George

Sales Harriet

Production Charles

Name EmpId DeptName Manager

Harry 3415 Finance George

Sally 2241 Sales Harriet

George 3401 Finance George

Harriet 2202 Sales Harriet

Employee

Dept

Employee ⋈ Dept

Page 10: 07.04 joins
Page 11: 07.04 joins

•First we note which attributes the two relations have

in commn

•There are two rows in s that match our first row in r,

(in the relevant attributes) so both are joined to our

first row…

Page 12: 07.04 joins

there are no rows in s that match our second row in

r, so do nothing…

there are no rows in s that match our third row in r,

so do nothing

Page 13: 07.04 joins

There are two rows in s that match our fourth row in

r, so both are joined to our fourth row…

Page 14: 07.04 joins

There is one row that matches our fifth row in r,.. so

it is joined to our fifth row and we are done!