displaying data from multiple tables

Download Displaying Data  from  Multiple Tables

Post on 08-Feb-2016

23 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Displaying Data from Multiple Tables. Objectives. After 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 joins - PowerPoint PPT Presentation

TRANSCRIPT

Displaying Data from Multiple TablesIntroduction to Oracle: MySQL and PL/MySQL 4-#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 itselfIntroduction to Oracle: MySQL and PL/MySQL 4-#

employee_nbr dept_nbr location------------ -------- -------- 7839 10 New York 7698 30 Chicago 7782 10 New York 7566 20 Dallas 7654 30 Chicago 7499 30 Chicago...14 rows selected.Obtaining Data from Multiple TablesEmployee Department employee_nbr name ... dept_nbr------ ------ ... ------ 7839 King ... 10 7698 Blake ... 30 ... 7934 Miller ... 10dept_nbr dept_name location -------- --------- -------- 10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations BostonIntroduction to Oracle: MySQL and PL/MySQL 4-#What Is a Join?Use a join to query data from more than one table.

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.SELECTtable1.column, table2.columnFROMtable1, table2WHEREtable1.column1 = table2.column2;Introduction to Oracle: MySQL and PL/MySQL 4-#Cartesian ProductA 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.Introduction to Oracle: MySQL and PL/MySQL 4-#Generating a Cartesian Productname dept_name------ ----------King AccountingBlake Accounting ...King ResearchBlake Research...56 rows selected.Employee (14 rows) Department(4 rows) employee_nbr name ...dept_nbr----------- ----- ...-------- 7839 King ... 10 7698 Blake ... 30 ... 7934 Miller... 10dept_nbr dept_name location -------- ------- -------- 10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations BostonCartesianproduct: 14*4=56 rowsIntroduction to Oracle: MySQL and PL/MySQL 4-#Types of JoinsEquijoinNon-equijoinOuter joinSelf joinIntroduction to Oracle: MySQL and PL/MySQL 4-#

