Tải bản đầy đủ (.pdf) (38 trang)

Tài liệu displaying data from Multiple tables docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (218.89 KB, 38 trang )

Displaying Data from Multiple
Tables
4
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć2
Displaying Data from Multiple Tables 4Ć3
Objectives
This lesson will cover how to obtain data from 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 data from 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 Data from Multiple Tables 4Ć5
Overview
When data from 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
Displaying Data from Multiple Tables 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.
Introduction to Oracle: SQL and PL/SQL 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 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
ÉÉÉÉ
Displaying Data from Multiple Tables 4Ć11
Simple Join Query
continued
Equijoin
In order to determine the name of an employee’s department, you compare the value
in the DEPT_ID column in the S_EMP table with the ID values in the S_DEPT table.
The relationship between the S_EMP and S_DEPT tables is an equijoin, that is
values in the DEPT_ID column on both tables must be equal. Frequently, these
columns are primary and foreign key complements.

Example
Join together the employee and department tables to display the employee name,
department number, and department name.
SQL> SELECT s_emp.last_name, s_emp.dept_id,
2 s_dept.name
3 FROM s_emp, s_dept
4 WHERE s_emp.dept_id = s_dept.id;
LAST_NAME DEPT_ID NAME
------------ ------- --------------------
Velasquez 50 Administration
Ngao 41 Operations
Nagayama 31 Sales
Quick-To-See 10 Finance
Ropeburn 50 Administration
Urguhart 41 Operations
Menchu 42 Operations
Biri 43 Operations
Catchpole 44 Operations
Havel 45 Operations
Magee 31 Sales
Giljum 32 Sales
Sedeghi 33 Sales
...
25 rows selected.
Every employee now has their 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.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć12
Displaying Data from Multiple Tables 4Ć13
Simple Join Query

continued
Qualifying Ambiguous Column Names
You need to qualify the names of the columns in the WHERE clause with the table
name to avoid ambiguity. Without the table prefixes, the ID column could be from
either the S_DEPT or the S_EMP table. It is necessary to add the table prefix to
execute your query.
If there are no names that are the same between the two tables, then there is no need
to qualify the columns. However, you will gain improved performance by using the
table prefix.
Example
Display the department number, region number, and region name for all departments.
SQL> SELECT s_dept.id ”Department ID”,
2 s_region.id ”Region ID”,
3 s_region.name ”Region Name”
4 FROM s_dept, s_region
5 WHERE s_dept.region_id = s_region.id;
Department ID Region ID Region Name
------------- ---------- ---------------------------
10 3 Africa / Middle East
31 1 North America
32 2 South America
33 3 Africa / Middle East
34 4 Asia
35 5 Europe
41 1 North America
42 2 South America
43 3 Africa / Middle East
44 4 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.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder4Ć14
Server
ÉÉ ÉÉ
Displaying Data from Multiple Tables 4Ć15
Simple Join Query
continued
Additional Search Conditions
In addition to the join, you may have additional criteria for your WHERE clause.
Since the join is required to obtain the matches, you need to add your additional
conditions by using the AND operator. Table aliases help to keep SQL code smaller,
therefore using less memory.
Example
Display employee Menchu’s last name, department number, and department name.
SQL> SELECT s_emp.last_name, s_emp.dept_id,
2 s_dept.name
3 FROM s_emp, s_dept
4 WHERE s_emp.dept_id = s_dept.id
5 AND INITCAP(s_emp.last_name) = ’Menchu’;
LAST_NAME DEPT_ID NAME
------------ ------- --------------------
Menchu 42 Operations
Display the last name, region name, and commission percent of all employees who
earn a commission.
SQL> SELECT s_emp.last_name, s_region.name,
2 s_emp.commission_pct
3 FROM s_emp, s_dept, s_region
4 WHERE s_emp.dept_id = s_dept.id

5 AND s_dept.region_id = s_region.id
6 AND s_emp.commission_pct > 0;
LAST_NAME NAME COMMISSION_PCT
------------ -------------------- --------------
Magee North America 10
Giljum South America 12.5
Sedeghi Africa / Middle East 10
Nguyen Asia 15
Dumas Europe 17.5

×