Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Natural Joins These slides are licensed.

Similar presentations


Presentation on theme: "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Natural Joins These slides are licensed."— Presentation transcript:

1 1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Natural Joins These slides are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. For more information on how you may use them, please see http://www.openlineconsult.com/db

2 2 © Ellis Cohen 2001-2008 Overview of Lecture Natural Joins Natural Join of 1:M Relationships Natural Joins and Participation Natural Joins and Attributes Joining Multiple Tables

3 3 © Ellis Cohen 2001-2008 Natural Joins

4 4 © Ellis Cohen 2001-2008 Combining Tables empno ename deptno sal comm 7499ALLEN301600300 7654MARTIN3012501400 7698BLAKE302850 7839KING105000 7844TURNER3015000 7986STERN501500 Emps empno lockerno deskid 749950221AAXL 765473182BQRF 769861110TMLB 783910579JYZT 784420324GHHT 798661181ARFO OtherEmpData Suppose we want a query to combine this information: For each empno in common, we want the query result to include empno, ename, deptno, sal, comm, lockerno & deskid

5 5 © Ellis Cohen 2001-2008 Joined Tables empno ename deptno sal comm lockerno deskid 7499ALLEN30160030050221AAXL 7654MARTIN301250140073182BQRF 7698BLAKE30285061110TMLB 7839KING10500010579JYZT 7844TURNER301500020324GHHT 7986STERN50150061181ARFO Would it have been reasonable to have combined the tables in the database to start with? from Emps from OtherEmpData Emps OtherEmpData Relational Algebra Expression

6 6 © Ellis Cohen 2001-2008 Natural Join Classic Relational Algebra Emps OtherEmpData SQL-92 (also in Oracle 9i, but not 8i) SELECT * FROM (Emps NATURAL JOIN OtherEmpData)

7 7 © Ellis Cohen 2001-2008 Overlapping Matching Columns empno ename deptno sal comm 7499ALLEN301600300 7654MARTIN3012501400 7698BLAKE302850 7839KING105000 7844TURNER3015000 7986STERN501500 Emps empno lockerno deskid 749950221AAXL 765373182BQRF 783710579JYZT 798661181ARFO 769861110TMLB OtherEmpData empno ename deptno sal comm lockerno deskid 7499ALLEN30160030050221AAXL 7698BLAKE30285061110TMLB 7986STERN50150061181ARFO Emps OtherEmpData Joins the columns whose names are identical: empno Only includes tuples with matching values for empno 3 matches 3 tuples

8 8 © Ellis Cohen 2001-2008 Natural Joins of 1:M Relationships

9 9 © Ellis Cohen 2001-2008 Natural Join of 1:M Relationships empno ename addr deptno dname 7499ALLEN...30SALES 7654MARTIN…30SALES 7698BLAKE…30SALES 7839KING…10ACCOUNTING 7844TURNER…30SALES 7986STERN…50SUPPORT Emps Depts 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…50 Emps 6 matches 6 tuples

10 10 © Ellis Cohen 2001-2008 Projected Join of 1:M Relationship To just get each employee's name & department name, do SELECT ename, dname FROM (Emps NATURAL JOIN Depts) ENAME DNAME ------ ALLEN SALES MARTIN SALES BLAKE SALES KING ACCOUNTING TURNER SALES STERN SUPPORT empno ename addr deptno dname 7499ALLEN...30SALES 7654MARTIN…30SALES 7698BLAKE…30SALES 7839KING…10ACCOUNTING 7844TURNER…30SALES 7986STERN…50SUPPORT

11 11 © Ellis Cohen 2001-2008 Restricting Joined Result To just get each employee's name & department name, do SELECT ename, dname FROM (Emps NATURAL JOIN Depts) WHERE ename LIKE '%E%' ENAME DNAME ------ ALLEN SALES BLAKE SALES TURNER SALES STERN SUPPORT empno ename addr deptno dname 7499 E ALLEN...30SALES 7654MARTIN…30SALES 7698 E BLAKE…30SALES 7839KING…10ACCOUNTING 7844 E TURNER…30SALES 7986 E STERN…50SUPPORT WHERE clause is applied to the joined result

12 12 © Ellis Cohen 2001-2008 Joins Get Extended Information 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE…30 7839KING…10 7844TURNER…30 7986STERN…50 Emps Joins provide access to extended information. Suppose we want to know the name of KING's dept. Emps provides us with the # of KING's dept. The NATURAL JOIN connect's KING's tuple to the tuple of KING's dept, from which we can get the department name: SELECT dname FROM (Emps NATURAL JOIN Depts) WHERE ename = 'KING'

13 13 © Ellis Cohen 2001-2008 Natural Join Exercises Write SQL to Show the name of the department which SMITH is in Show the names of the employees in the SALES department

14 14 © Ellis Cohen 2001-2008 Natural Join Exercise Answer Show the name of the department which SMITH is in SELECT dname FROM (Emps NATURAL JOIN Depts) WHERE ename = 'SMITH' Show the name of the employees in the SALES department SELECT ename FROM (Emps NATURAL JOIN Depts) WHERE dname = 'SALES'

15 15 © Ellis Cohen 2001-2008 Natural Joins and Participation

16 16 © Ellis Cohen 2001-2008 Natural Joins with Optional Participation 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN...30 7654MARTIN…30 7698BLAKE… 7839KING…10 7844TURNER…30 7986STERN…50 Emps empno ename addr deptno dname 7499ALLEN...30SALES 7654MARTIN…30SALES 7839KING…10ACCOUNTING 7844TURNER…30SALES 7986STERN…50SUPPORT SELECT * FROM (Emps NATURAL JOIN Depts) Blake is unassigned to a department Blake is not included in the natural join 5 matches 5 tuples

