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

Slides:



Advertisements
Similar presentations
4-1 Copyright  Oracle Corporation, All rights reserved. Types of Joins Equijoin Non-equijoin Outer join Self join.
Advertisements

BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Conceptual Database Design.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Extended SQL & The Relational Calculus.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
CS370 Spring 2007 CS 370 Database Systems Lecture 7 The Relational model.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Objects & Databases These.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen M:N Relationships & Bridge Classes These.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relationship Classes & N-ary Relationships.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Dependent & Identifying Relationships.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic Normal Forms These slides are licensed.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Conceptual State Constraints These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic Relational Algebra These slides.
Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen :1 Relationships These slides are licensed.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Union Subclasses & Disjoint 1:M Relationships.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic SQL These slides are licensed under.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational Mapping with Constraints &
4 Displaying Data from Multiple Tables Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Constraints These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen The Relational Model & Relational Mapping.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Advanced Relational Algebra These slides.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Object-Relational Database Programming.
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Using Data Structures in Embedded Programs.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Views These slides are licensed under.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Oracle CONNECT BY function JAVA WEB Programming. Emp 테이블의 내용 ( 상 / 하급자 계층구조 ) SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Inner Joins These slides are licensed.
11-1 © Prentice Hall, 2004 Chapter 11: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Functional Dependencies & Normalization.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
Relational Normalization Theory
Subqueries.
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Copyright © Ellis Cohen Consistency & Initialization
Lecture 16 : The Relational Data Model
Restricting and Sorting Data
Lecture 16 : The Relational Data Model
Copyright © Ellis Cohen
Presentation transcript:

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

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 © 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 © 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 © 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 © Ellis Cohen Natural Joins of 1:M Relationships

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

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

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

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