4 displaying data from multiple tables

Download 4 Displaying Data from  Multiple Tables

Post on 19-Jan-2016

47 views

Category:

Documents

2 download

Embed Size (px)

DESCRIPTION

4 Displaying Data from Multiple Tables. Important Legal Notice: Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp. For further information, visit www.oracle.com - PowerPoint PPT Presentation

TRANSCRIPT

  • 4 Displaying Data from Multiple TablesImportant Legal Notice:Materials on this lecture are from a book titled Oracle Education by Kochhar, Gravina, and Nathan (1999), published by Oracle Corp.For further information, visit www.oracle.com This presentation must be used for only education purpose for students at Central Washington University which is a member of Oracle Academic Initiatives (OAI) and has used Oracle systems for HRIS & Accounting Systems as a database platform embedded on its PeopleSoft ERP system, since 1999.

  • ObjectivesAfter completing this lesson, you should be able to do the following:Write SELECT statements to access data from more than one table using equality and nonequality joinsView data that generally does not meet a join condition by using outer joinsJoin a table to itself

  • Obtaining Data from Multiple TablesEMPNOENAME. . . DEPTNO --------------------. . . ------------ 7839KING. . .10 7698BLAKE. . .30 . . . 7934MILLER. . .10EMPDEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTONDEPTEMPNODEPTNOLOC -------------------------------------- 783910NEW YORK 769830CHICAGO 778210NEW YORK 756620DALLAS 765430CHICAGO 749930CHICAGO . . . 14 rows selected.

  • What is a Join?SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column1 = table2.column2;Write the join condition in the WHERE clause.Prefix the column name with the table name when the same column name appears in more than one table.Use a join to query data from more than one table.

  • Cartesian Product (bad guy)A Cartesian product is formed when:A join condition is omittedA join condition is invalidAll rows in the first table are joined to all rows in the second tableTo avoid a Cartesian product, always include a valid join condition in a WHERE clause.

  • Generating a Cartesian ProductEMPNOENAME. . . DEPTNO --------------------. . . ------------ 7839KING. . .10 7698BLAKE. . .30 . . . 7934MILLER. . .10EMP (14 rows)DEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTONDEPT (4 rows)ENAMEDNAME ----------------------- KINGACCOUNTING BLAKEACCOUNTING . . . KINGRESEARCH BLAKERESEARCH . . . 56 rows selected.Cartesian product: 14*4=56 rows

  • Types of JoinsEquijoinNon-equijoinOuter joinSelf join

  • What is an Equijoin?EMPNOENAMEDEPTNO -------------------- -----------7839KING10 7698BLAKE30 7782CLARK10 7566JONES20 7654MARTIN30 7499ALLEN30 7844TURNER30 7900JAMES30 7521WARD30 7902FORD20 7369SMITH20 . . . 14 rows selected.EMPDEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 30SALESCHICAGO 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 20RESEARCHDALLAS 20RESEARCHDALLAS . . . 14 rows selected.DEPTForeign keyPrimary key

  • Retrieving Records with EquijoinsSQL>SELECT emp.empno,emp.ename,emp.deptno,2 dept.deptno, dept.loc3FROM emp, dept4WHERE emp.deptno = dept.deptno;EMPNOENAME DEPTNO DEPTNOLOC --------------------- ------------ -----------------------------7839KING1010NEW YORK 7698BLAKE3030CHICAGO 7782CLARK1010NEW YORK 7566JONES2020DALLAS . . . 14 rows selected.

  • Qualifying Ambiguous Column NamesUse table prefixes to qualify column names that are in multiple tables. Improve performance by using table prefixes. Distinguish columns that have identical names but reside in different tables using column aliases.

  • Additional Search Conditions Using the AND OperatorEMPNOENAMEDEPTNO -------------------- -----------7839KING10 7698BLAKE30 7782CLARK10 7566JONES20 7654MARTIN30 7499ALLEN30 7844TURNER30 7900JAMES30 7521WARD30 7902FORD20 7369SMITH20 . . . 14 rows selected.EMPDEPTNODNAME LOC ------------------------------- ------------ 10ACCOUNTINGNEW YORK 30SALESCHICAGO 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 30SALESCHICAGO 20RESEARCHDALLAS 20RESEARCHDALLAS . . . 14 rows selected.DEPT

  • Using Table AliasesSQL>SELECT emp.empno,emp.ename,emp.deptno,2 dept.deptno, dept.loc3FROM emp, dept4WHERE emp.deptno=dept.deptno;SQL>SELECT e.empno,e.ename, e.deptno,2 d.deptno, d.loc3FROM emp e, dept d4WHERE e.deptno = d.deptno;Simplify queries by using table aliases.

  • Joining More Than Two TablesCUSTOMERNAMECUSTID --------------- ------------JOCKSPROTS100 TKB SPORT SHOP101 VOLLYRITE102 JUST TENNIS103 K+T SPORTS105 SHAPE UP106 WOMENS SPORTS107 . . . . . . 9 rows selected.ORDCUSTIDORDID ----------- --------- 101610 102611 104612 106601 102602 106 106 . . . 21 rowsITEMORDIDITEMID --------------------- 6103 6111 6121 6011 6021. . . 64 rows selected.

  • Non-EquijoinsEMPNOENAMESAL -------------------- -----------7839KING5000 7698BLAKE2850 7782CLARK2450 7566JONES2975 7654MARTIN1250 7499ALLEN1600 7844TURNER1500 7900JAMES950 . . . 14 rows selected.EMPGRADELOSALHISAL -------------------- -----------17001200 212011400 314012000 420013000 530019999 SALGRADESalary in the EMP table is between low salary and high salary in the SALGRADE table

  • Retrieving Records with Non-EquijoinsSQL> SELECT e.ename, e.sal, s.grade2 FROM emp e, salgrade s3 WHEREe.sal4 BETWEEN s.losal AND s.hisal;ENAME SAL GRADE ------------------------------ JAMES9501 SMITH8001 . . . 14 rows selected.

  • Outer JoinsENAME DEPTNO ----------- ------------ KING 10 BLAKE 30 CLARK 10 JONES 20 . . .DEPTNO DNAME ------------ ------------------------- 10 ACCOUNTING 30 SALES 10 ACCOUNTING 20 RESEARCH . . . 40 OPERATIONSEMPDEPTNo employee in the OPERATIONS department

  • Outer JoinsYou use an outer join to also see rows that do not usually meet the join condition.Outer join operator is the plus sign (+)SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column(+) = table2.column;SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column = table2.column(+);

  • Using Outer JoinsSQL>SELECT e.ename, e.deptno,d.dname2FROMemp e, dept d 3WHEREe.deptno(+) = d.deptno4ORDER BYe.deptno;ENAMEDEPTNODNAME-------------------------------KING10ACOUNTINGCLARK10ACCOUNTING . . .40OPERATIONS15 rows selected.

  • Self JoinsEMPNOENAMEMGR -------------------- -----------7839KING 7698BLAKE7839 7782CLARK7839 7566JONES7839 7654MARTIN7698 7499ALLEN7698EMP(WORKER Table)EMPNOENAME ----------- ------------ 7839KING 7839KING 7839KING 7698BLAKE 7698BLAKE EMP(MANAGER Table)MGR in the WORKER table is equal to EMPNO in the MANAGER table

  • Joining a Table to ItselfSQLSELECT worker.ename | | works for | | manager.ename 2FROM emp worker, emp manager3WHERE worker.mgr = manager.empno;WORKER.ENAME | | WORKSFOR | | MANAG-------------------------------------------------------------BLAKE works for KINGCLARK works for KINGJONES works for KINGMARTIN works for BLAKE. . .13 rows selected

  • SummarySELECTtable1.column, table2.columnFROMtable1, table 2WHEREtable1.column1 = table2.column2EquijoinNon-equijoinOuter joinSelf join

  • Practice OverviewJoining tables using an equijoinPerforming outer and self joinsAdding conditions

    Lesson Aim This lesson covers how to obtain data from more than one table, using the different methods available.Data from Multiple Tables

    Somet

Recommended

View more >