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

2 2 © Ellis Cohen 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 Natural Joins

4 4 © Ellis Cohen Combining Tables empno ename deptno sal comm 7499ALLEN MARTIN BLAKE KING TURNER STERN Emps empno lockerno deskid AAXL BQRF TMLB JYZT GHHT ARFO 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 Joined Tables empno ename deptno sal comm lockerno deskid 7499ALLEN AAXL 7654MARTIN BQRF 7698BLAKE TMLB 7839KING JYZT 7844TURNER GHHT 7986STERN ARFO 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 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 Overlapping Matching Columns empno ename deptno sal comm 7499ALLEN MARTIN BLAKE KING TURNER STERN Emps empno lockerno deskid AAXL BQRF JYZT ARFO TMLB OtherEmpData empno ename deptno sal comm lockerno deskid 7499ALLEN AAXL 7698BLAKE TMLB 7986STERN ARFO 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 Natural Joins of 1:M Relationships

9 9 © Ellis Cohen 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 MARTIN… BLAKE… KING… TURNER… STERN…50 Emps 6 matches 6 tuples

10 10 © Ellis Cohen 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 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 Joins Get Extended Information 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN MARTIN… BLAKE… KING… TURNER… STERN…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 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 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 Natural Joins and Participation

16 16 © Ellis Cohen Natural Joins with Optional Participation 30SALES 10ACCOUNTING 50SUPPORT deptno dname Depts empno ename addr deptno 7499ALLEN MARTIN… BLAKE… 7839KING… TURNER… STERN…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 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 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

19 19 © Ellis Cohen 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

20 20 © Ellis Cohen Natural Joins and Attributes

21 21 © Ellis Cohen 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 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 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 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 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 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 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 Joining Multiple Tables

29 29 © Ellis Cohen 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 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 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 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 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