Upload
jnax101
View
215
Download
0
Embed Size (px)
Citation preview
7/21/2019 21323-xppt-chap07
1/62
7
1
Chapter 7
Advanced SQL
Database Systems:Design, Implementation, and Management,
Sixth Edition, Rob and Coronel
7/21/2019 21323-xppt-chap07
2/62
7
2Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
In this chapter, you will learn:
About the relational set operators UNION,
UNION ALL, INTERSECT, and MINUS
How to use the advanced SQL JOIN operatorsyntax
About the different types of subqueries and
correlated queries
How to use SQL functions to manipulate dates,
strings, and other data
7/21/2019 21323-xppt-chap07
3/62
7/21/2019 21323-xppt-chap07
4/62
7
4Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
UNION Query Result
7/21/2019 21323-xppt-chap07
5/62
7
5Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
UNION ALL Query Result
7/21/2019 21323-xppt-chap07
6/62
7
6Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
INTERSECT Query Result
7/21/2019 21323-xppt-chap07
7/62
7
7Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
MINUS Query Results
7/21/2019 21323-xppt-chap07
8/62
7
8Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
INTERSECT Alternative
7/21/2019 21323-xppt-chap07
9/62
7
9Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
MINUS Alternative
7/21/2019 21323-xppt-chap07
10/62
7
10Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
SQL Join Expression Styles
7/21/2019 21323-xppt-chap07
11/62
7
11Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
NATURAL JOIN Result
7/21/2019 21323-xppt-chap07
12/62
7
12Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
JOIN USING Result
7/21/2019 21323-xppt-chap07
13/62
7
13Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
JOIN ON Result
7/21/2019 21323-xppt-chap07
14/62
7
14Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Outer Joins
Returns not only rows matching join condition
but also rows with unmatched values
Three types:
Left
Right
Full
7/21/2019 21323-xppt-chap07
15/62
7
15Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
LEFT JOIN Result
7/21/2019 21323-xppt-chap07
16/62
7
16Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
RIGHT JOIN Result
7/21/2019 21323-xppt-chap07
17/62
7
17Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
FULL JOIN Result
7/21/2019 21323-xppt-chap07
18/62
7
18Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
SELECT Subquery Examples
7/21/2019 21323-xppt-chap07
19/62
7
19Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
WHERE Subquery Examples
7/21/2019 21323-xppt-chap07
20/62
7
20Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
IN Subquery Example
7/21/2019 21323-xppt-chap07
21/62
7
21Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
HAVING Subquery Example
7/21/2019 21323-xppt-chap07
22/62
7
22Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Multirow Subquery Operator Example
7/21/2019 21323-xppt-chap07
23/62
7
23Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
FROM Subquery Example
7/21/2019 21323-xppt-chap07
24/62
7
24Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Inline Subquery Example
7/21/2019 21323-xppt-chap07
25/62
7
25Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Correlated Subquery Examples
7/21/2019 21323-xppt-chap07
26/62
7
26Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
EXISTS Correlated Subquery Examples
7/21/2019 21323-xppt-chap07
27/62
7
27Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Selected MS Access/SQL Server
Date/Time Functions
7/21/2019 21323-xppt-chap07
28/62
7
28Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Selected Oracle Date/Time Functions
7
7/21/2019 21323-xppt-chap07
29/62
7
29Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Selected Oracle Date/Time Functions
(continued
)
7
7/21/2019 21323-xppt-chap07
30/62
7
30Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Selected Oracle Numeric Functions
7
7/21/2019 21323-xppt-chap07
31/62
7
31Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Selected Oracle String Functions
7
7/21/2019 21323-xppt-chap07
32/62
7
32Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Selected Oracle Conversion Functions
7
7/21/2019 21323-xppt-chap07
33/62
7
33Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Selected Oracle Conversion Functions
(continued)
7
7/21/2019 21323-xppt-chap07
34/62
7
34Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Oracle Sequence
7
7/21/2019 21323-xppt-chap07
35/62
7
35Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
Oracle Sequence Examples
7
7/21/2019 21323-xppt-chap07
36/62
7
36Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
The PRODMASTER
and PRODSALES Tables
7
7/21/2019 21323-xppt-chap07
37/62
7
37Database Systems: Design, Implementation, & Management, 6 thEdition, Rob & Coronel
The Oracle UPDATE Error Message
7
7/21/2019 21323-xppt-chap07
38/62
7
38Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Creating an Updatable View in Oracle
7
7/21/2019 21323-xppt-chap07
39/62
7
39Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
PRODMASTER Table Update,
Using an Updatable View
7
7/21/2019 21323-xppt-chap07
40/62
7
40Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Anonymous PL/SQL Block Examples
7
7/21/2019 21323-xppt-chap07
41/62
7
41Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
SHOW ERRORS
Can help diagnose errors found in PL/SQL
blocks
Yields additional debugging information
whenever an error is generated after an
PL/SQL block is created or executed
7
7/21/2019 21323-xppt-chap07
42/62
7
42Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Anonymous PL/SQL Block
with Variables and Loops
7
7/21/2019 21323-xppt-chap07
43/62
7
43Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
PL/SQL Basic Data Types
7
7/21/2019 21323-xppt-chap07
44/62
7
44Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
The PRODUCT Table
7
7/21/2019 21323-xppt-chap07
45/62
7
45Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Creating the
TRG_PRODUCT_REORDER Trigger
7
7/21/2019 21323-xppt-chap07
46/62
7
46Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Verifying the TRG_PRODUCT_REORDER
Trigger Execution
7
7/21/2019 21323-xppt-chap07
47/62
7
47Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
The P_REORDER Value Mismatch After
Update of the P_MIN Attribute
7
7/21/2019 21323-xppt-chap07
48/62
7
48Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Stored Procedures: Advantages
Substantially reduce network traffic and
increase performance
No transmission of individual SQL statementsover network
Help reduce code duplication by means of
code isolation and code sharing Minimize chance of errors and cost of
application development and maintenance
7
7/21/2019 21323-xppt-chap07
49/62
7
49Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Creating the PRC_PROD_DISCOUNT
Stored Procedure
7
7/21/2019 21323-xppt-chap07
50/62
7
50Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Results of the PRC_PROD_DISCOUNT
Stored Procedure
7
7/21/2019 21323-xppt-chap07
51/62
7
51Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
The PRC_CUS_ADD Stored Procedure
7
7/21/2019 21323-xppt-chap07
52/62
7
52Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
The PRC_INV_ADD and
PRC_LINE_ADD Stored Procedures
7
7/21/2019 21323-xppt-chap07
53/62
7
53Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Testing the PRC_INV_ADD and
PRC_LINE_ADD Procedures
7
7/21/2019 21323-xppt-chap07
54/62
7
54Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Cursor Processing Commands
7
7/21/2019 21323-xppt-chap07
55/62
7
55Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Cursor Attributes
7
7/21/2019 21323-xppt-chap07
56/62
7
56Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
SQL and Procedural Languages:
Key Differences Run-time mismatch:
SQL executed one instruction at a time
Host language typically runs at the client sidein its own memory space
Processing mismatch:
Host language processes one data element at
a time
Data type mismatch:
Data types may not match
7
7/21/2019 21323-xppt-chap07
57/62
57Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Embedded SQL Framework
A standard syntax to identify embedded SQL
code within host language
A standard syntax to identify host variables
A communication area used to exchange
status and error information between SQL
and the host language
7
7/21/2019 21323-xppt-chap07
58/62
58Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
SQL Status and Error Reporting Variables
7
7/21/2019 21323-xppt-chap07
59/62
59Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Static SQL
Embedded SQL in which the programmer
used predefined SQL statements and
parameters
End users of programs are limited to actions
that were specified in application programs
SQL statements will not change whileapplication is running
7
7/21/2019 21323-xppt-chap07
60/62
60Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Dynamic SQL
SQL statement is not known in advance, but
instead is generated at run time
Program can generate SQL statements at run
time that are required to respond to ad hocqueries
Attribute list and the condition are not known
until the end user specifies them
Tends to be much slower than static SQL
Requires more computer resources
7
7/21/2019 21323-xppt-chap07
61/62
61Database Systems: Design, Implementation, & Management, 6th
Edition, Rob & Coronel
Summary
SQL provides relational set operators to
combine output of two queries to generate new
relation
Operations that join tables can be classified asinner joins and outer joins
Subqueries and correlated queries are used
when it is necessary to process data based on
other processed data
SQL functions are used to extract or transform
data
7
7/21/2019 21323-xppt-chap07
62/62
Summary (continued)
Oracle sequences may be used to generate
values to be assigned to a record
PL/SQL may be used to create triggers,
stored procedures, and PL/SQL functions If SQL statements are designed to return
more than one value inside the PL/SQL code,
a cursor is needed
Embedded SQL refers to the use of SQL
statements within an application
programming language