17 17 © Ellis Cohen 2001-2008 Joining 1:M Relationships DeptEmployee works for deptno dname empno ename addr Emps empno ename addr deptno Depts deptno dname Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) Given that Depts and Emps represent the 1:M relationship between Dept and Employee, how does participation affect the # of tuples in their join?

18 18 © Ellis Cohen 2001-2008 Size of 1:M Joins If there are 1000 tuples in Emps 50 tuples in Depts How many tuples (min and max) could there be in the NATURAL JOIN of Emps and Depts? Dept Employee Dept Employee Dept Employee Dept Employee Consider each of these 4 cases 1 4 3 2

19 19 © Ellis Cohen 2001-2008 Answer: Size of 1:M Joins If there are 1000 tuples in Emps 50 tuples in Depts How many tuples (min and max) could there be in the NATURAL JOIN of Emps and Depts Dept Employee Dept Employee Dept Employee Dept Employee 1 4 3 2 1000..1000 50..10000..1000

20 20 © Ellis Cohen 2001-2008 Natural Joins and Attributes

21 21 © Ellis Cohen 2001-2008 Choosing Attribute Names Why use Emps( empno, ename, addr, deptno ) Depts( deptno, dname ) Why not use Emps( empno, name, addr, deptno ) Depts( deptno, name ) What happens if we try to do NATURAL JOIN of Emps & Depts?

22 22 © Ellis Cohen 2001-2008 Natural Join SQL-92 (also in Oracle 9i, but not 8i) SELECT * FROM (Emps NATURAL JOIN Depts) SQL-89 SELECT * FROM Emps, Depts WHERE Emps.deptno = Depts.deptno Explicitly specify which fields match up

23 23 © Ellis Cohen 2001-2008 Join Attribute Problem Suppose Depts names its primary key deptid, but Emps refers to it as deptno Write the SQL to get the name and department name of every employee

24 24 © Ellis Cohen 2001-2008 Join Attribute Answer Suppose Depts names its primary key deptid, but Emps refers to it as deptno Write the SQL to get the name and department name of every employee SELECT ename, dname FROM Emps, Depts WHERE Emps.deptno = Depts.deptid

25 25 © Ellis Cohen 2001-2008 Join and Group Suppose we want to find out the number of employees in each department SELECT deptno, count(*) FROM Emps GROUP BY deptno Suppose we want to show the deptno and the dname of each such department We need to join Emps with Depts in order to gain access to dname

26 26 © Ellis Cohen 2001-2008 Unique Attributes Simply adding dname to the SELECT clause will not work in many DB's (including Oracle) SELECT deptno, dname, count(*) FROM (Emps NATURAL JOIN Depts) GROUP BY deptno Perhaps this should work, since deptno  dname (i.e. each deptno determines a single dname value) However, this doesn't work in many SQL's since dname is neither in the GROUP BY list, nor an aggregate function! How do we fix this?

27 27 © Ellis Cohen 2001-2008 Grouping w Unique Attributes SELECT deptno, dname, count(*) FROM (Emps NATURAL JOIN Depts) GROUP BY deptno SELECT deptno, dname, count(*) FROM (Emps NATURAL JOIN Depts) GROUP BY deptno, dname

28 28 © Ellis Cohen 2001-2008 Joining Multiple Tables

29 29 © Ellis Cohen 2001-2008 Multiple Join Exercise Items Styles itemsku size color stylecode stylenam styledate catid Categories catid catnam List the itemskus of all items in the category 'Mens Underwear'

30 30 © Ellis Cohen 2001-2008 Multiple Join Exercise Answer Items Styles itemsku size color stylecode stylenam styledate catid Categories catid catnam List the itemskus of all items in the category 'Mens Underwear' SELECT itemsku FROM (Categories NATURAL JOIN (Styles NATURAL JOIN Items)) WHERE catnam = 'Mens Underwear ' Does the order in which the joins are done matter?

31 31 © Ellis Cohen 2001-2008 Join Order Items Styles itemsku size color stylecode stylenam styledate catid Categories catid catnam SELECT itemsku FROM ( Categories NATURAL JOIN (Items NATURAL JOIN Styles) ) WHERE catnam = 'Mens Underwear ' For NATURAL JOINs, join order doesn't matter SELECT itemsku FROM ( (Categories NATURAL JOIN Styles) NATURAL JOIN Items ) WHERE catnam = 'Mens Underwear ' Equivalent

32 32 © Ellis Cohen 2001-2008 Joins & Redundant Relationships TeamPlayerChild has Teams teamnam city pssno teamnam Players Children cssno pssno teamnam For each child, list the city of the child's parent's team parent's team SELECT cssno, city FROM (Teams NATURAL JOIN Children) Only requires 2-way join

33 33 © Ellis Cohen 2001-2008 Joins without Redundancy TeamPlayerChild has Teams teamnam city pssno teamnam Players Children cssno pssno SELECT cssno, city FROM (Teams NATURAL JOIN (Players NATURAL JOIN Children)) Requires slower 3-way join TRADEOFF: Maintainability vs. Performance Conceptual Design should NOT be Redundant If necessary, add redundant reference during relational mapping For each child, list the city of the child's parent's team


Download ppt "1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Natural Joins These slides are licensed."

Similar presentations


Ads by Google