Você está na página 1de 62

7

1
Chapter 7
Advanced SQL

Database Systems:
Design, Implementation, and Management,
Sixth Edition, Rob and Coronel
7
2 Database Systems: Design, Implementation, & Management, 6
th
Edition, 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 operator
syntax
About the different types of subqueries and
correlated queries
How to use SQL functions to manipulate dates,
strings, and other data

7
3 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
In this chapter, you will learn: (continued)
How to create and use updatable views
How to create and use triggers and stored
procedures
How to create embedded SQL

7
4 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
UNION Query Result
7
5 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
UNION ALL Query Result
7
6 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
INTERSECT Query Result
7
7 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
MINUS Query Results
7
8 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
INTERSECT Alternative
7
9 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
MINUS Alternative
7
10 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
SQL Join Expression Styles
7
11 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
NATURAL JOIN Result
7
12 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
JOIN USING Result
7
13 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
JOIN ON Result
7
14 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Outer Joins
Returns not only rows matching join condition
but also rows with unmatched values
Three types:
Left
Right
Full
7
15 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
LEFT JOIN Result
7
16 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
RIGHT JOIN Result
7
17 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
FULL JOIN Result
7
18 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
SELECT Subquery Examples
7
19 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
WHERE Subquery Examples
7
20 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
IN Subquery Example
7
21 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
HAVING Subquery Example
7
22 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Multirow Subquery Operator Example
7
23 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
FROM Subquery Example
7
24 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Inline Subquery Example
7
25 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Correlated Subquery Examples
7
26 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
EXISTS Correlated Subquery Examples
7
27 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Selected MS Access/SQL Server
Date/Time Functions
7
28 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Selected Oracle Date/Time Functions
7
29 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Selected Oracle Date/Time Functions
(continued)
7
30 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Selected Oracle Numeric Functions
7
31 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Selected Oracle String Functions
7
32 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Selected Oracle Conversion Functions
7
33 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Selected Oracle Conversion Functions
(continued)
7
34 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Oracle Sequence
7
35 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Oracle Sequence Examples
7
36 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
The PRODMASTER
and PRODSALES Tables
7
37 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
The Oracle UPDATE Error Message
7
38 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Creating an Updatable View in Oracle
7
39 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
PRODMASTER Table Update,
Using an Updatable View
7
40 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Anonymous PL/SQL Block Examples
7
41 Database Systems: Design, Implementation, & Management, 6
th
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
42 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Anonymous PL/SQL Block
with Variables and Loops
7
43 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
PL/SQL Basic Data Types
7
44 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
The PRODUCT Table
7
45 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Creating the
TRG_PRODUCT_REORDER Trigger
7
46 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Verifying the TRG_PRODUCT_REORDER
Trigger Execution
7
47 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
The P_REORDER Value Mismatch After
Update of the P_MIN Attribute
7
48 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Stored Procedures: Advantages
Substantially reduce network traffic and
increase performance
No transmission of individual SQL statements
over 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
49 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Creating the PRC_PROD_DISCOUNT
Stored Procedure
7
50 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Results of the PRC_PROD_DISCOUNT
Stored Procedure
7
51 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
The PRC_CUS_ADD Stored Procedure
7
52 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
The PRC_INV_ADD and
PRC_LINE_ADD Stored Procedures
7
53 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Testing the PRC_INV_ADD and
PRC_LINE_ADD Procedures
7
54 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Cursor Processing Commands
7
55 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
Cursor Attributes
7
56 Database Systems: Design, Implementation, & Management, 6
th
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 side
in its own memory space
Processing mismatch:
Host language processes one data element at
a time
Data type mismatch:
Data types may not match

7
57 Database Systems: Design, Implementation, & Management, 6
th
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
58 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
SQL Status and Error Reporting Variables
7
59 Database Systems: Design, Implementation, & Management, 6
th
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 while
application is running

7
60 Database Systems: Design, Implementation, & Management, 6
th
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 hoc
queries
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
61 Database Systems: Design, Implementation, & Management, 6
th
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 as
inner 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
62 Database Systems: Design, Implementation, & Management, 6
th
Edition, Rob & Coronel
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

Você também pode gostar