Transcript Chapter 8

8
Chapter 8
Advanced SQL
Database Systems:
Design, Implementation, and Management,
Seventh Edition, Rob and Coronel
1
8
Numeric Functions
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
2
8
String Functions
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
3
8
String Functions (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
4
8
Conversion Functions
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
5
8
Conversion Functions (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
6
8
Oracle Sequences
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
7
8
Oracle Sequences (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
8
8
Updatable Views
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
9
8
Updatable Views (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
10
8
Updatable Views (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
11
8
Updatable Views (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
12
8
Procedural SQL
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
13
8
Procedural SQL (continued)
• SHOW ERRORS
– Can help diagnose errors found in PL/SQL
blocks
– Yields additional debugging information
whenever error is generated after creating or
executing a PL/SQL block
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
14
8
Procedural SQL (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
15
8
Procedural SQL (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
16
8
Triggers
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
17
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
18
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
19
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
20
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
21
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
22
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
23
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
24
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
25
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
26
8
Triggers (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
27
8
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
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
28
8
Stored Procedures (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
29
8
Stored Procedures (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
30
8
Stored Procedures (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
31
8
Stored Procedures (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
32
8
Stored Procedures (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
33
8
Stored Procedures (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
34
8
Stored Procedures (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
35
8
PL/SQL Processing with Cursors
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
36
8
PL/SQL Processing with Cursors
(continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
37
8
PL/SQL Processing with Cursors
(continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
38
8
PL/SQL Stored Functions
• Syntax:
– CREATE FUNCTION function_name
(argument IN data-type, …) RETURN datatype [IS]
BEGIN
PL/SQL statements;
…
RETURN (value or expression);
END;
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
39
8
Embedded SQL
• Key differences between SQL and procedural
languages are:
– Run-time mismatch
• SQL executed one instruction at a time
• Host language typically runs at 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
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
40
8
Embedded SQL (continued)
• Embedded SQL framework defines:
– Standard syntax to identify embedded SQL
code within host language
– Standard syntax to identify host variables
– Communication area used to exchange status
and error information between SQL and host
language
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
41
8
Embedded SQL (continued)
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
42
8
Embedded SQL (continued)
• Static SQL
– Embedded SQL in which 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
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
43
8
Embedded SQL (continued)
• 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 condition are not known until end
user specifies them
– Tends to be much slower than static SQL
– Requires more computer resources
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
44
8
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
• Natural join returns all rows with matching
values in the matching columns and
eliminates duplicate columns
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
45
8
Summary (continued)
• Joins may use keywords, such as USING and
ON
• Subqueries and correlated queries are used
when it is necessary to process data based
on other processed data
• Most subqueries are executed in serial
fashion
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
46
8
Summary (continued)
• SQL functions are used to extract or
transform data
• Oracle sequences may be used to generate
values to be assigned to a record
• PL/SQL can be used to create triggers, stored
procedures, and PL/SQL functions
• A stored procedure is a named collection of
SQL statements
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
47
8
Summary (continued)
• When SQL statements are designed to return
more than one value inside the PL/SQL code,
cursor is needed
• Embedded SQL refers to use of SQL
statements within an application
programming language
Database Systems: Design, Implementation, & Management, 7th Edition, Rob & Coronel
48