What Is an Equijoin?EmployeeDepartment employee_nbr name dept_nbr ------------ ---- -------- 7839 King 10 7698 Blake 30 7782 Clark 10 7566 Jones 20 7654 Martin 30 7499 Allen 30 7844 Turner 30 7900 James 30 7521 Ward 30 7902 Ford 20 7369 Smith 20 ... 14 rows selected.dept_nbr dept_name location -------- ---------- -------- 10 Accounting New York 30 Sales Chicago 10 Accounting New York 20 Research Dallas 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 20 Research Dallas 20 Research Dallas...14 rows selected.Foreign keyPrimary keyIntroduction to Oracle: MySQL and PL/MySQL 4-# Retrieving Records with EquijoinsMySQL> SELECT employee.employee_nbr,employee.name, -> employee.dept_nbr, -> dept.dept_nbr, dept.location -> FROM employee, dept -> WHERE employee.dept_nbr=dept.dept_nbr;employee_nbr name dept_nbr dept_nbr location----------- ------- -------- -------- ---------- 7839King10 10 New York 7698 Blake 30 30 Chicago 7782Clark10 10 New York 7566 Jones20 20 Dallas...14 rows selected.Introduction to Oracle: MySQL and PL/MySQL 4-#Inner Join SyntaxThe Keyword INNER is optional MySQL> SELECT employee_nbr, name, -> department.dept_nbr, location -> FROM employee [INNER] JOIN department -> ON employee.dept_nbr = department.dept_nbr;Introduction to Oracle: MySQL and PL/MySQL 4-#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 by using column aliases.Introduction to Oracle: MySQL and PL/MySQL 4-#Additional Search ConditionsUsing the AND Operator Employee Department employee_nbr name dept_nbr------------ ---- -------- 7839 King 10 7698 Blake 30 7782 Clark 10 7566 Jones 20 7654 Martin 30 7499 Allen 30 7844 Turner 30 7900 James 30 7521 Ward 30 7902 Ford 20 7369 Smith 20...14 rows selected.dept_nbr dept_name location -------- ----------------- 10 Accounting New York 30 Sales Chicago 10 Accounting New York 20 Research Dallas 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 20 Research Dallas 20 Research Dallas...14 rows selected.Introduction to Oracle: MySQL and PL/MySQL 4-#Using AND with Composite Keys If the Primary key is a composite key use the AND operator in the ON clause to set the composite key columns in equal to the foreign key columns. MySQL> SELECT customer_case_id, orders.order_id, -> payment_id, payment_amt -> FROM orders INNER JOIN payments -> ON orders.cust_id = payments.cust_id -> AND orders.order_id = payments_order_id;Introduction to Oracle: MySQL and PL/MySQL 4-#Using Table AliasesSimplify queries by using table aliases.MySQL> SELECT employee.employee_nbr,employee.name, -> employee.dept_nbr, -> dept.dept_nbr, dept.location -> FROM employee, dept -> WHERE employee.dept_nbr = dept.dept_nbr;MySQL> SELECT employee_nbr, e.name, e.dept_nbr, -> d.dept_nbr, d.location -> FROM employee e, dept d -> WHERE e.dept_nbr = d.dept_nbr; Introduction to Oracle: MySQL and PL/MySQL 4-#Joining More Than Two TablesNamecust_id------------------Jocksports 100TKB Sport Shop 101Vollyrite 102Just Tennis 103K+T Sports 105Shape Up 106Women's Sports 107... ...9 rows selected.customer cust_id order_id------- -------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605 ... 21 rows selected.orders order_id item_ID ------ ------- 610 3 611 1 612 1 601 1 602 1 ...64 rows selected. itemcust_id order_id payment_amt------- -------- ----------- 100 610 1050 103 601 1230 106 604 1155 paymentsIntroduction to Oracle: MySQL and PL/MySQL 4-#Joining More Than Two TablesIf you have N tables to join you will need at least N-1 join statements MySQL> SELECT customer.cust_id, name, -> orders.order_id, item_id -> FROM customer JOIN orders -> ON customer.cust_id = orders.cudt_id -> JOIN item -> ON orders.order_id = item.order_id;Introduction to Oracle: MySQL and PL/MySQL 4-#Non-EquijoinsEmployeeSalary_gradeemployee_nbr name salary----------- ----- ---- 7839 King 5000 7698 Blake 2850 7782 Clark 2450 7566 Jones 2975 7654 Martin 1250 7499 Allen 1600 7844 Turner 1500 7900 James 950...14 rows selected.Grade low_salary high_salary----- ---------- -----------1 700 12002 1201 14003 1401 20004 2001 30005 3001 9999salary in the employee table is between low salary and high salary in the salary_gradetableIntroduction to Oracle: MySQL and PL/MySQL 4-#Retrieving Records with Non-Equijoinsname salary grade---------- --------- ---------James 950 1Smith 800 1Adams 1100 1...14 rows selected.MySQL> SELECT name, salary, grade -> FROMemployee, salary_grade -> WHERE salary -> BETWEEN low_salary AND high_salary;Introduction to Oracle: MySQL and PL/MySQL 4-#

Outer JoinsEmployeeDepartmentNo employee in theOperations departmentname dept_nbr----- ------King 10Blake 30Clark 10Jones 20...

dept_nbr dept_name------ ---------10 Accounting30 Sales10 Accounting20 Research...40 OperationsIntroduction to Oracle: MySQL and PL/MySQL 4-#Outer JoinsYou use an outer join to also see rows that do not usually meet the join condition.Outer joins require the keyword LEFT or RIGHT.RIGHT or LEFT keyword specify the table from which the missing data is to come The only difference between a RIGHT and LEFT outer join ... ... ...SELECTtable1.column, table2.columnFROMtable1 LEFT [OUTER] JOIN table2ONtable1.column = table2.column;SELECTtable1.column, table2.columnFROMtable1 RIGHT [OUTER] JOIN table2ONtable1.column = table2.column;Introduction to Oracle: MySQL and PL/MySQL 4-#Using Outer JoinsMySQL> SELECT name, department.dept_nbr, dept_name -> FROM employee RIGHT OUTER JOIN department -> ON employee.dept_nbr = department.dept_nbr -> ORDER BY employee.dept_nbr;name dept_nbr dept_name--------- --------- -------------King 10 AccountingClark 10 Accounting... 40 Operations15 rows selected.Introduction to Oracle: MySQL and PL/MySQL 4-#Self JoinsEmployee(Worker)Employee (Manager)MGR in the Worker table is equal to employee_nbr in the Manager tableemployee_nbrname MGR---------------- --- 7839King 7698Blake7839 7782Clark7839 7566Jones7839 7654Martin7698 7499Allen7698employee_nbrname----------------

7839King 7839King 7839King 7698Blake 7698BlakeIntroduction to Oracle: MySQL and PL/MySQL 4-#Joining a Table to ItselfWORKER.name||'WORKSFOR