Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 38 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
38
Dung lượng
245,21 KB
Nội dung
DisplayingDatafrom Multiple
Tables
4
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć2
Schedule: Timing Topic
40 minutes Lecture
50 minutes Practice
90 minutes Total
Class Management Note:
Files required for lesson are:
Demonstration: l4cart.sql, l4region.sql, l4ejoin.sql, l4ojoin.sql
Practice: None
Displaying DatafromMultipleTables 4Ć3
Objectives
This lesson will cover how to obtain datafrom more than one table, using the
many different methods available.
At the end of this lesson, you should be able to
D Write SELECT statements to access datafrom more than one table using equality
and non-equality joins.
D View data that would not normally meet a join condition by using outer joins.
D Join a table to itself.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć4
Displaying DatafromMultipleTables 4Ć5
Overview
When datafrom more than one table in the database is required, a join condition is
used. Rows in one table may be joined to rows in another table according to common
values existing in corresponding columns, that is to say primary and foreign key
columns.
There are two main types of join conditions:
D Equijoins
D Non-equijoins
Additional join methods include the following:
D Outer joins
D Self joins
D Set operators
For more information about set operators, attend
Advanced SQL and SQL*Plus course.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć6
Class Management Note:
DEMO: l4cart.sql
PURPOSE: Point out to students that all rows are being joined since there is
no WHERE clause to join together the two tables.
Displaying DatafromMultipleTables 4Ć7
What Is a Cartesian Product?
When a join condition is invalid or omitted completely, the result is a Cartesian
Product, in which all combinations of rows will be displayed. All rows in the first
table are joined to all rows in the second table.
And Why Should You Care?
A Cartesian product tends to generate a large number of rows, and its result is rarely
useful. You should always include a valid join condition in a WHERE clause, unless
you have a specific need to combine all rows from all tables.
SQL> SELECT name, last_name
2 FROM s_dept, s_emp;
300 rows selected.
Class Management Note:
The S_EMP table contains 25 rows. The S_DEPT table contains 12 rows.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć8
Displaying DatafromMultipleTables 4Ć9
Simple Join Query
To display datafrom two or more related tables, write a simple join condition in the
WHERE clause.
Syntax
SELECT table.column, table.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
where: table.column denotes the table and column from which data is
retrieved.
table1.column1 = is the condition that joins (or relates)
table2.column2 the tables together.
Guidelines
D When writing a SELECT statement that joins tables, precede the column name
with the table name for clarity and to enhance database access.
D If the same column name appears in more than one table, then the column name
must be prefixed with the table name.
D To join tables together, you need a minimum of the number of join conditions
summarized as the number of tables minus one. Therefore, to join four tables, a
minimum of three joins would be required. This rule may not apply if your table
has a concatenated primary key, in which case more than one column is required
to uniquely identify each row.
For more information, see
Oracle7 Server SQL Language Reference Manual, “SELECT.”
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć10
Server
[...]... code smaller, therefore conserving memory DisplayingData from MultipleTables 4Ć27 4Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice Overview This practice is intended to give you practical experience extracting datafrom more than one table You will be required to join and restrict rows in the WHERE clause Practice Contents D Joining tables using an equijoin D Performing outer... representative DisplayingDatafromMultipleTables 4Ć21 Server É ÉÉ 4Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Returning Records with No Direct Match continued Example Display the sales representative name and employee number and the customer name for all customers Include the customer name even if the customer has not been assigned a sales representative SQL> 2 3 4 SELECT FROM WHERE... appear on one side of the expression—the side that has information missing It returns those rows from one table which have no direct match in the other table D A condition involving an outer join may not use the IN operator or be linked to another condition by the OR operator DisplayingDatafromMultipleTables 4Ć23 Server É ÉÉ 4Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Joining... SELECT statement D Table aliases should be meaningful D The table alias is only valid for the current SELECT statement DisplayingDatafromMultipleTables 4Ć17 4Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder NonĆEquijoin The relationship between the EMP and SALGRADE tables is a non-equijoin, in that no column in EMP corresponds directly to a column in SALGRADE The relationship is... Performing outer and self joins D Adding additional conditions Class Management Note: Duration: 50 minutes DisplayingDatafromMultipleTables 4Ć29 4Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 4 Use the S_EMP, S_DEPT, S_CUSTOMER, S_REGION, S_ORD, S_ITEM, and S_PRODUCT tables to complete the following exercises 1 Write a report containing each employee’s last name, department... Asia 45 5 Europe 50 1 North America 12 rows selected The requirement to qualify ambiguous column names is also applicable to columns that may be ambiguous in a SELECT or ORDER BY clause DisplayingDatafromMultipleTables 4Ć13 Server ÉÉ ÉÉ 4Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Simple Join Query continued Additional Search Conditions In addition to the join, you may have... s_dept.id s_dept.region_id = s_region.id s_emp.commission_pct > 0; NAME COMMISSION_PCT -North America 10 South America 12.5 Africa / Middle East 10 Asia 15 Europe 17.5 DisplayingData from MultipleTables 4Ć15 4Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Using Table Aliases Qualifying column names with table names can be very time consuming, particularly if table... works for Nagayama Giljum works for Nagayama Sedeghi works for Nagayama Nguyen works for Nagayama Dumas works for Nagayama 24 rows selected Class Management Note: See note on page 4-26 DisplayingData from MultipleTables 4Ć25 Class Management Note: Note refers to topic covered on page 4-25 Ask students to derive the name of Ngao’s boss (Velasquez) You want them to describe the process achieve the... respective department name displayed The rows of the S_EMP table are combined with the rows of the S_DEPT table, and rows are only returned if the values of S_EMP.DEPT_ID and S_DEPT.ID are equal DisplayingData from MultipleTables 4Ć11 Class Management Note: DEMO: l4region.sql PURPOSE: This script contains no aliases for the SELECT clause Compare this to the example on the facing page to see that the aliases... simplest Remember to specify the low value first and the high value last when using BETWEEN Table aliases have been specified for performance reasons, not because of possible ambiguity DisplayingData from MultipleTables 4Ć19 Class Management Note: DEMO: l4ejoin.sql PURPOSE: Show that only 14 of the 15 customers appear Sweet Rock Sports is not in this list because it has no sales representative Class . PL/SQL Using Procedure Builder4Ć4
Displaying Data from Multiple Tables 4Ć5
Overview
When data from more than one table in the database is required, a join condition. Using Procedure Builder4Ć8
Displaying Data from Multiple Tables 4Ć9
Simple Join Query
To display data from two or more related tables, write a simple join