© 2007 by prentice hall6-1 introduction to oracle 10g chapter 6 creating multitable queries and...

Post on 16-Jan-2016

216 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

© 2007 by Prentice Hall6-1

Introduction to Oracle 10gChapter 6Creating Multitable Queries and Views

James Perry and Gerald Post

© 2007 by Prentice Hall6-2

Chapter Outline

• Creating and Using Multitable Queries• Creating and Using Views

© 2007 by Prentice Hall6-3

Table 6.1 Asking price distribution table

CategoryID

LowLimit

HighLimit

1000 $0 $50,000

1010 $50,000 $100,000

1020 $100,000

$150,000

1030 $150,000

$200,000

1040 $200,000

$250,000

1050 $250,000

$300,000

1060 $300,000

$350,000

1070 $350,000

$400,000

1080 $400,000

$2,000,000

© 2007 by Prentice Hall6-4

Table 6.2 SQL set operators

Set operator

Description

UNION Returns all unique rows retrieved by the queries.

UNION ALL Returns all the rows retrieved by the queries, including any duplicate rows.

INTERSECT Returns rows that are retrieved by both queries.

MINUS Returns the rows that remain when the rows retrieved by a second query are removed (subtracted) from the rows retrieved by the first query.

© 2007 by Prentice Hall6-5

Table 6.3 ANY and ALL operator descriptions

Operator

Meaning

=ANY Equal to any value the subquery returns. It is the same as the IN operator.

<ANY Smaller than the largest value the subquery returns.

>ANY Larger than the smallest value the subquery returns.

>ALL Larger than the largest value the subquery returns.

<ALL Smaller than the smallest value the subquery returns.

© 2007 by Prentice Hall6-6

Table 6.4 Description of some of the user_views columns

Column Name

Data Type Description

view_name VARCHAR2(30)

Name of the view.

text_length NUMBER Number of characters in the view’s subquery.

text LONG Text of the view’s subquery that created the view.

© 2007 by Prentice Hall6-7

ID Name Div

1001 Bob Mkt

1002 Dawn Sales

1003 Betty Sales

1006 Nancy Mkt

1009 Fred Sales

PK2 Emp#

Amount

21 1002 $2,000

22 1002 $2,200

23 1003 $1,800

24 1003 $200

25 1009 $600

26 1009 $750

27 1009 $425

Employees Sales

Result of joining the Employees and Sales tables:

from Employees from Sales

1002 Dawn Sales 21 1002 $2,0001002 Dawn Sales 22 1002 $2,2001003 Betty Sales 23 1003 $1,8001003 Betty Sales 24 1003 $2001009 Fred Sales 25 1009 $6001009 Fred Sales 26 1009 $7501009 Fred Sales 27 1009 $425

6.1 Joining two tables

© 2007 by Prentice Hall6-8

joining tables on primary key/foreign key pairs

6.2 Joining the Customers and Properties tables

© 2007 by Prentice Hall6-9

specifies column in both tables that links them

6.3 Joining tables on like-named columns

© 2007 by Prentice Hall6-10

6.4 Using a natural join

© 2007 by Prentice Hall6-11

6.5 Joining three tables and filtering rows

© 2007 by Prentice Hall6-12

ID Name Div

1001 Bob Mkt

1002 Dawn Sales

1003 Betty Sales

1006 Nancy Mkt

1009 Fred Sales

PK2 Emp#

Amount

21 1002 $2,000

22 1002 $2,200

23 1003 $1,800

24 1003 $200

Salespersons Sales

Result of joining the Salespersons and Sales tables:

from Salespersons from Sales

1002 Dawn Sales 21 1002 $2,0001002 Dawn Sales 22 1002 $2,2001003 Betty Sales 23 1003 $1,8001003 Betty Sales 24 1003 $2001001 Bob Mkt null null null1009 Nancy Mkt null null null1009 Fred Sales null null null

6.6 Outer join illustration

© 2007 by Prentice Hall6-13

inner join left outer join

Salesperson Sales

rows with matching values

(selected rows)

excluded rows excluded rows

rows with matching values

excluded rows

Salesperson Sales

selected rows

6.7 Venn diagrams showing inner join and left outer join

© 2007 by Prentice Hall6-14

6.8 Using a left outer join

© 2007 by Prentice Hall6-15

value 0 indicates no match in Listings (left) table

6.9 Showing non matching rows with a COUNT function

© 2007 by Prentice Hall6-16

6.10 Displaying the EmpSelfJoin table

© 2007 by Prentice Hall6-17

EmployeeID

BossID EmployeeID

BossID

EmpSelfJoin e EmpSelfJoin m

e.BossID = m.EmployeeID

6.11 Illustration of a table joined to itself by using aliases

© 2007 by Prentice Hall6-18

6.12 Listing managers and their employees with a self join query

© 2007 by Prentice Hall6-19

“Board of Directors” appears when the BossID is null

6.13 Managers and their employees produced using an outer join query

© 2007 by Prentice Hall6-20

non-equijoin join conditions

6.14 Creating the PriceCat table and running a non-equijoin query

© 2007 by Prentice Hall6-21

6.15 Comparing a non-equijoin and a right outer non-equijoin

© 2007 by Prentice Hall6-22

6.16 Using UNION in a compound query

© 2007 by Prentice Hall6-23

display homes newer by 18 years than the average for Arcata

display agents who have the same title as Jessica Taylor

6.17 Using subqueries in WHERE clauses

© 2007 by Prentice Hall6-24

6.18 Examples of the IN operator and subqueries

© 2007 by Prentice Hall6-25

(additional rows are out of sight)

6.19 Description and contents of the AgentsHR table

© 2007 by Prentice Hall6-26

6.20 Using ALL and ANY operators in a query

© 2007 by Prentice Hall6-27

6.21 Using two subqueries and the ALL operator

© 2007 by Prentice Hall6-28

6.22 A correlated subquery to locate above-average asking prices

© 2007 by Prentice Hall6-29

6.23 A correlated subquery to display above-average salaries

© 2007 by Prentice Hall6-30

Count the customers without agents

Count the customers without properties

list the agents who are not designated “Licensed”

6.24 Using the EXISTS operator in several correlated subqueries

© 2007 by Prentice Hall6-31

6.25 Creating and describing views

© 2007 by Prentice Hall6-32

6.26 Displaying rows through the Nonbrokers view

© 2007 by Prentice Hall6-33

6.27 Defining a view with alternative column names

© 2007 by Prentice Hall6-34

6.28 Executing DML statements on a view

© 2007 by Prentice Hall6-35

6.29 Disallowing an update on a one-table complex view

© 2007 by Prentice Hall6-36

creating a multiple table view

querying a multiple table view and the results

6.30 Creating a multiple table view and querying it

© 2007 by Prentice Hall6-37

creating a five-table view

format columns

display rows using the ForSale view

6.31 Creating a multiple table view joining five tables

© 2007 by Prentice Hall6-38

6.32 Displaying view information

© 2007 by Prentice Hall6-39

6.33 Listing and then dropping views you created

top related