Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2003-2008 Curt Hill Joins Revisited What is there beyond Natural Joins?

Similar presentations


Presentation on theme: "Copyright © 2003-2008 Curt Hill Joins Revisited What is there beyond Natural Joins?"— Presentation transcript:

1 Copyright © 2003-2008 Curt Hill Joins Revisited What is there beyond Natural Joins?

2 Copyright © 2003-2008 Curt Hill Ways to think of Join We have usually considered the Join as a sequence of algebra operations –Cartesian product –Selection –Optional project This is not always the best way, especially from an implementation perspective The alternative is the zipper view

3 Copyright © 2003-2008 Curt Hill The zipper approach Consider two files –Faculty The key is naid –Schedule The key is dept, number, section Another candidate key is naid, time –The join fields are naid If both files are sorted on the join field the join resembles a Match-Merge

4 Copyright © 2003-2008 Curt Hill The Match Merge The match merge is the means of updating a sorted master file with sorted transactions Read in one item from both, then do the following until done: –Transaction = Master Update the master Get new transaction –Master < Transaction Write master Read master –Transaction < Master Declare an error Read transaction

5 Match Merge Revisited The idea of the match merge is to make one pass through a master file and transaction file to do an update This only works if both are sorted and by the same key The same thing will work in database if both tables have an index for the joined field We will consider creating indices later Copyright © 2003-2008 Curt Hill

6 The Zipper Join 1024 a 1092 b 1233 c 1279 d 1092 v 1068 u 1024 t 1024 s 1024 r FacultySchedule 1092 w 1279 x 1279 y 1279 z

7 Copyright © 2003-2008 Curt Hill Join types The last picture suggests two types of joins: inner and outer What we have considered so far is the inner join –Only things that match on key are worth considering However, those things in either relation that match nothing in the other are also interesting –This is an outer join

8 Copyright © 2003-2008 Curt Hill Outer Join An outer join produces all the tuples of an inner join and some additional ones These include the unmatched items padded with null values This produces three types of outer joins: Left outer joins – only pad unmatched things in the left relation Right outer joins – only pad unmatched things in the right relation Full outer joins – pad unmatched things in the both relations

9 Copyright © 2003-2008 Curt Hill Outer Join 1024 a 1092 b 1233 c 1279 d 1092 v 1068 u 1024 t 1024 s 1024 r FacultySchedule 1092 w 1279 x 1279 y 1279 z

10 Copyright © 2003-2008 Curt Hill Join Results 1024 a 1279 d 1233 c 1024 r 1092 b1024 s 1024 t 1068 u 1279 x 1092 w 1092 v 1279 y 1279 z FacultySchedule 1024 a 1024 r 1024 a 1024 s 1024 a 1024 t 1092 b 1092 v 1092 b 1092 w 1279 d 1279 x 1279 d 1279 y 1279 d 1279 z Inner Join Result 1233 c - -- - 1068 u Padded faculty Padded schedule Left outer join is Inner union padded faculty Right outer join is Inner union padded schedule Full outer join is union of all three

11 Copyright © 2003-2008 Curt Hill Joins in SQL The Join keyword usually occurs in the FROM clause rather than in the Where The above query should connect faculty with classes they teach: Select * From Faculty Natural Join c_teach You may add additional conditions with a where A Natural join will only work with like named fields –If there are none, then a Cartesian product –Thus the current college database does not handle correctly

12 Copyright © 2003-2008 Curt Hill Un-natural outer joins There is a syntax to allow the same thing without the field names matching Leave out the Natural keyword and add the On clause: Select * From Faculty Join c_teach on (f_naid = ct_naid) Notice the lack of Where clause This is still an inner join and Inner may precede Join

13 Variants Oracle does not require the parentheses around the condition MySQL requires parenthesis SQL Server do not

14 Outers A Left, right or full may precede Join –These all assume an Outer Join A left will give all of the regular join plus any faculty who teach no classes –There are a few extras with nulls for fill The right will give no extras, since ct_naid is a foreign key to f_naid The way the keys are designed there are few differences with left/right in college db Copyright © 2003-2008 Curt Hill

15 Examples: A left Select count(*) From faculty left outer Join c_teach on (f_naid = ct_naid) A right Select count(*) From grades right outer Join students on (g_naid = s_id) A full Select count(*) From grades full outer Join students on (g_naid = s_id) Copyright © 2003-2008 Curt Hill

16 Tables Performing the Join in the From makes for different table count –Notice there was no comma separating the tables in the From Is there one table or two? –Both One –The join is done explicitly, not implicitly as is more often the case Two –The faculty and division qualifiers are still usable to represent where the fields came from in the resulting tuple


Download ppt "Copyright © 2003-2008 Curt Hill Joins Revisited What is there beyond Natural Joins?"

Similar presentations


Ads by Google