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

advanced sql Functions in Oracle 10G phần 1 doc

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 (1.21 MB, 42 trang )

Richard Walsh Earp
Sikha Saha Bagui
Wordware Publishing, Inc.
Library of Congress Cataloging-in-Publication Data
Earp, Richard, 1940-
Advanced SQL functions in Oracle 10g / by Richard Walsh Earp
and Sikha Saha Bagui.
p. cm.
Includes bibliographical references and index.
ISBN-13: 978-1-59822-021-6
ISBN-10: 1-59822-021-7 (pbk.)
1. SQL (Computer program language) 2. Oracle (Computer file).
I. Bagui, Sikha, 1964 II. Title.
QA76.73.S67E26 2006
005.13'3 dc22 2005036444
CIP
© 2006, Wordware Publishing, Inc.
All Rights Reserved
2320 Los Rios Boulevard
Plano, Texas 75074
No part of this book may be reproduced in any form or by
any means without permission in writing from
Wordware Publishing, Inc.
Printed in the United States of America
ISBN-13: 978-1-59822-021-6
ISBN-10: 1-59822-021-7
10987654321
0601
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other brand names and product names mentioned in this book are trademarks or service marks of their


respective companies. Any omission or misuse (of any kind) of service marks or trademarks should not be
regarded as intent to infringe on the property of others. The publisher recognizes and respects all marks used by
companies, manufacturers, and developers as a means to distinguish their products.
This book is sold as is, without warranty of any kind, either express or implied, respecting the contents of this
book and any disks or programs that may accompany it, including but not limited to implied warranties for the
book’s quality, performance, merchantability, or fitness for any particular purpose. Neither Wordware Publishing,
Inc. nor its dealers or distributors shall be liable to the purchaser or any other person or entity with respect to
any liability, loss, or damage caused or alleged to have been caused directly or indirectly by this book.
All inquiries for volume purchases of this book should be addressed to Wordware
Publishing, Inc., at the above address. Telephone inquiries may be made by calling:
(972) 423-0090
To my wife, Brenda,
and
my children, Beryl, Rich, Gen, and Mary Jo
R.W.E.
To my father, Santosh Saha, and mother, Ranu Saha,
and
my husband, Subhash Bagui,
and
my sons, Sumon and Sudip,
and
my brother, Pradeep, and nieces, Priyashi and Piyali
S.S.B.
This page intentionally left blank.
Contents
Preface xi
Acknowledgments xiii
Introduction xv
Chapter 1 Common Oracle Functions: A Function Review 1
Calling Simple SQL Functions 3

Numeric Functions 4
Common Numerical Manipulation Functions 4
Near Value Functions 7
Null Value Function 10
Log and Exponential Functions 12
Ordinary Trigonometry Functions 14
Hyperbolic Trig Functions 16
String Functions 18
The INSTR Function 18
The SUBSTR Function 20
The REPLACE Function 23
The TRIM Function 24
Date Functions 27
Chapter 2 Reporting Tools in Oracle’s SQL*Plus 31
COLUMN 32
Formatting Numbers 35
Scripts 39
Formatting Dates 41
BREAK 43
COMPUTE 45
Remarks in Scripts 48
TTITLE and BTITLE 49
References 52
v
Chapter 3 The Analytical Functions in Oracle
(Analytical Functions I) 53
What Are Analytical Functions? 53
The Row-numbering and Ranking Functions 55
The Order in Which the Analytical Function Is
Processed in the SQL Statement 65

A SELECT with Just a FROM Clause 66
A SELECT with Ordering 66
A WHERE Clause Is Added to the Statement 67
An Analytical Function Is Added to the Statement . . . 67
A Join Is Added to the Statement 68
The Join Without the Analytical Function 69
Adding Ordering to a Joined Result 70
Adding an Analytical Function to a Query that
Contains a Join (and Other WHERE Conditions) . . 71
The Order with GROUP BY Is Present 72
Adding Ordering to the Query Containing the
GROUP BY 73
Adding an Analytical Function to the GROUP BY
with ORDER BY Version 74
Changing the Final Ordering after Having Added
an Analytical Function 75
Using HAVING with an Analytical Function 76
Where the Analytical Functions Can be Used in a
SQL Statement 77
More Than One Analytical Function May Be Used in
a Single Statement 78
The Performance Implications of Using Analytical
Functions 80
Nulls and Analytical Functions 86
Partitioning with PARTITION_BY 95
A Problem that Uses ROW_NUMBER for a Solution 96
NTILE 101
RANK, PERCENT_RANK, and CUME_DIST 105
References 110
vi

Contents
Chapter 4 Aggregate Functions Used as Analytical Functions
(Analytical Functions II) 111
The Use of Aggregate Functions in SQL 111
RATIO-TO-REPORT 115
Windowing Subclauses with Physical Offsets in
Aggregate Analytical Functions 120
An Expanded Example of a Physical Window 127
Displaying a Running Total Using SUM as an
Analytical Function 131
UNBOUNDED FOLLOWING 134
Partitioning Aggregate Analytical Functions 135
Logical Windowing 137
The Row Comparison Functions — LEAD and LAG 143
LAG and LEAD Options 146
Chapter 5 The Use of Analytical Functions in Reporting
(Analytical Functions III) 149
GROUP BY 150
Grouping at Multiple Levels 155
ROLLUP 157
CUBE 160
GROUPING with ROLLUP and CUBE 162
Chapter 6 The MODEL or SPREADSHEET Predicate in
Oracle’s SQL 165
The Basic MODEL Clause 166
Rule 1. The Result Set 169
Rule 2. PARTITION BY 169
Rule 3. DIMENSION BY 170
Rule 4. MEASURES 170
RULES that Use Other Columns 174

RULES that Use Several Other Rows to Compute
New Rows 178
RETURN UPDATED ROWS 183
Using Comparison Operators on the LHS 184
Adding a Summation Row — Using the RHS to
Generate New Rows Using Aggregate Data 186
Summing within a Partition 189
vii
Contents
Aggregation on the RHS with Conditions on the
Aggregate 191
Revisiting CV with Value Offsets — Using Multiple
MEASURES Values 193
Ordering of the RHS 198
AUTOMATIC versus SEQUENTIAL ORDER 202
The FOR Clause, UPDATE, and UPSERT 206
Iteration 211
A Square Root Iteration Example 214
References 221
Chapter 7 Regular Expressions: String Searching and
Oracle 10g 223
A Simple Table to Illustrate an RE 225
REGEXP_INSTR 226
A Simple RE Using REGEXP_INSTR 230
Metacharacters 231
Brackets 237
Ranges (Minus Signs) 239
REGEXP_LIKE 239
Negating Carets 241
Bracketed Special Classes 243

Other Bracketed Classes 246
The Alternation Operator 247
Repetition Operators — aka “Quantifiers” 248
More Advanced Quantifier Repeat Operator
Metacharacters — *, %, and ? 251
REGEXP_SUBSTR 253
Empty Strings and the ? Repetition Character 258
REGEXT_REPLACE 259
Grouping 261
The Backslash (\) 262
The Backslash as an Escape Character 263
Alternative Quoting Mechanism in Oracle 10g 264
Backreference 265
References 267
viii
Contents
Chapter 8 Collection and OO SQL in Oracle 269
Associative Arrays 270
The OBJECT TYPE — Column Objects 273
CREATE a TABLE with the Column Type in It 274
INSERT Values into a Table with the Column
Type in It 275
Display the New Table (SELECT * and SELECT
by Column Name) 275
COLUMN Formatting in SELECT 277
SELECTing Only One Column in the Composite 277
SELECT with a WHERE Clause 278
Using UPDATE with TYPEed Columns 278
Create Row Objects — REF TYPE 279
Loading the “row object” Table 281

UPDATE Data in a Table of Row Objects 283
CREATE a Table that References Our Row Objects. . 284
INSERT Values into a Table that Contains Row
Objects (TCRO) 284
UPDATE a Table that Contains Row Objects
(TCRO) 285
SELECT from the TCRO — Seeing Row
Addresses 286
DEREF (Dereference) the Row Addresses 286
One-step INSERTs into a TCRO 287
SELECTing Individual Columns in TCROs 288
Deleting Referenced Rows 289
The Row Object Table and the VALUE Function . . . 291
Creating User-defined Functions for Column
Objects 292
VARRAYs 297
CREATE TYPE for VARRAYs 299
CREATE TABLE with a VARRAY 300
Loading a Table with a VARRAY in It — INSERT
VALUEs with Constants 301
Manipulating the VARRAY 302
The TABLE Function 303
The VARRAY Self-join 305
ix
Contents
The THE and VALUE Functions 306
The CAST Function 308
Using PL/SQL to Create Functions to
Access Elements 311
Creating User-defined Functions for VARRAYs. . 320

Nested Tables 324
Summary 334
References 335
Chapter 9 SQL and XML 337
What Is XML? 338
Displaying XML in a Browser 342
SQLtoXML 344
Generating XML from “Ordinary” Tables 344
XMLtoSQL 347
References 355
Appendix A String Functions 357
Appendix B Statistical Functions 371
Index 392
x
Contents
Preface
Why This Book?Why This Book?
Oracle
®
10g has introduced new features into its reper
-
toire of SQL instructions that make database queries
more versatile. When programmers use SQL in Oracle,
they inevitably look for easier and new ways to handle
queries. What is needed is a way to introduce SQL
users to the new features of Oracle 10g concisely and
systematically so that database programmers can take
full advantage of the newer capabilities. This book
hopes to meet this need by exploring some common
new SQL features. Each chapter includes numerous

working examples, and Oracle users can run these
examples as they read and work through the book.
Also, many books on Oracle 10g present the language
syntax alone with no in-depth explanation, analysis, or
examples. In this book, we present not only the syntax
for new features and functions, but also a thorough
clarification and breakdown of the different functions,
along with examples of ways they can and should be
used.
Audience and CoverageAudience and Coverage
This book is meant to be used by Oracle professionals
as well as students, but it is not a SQL primer. Readers
of this book are expected to have previously used Ora
-
cle, SQL*Plus, and, to some extent, PL/SQL. This book
can be used for individual study or reference, in
advanced Oracle training settings, and in advanced
xi
database classes in schools. It is meant for those famil
-
iar with SQL programming since most of the topics
present not only the syntax, queries, and answers, but
also have an analytical programming perspective to
them. This book will allow the Oracle user to use SQL
in new and exciting ways.
This book contains nine chapters. It begins by
reviewing some of the common SQL functions and
techniques to help transition into the newer tools of
Oracle 10g. Chapter 1 reviews common Oracle func
-

tions. Chapter 2 covers some common reporting tools
in Oracle’s SQL*Plus. Chapter 3 introduces and dis
-
cusses Oracle 10g’s analytical functions, and Chapter 4
discusses Oracle 10g’s aggregate functions that are
used as analytical functions. Chapter 5 looks at the use
of analytical functions in reporting — for example, the
use of GROUP BY, ROLLUP, and CUBE. Chapter 6
discusses the MODEL or SPREADSHEET predicate
in Oracle’s SQL. Chapter 7 covers the new regular
expressions and string functions. Chapter 8 discusses
collections and object-oriented features of Oracle 10g.
Chapter 9 introduces by example the bridges between
SQL and XML, one of the most important topics Ora-
cle professionals are expected to know today.
This book also has two appendices. Appendix A
illustrates string functions with examples, and Appen
-
dix B gives examples of some important statistical
functions available in Oracle 10g.
Overall, this book explores advanced new features
of SQL in Oracle 10g from a programmer’s perspective.
The book can be considered a starting point for
research using some of the advanced topics since the
subjects are discussed at length with examples and
sample outputs. Query development is approached
from a logical standpoint, and in many areas perfor
-
mance implications of the queries are also discussed.
xii

Preface
Acknowledgments
Our special thanks to the staff at Wordware Pub
-
lishing, especially Wes Beckwith, Beth Kohler, Martha
McCuller, and Denise McEvoy.
We would also like to thank President John
Cavanaugh, Dean Jane Halonen, and Provost Sandra
Flake for their inspiration, encouragement, support,
and true leadership. We would also like to express our
gratitude to Dr. Wes Little on the same endeavor. Our
sincere thanks also goes to Dr. Ed Rodgers for his con-
tinuing support and encouragement throughout the
years. We also appreciate Dr. Leonard Ter Haar, chair
of the computer science department, for his advice,
guidance, and support, and encouraging us to complete
this book. Last, but not least, we would like to thank
our fellow faculty members Dr. Jim Bezdek and Dr.
Norman Wilde for their continuous support and
encouragement.
xiii
This page intentionally left blank.
Introduction
With the advent of new features added to SQL in Ora
-
cle 10g, we thought that some collection of material
related to the newer query mechanisms was in order.
Hence, in this book we have gathered some useful new
tools into a set of topics for exploiting Oracle 10g’s
SQL. We have also briefly reviewed some older tools

that will help transition to the new material.
This book mainly addresses advanced topics in
SQL with a focus on SQL functions for Oracle 10g. The
functions and methods we cover include the analytical
functions, MODEL statements, regular expressions,
and object-oriented/collection structures. We also
introduce and give examples of the SQL/XML bridges
as XML is a newer and common method of transferring
data from user to user. We rely heavily on examples, as
most SQL programmers can and do adapt examples to
other problems quickly.
Prerequisites
Some knowledge of SQL is assumed before beginning
this study, as this book is not meant to be a SQL
primer. More specifically, some knowledge of Oracle
functions is desirable, although some common func
-
tions are reviewed in Chapter 1. Functions have been
refined and expanded as Oracle versions have evolved,
culminating with the latest in Oracle 10g — analytical
functions, MODEL statements, and regular expres
-
sions. Additionally, the collection/object-oriented
structures of later versions of Oracle are covered and
xv
include some unique functions as well. Many people
now use XML to capture and move data; examples of
moving data from SQL*Plus to and from XML are also
covered.
Some knowledge of spreadsheets is helpful in

digesting this material. The analytical functions and
MODEL statements provide convenient ways to dis
-
play and use data in a manner similar to a spreadsheet.
While these functions are far more than simply display
mechanisms, often reporting/formatting functions are
used in conjunction with analytical functions. We
review some common reporting functions in Chapter 2.
Our Approach to SQLOur Approach to SQL
In addition to a basic knowledge of SQL, we will call
attention to “our way” of developing queries in SQL.
The way we develop queries in SQL is often by begin-
ning with a simple command and then building upon it
until the answer is found. There are different
approaches to building queries in SQL as in any other
language. One way is to build for a result using logical,
intermediate steps. A second way to build SQL queries
is for performance. In a real-world environment with
large tables, performance usually becomes an issue on
often-run commands. Even in the development of que
-
ries, performance issues may arise.
The way this material is approached is less from
the performance perspective and more from the logical,
developmental viewpoint. Once a result is obtained, if
the query is to be rerun, it is most appropriate to tune
the query for performance by examining the way it was
done and perhaps look for alternatives, e.g., joins ver
-
sus subqueries.

To develop queries, we will often find a result set
and then use that result set to move to the next part of
the query. This modular approach has an
xvi
Introduction
uncomplicated appeal as well as a way to check and
examine intermediate results. If the intermediate
result is faulty, then we correct and refine before we
move on. One should always be suspicious of intermedi
-
ate results by asking questions like, “Does this result
make sense?”, “How can we have that many rows?”, or
“How many rows did you expect?” When we are satis
-
fied with the result we have produced, we use the
result in a virtual table to attain the next level.
For example, consider this query:
SELECT class, COUNT(*)
FROM students
GROUP BY class
Having studied this result, we might use it in a virtual
table for another query. We can wrap our working
query in parentheses (hence making it a virtual view)
and then query it like this:
SELECT MAX(enrollment)
FROM
(SELECT class, COUNT(*) enrollment
FROM students
GROUP BY class)
There are, of course, times in real-world applications

where the virtual view is so complicated that it needs to
become a real view or even a temporary table. We call
this virtual table approach “wrap and build.”
In writing queries, we often use aliasing. Some
might argue that we overuse aliases, but we believe
that it makes a query more meaningful, easier to
debug, and more available for change in the future. As
well, in deference to precedence rules and defaults,
when a programmer uses aliases, he is very clear about
what the aliases meant when he wrote the query in the
first place.
xvii
Introduction
This page intentionally left blank.
Chapter 1
Common Oracle
Functions: A
Function Review
Oracle functions operate on “appropriate data” to
transform a value to another value. For example, using
a simple calculator, we commonly use the square root
function to compute the square root of some number.
In this case, the square root key on the calculator calls
the square root function and the number in the display
is transformed into its square root value. In the square
root case, “appropriate data” is a positive number. For
the sake of defining the scope of this discussion, we also
consider the square root key on a calculator as a
one-to-one function. By one-to-one we mean that if one
positive number is furnished, then one square root

results from pressing the square root key — a one-to-
one transformation.
1
Chapter
|
1
If we show the square root function algebraically as
SQRT, the resulting number as “Answer,” the equal
sign as meaning “is assigned to,” and the number to be
operated on as “original_value,” then the function could
be written like this:
Answer = SQRT(original_value)
where original_value is a positive number.
In algebra, the allowable values of original_value
are called the domain of the function, which in this case
is the set of non-negative numbers. Answer is called
the range of the function. Original_value in this exam
-
ple is called the argument of the function SQRT.
Oftentimes in computer situations, there is also an
upper limit on the domain and range, but theoretically,
there is no upper limit in algebra. The lower limit on
the domain is zero as the square root of negative num-
bers is undefined unless one ventures into the area of
complex numbers, which is beyond the scope of this
discussion.
Almost any programming language uses functions
similar to those found on calculators. In fact, most pro-
gramming languages go far beyond the calculator
functions.

Oracle’s SQL contains a rich variety of functions.
We can categorize Oracle’s SQL functions into simple
SQL functions, numeric functions, statistical functions,
string functions, and date functions. In this chapter, we
selectively illustrate several functions in each of these
categories. We start by discussing simple SQL
functions.
2
Common Oracle Functions: A Function Review
Calling Simple SQL FunctionsCalling Simple SQL Functions
Oracle has a large number of simple functions. Wher
-
ever a value is used directly or computed in a SQL
statement, a simple SQL function may be used. To
illustrate the above square root function, suppose that
a table named Measurement contained a series of
numeric measured values like this:
Subject Value
First 35.78
Second 22.22
Third 55.55
We could display the table with this SQL query:
SELECT *
FROM measurement
e
Note: We will not use semicolons at the end of SQL
statement illustrations; to run these statements in Oracle
from the command line, a semicolon must be added.
From the editor, a slash (/) is added to execute the state
-

ment and no semicolon is used.
We could also generate the same result set with this
SQL query:
SELECT subject, value
FROM measurement
Using the latter query, and adding a square root func
-
tion to the result set, the SQL query would look like
this:
SELECT subject, value, SQRT(value)
FROM measurement
3
Chapter
|
1
This would give the following result:
SUBJECT VALUE SQRT(VALUE)

First 35.78 5.98163857
Second 22.22 4.7138095
Third 55.55 7.45318724
Numeric FunctionsNumeric Functions
In this section we present and discuss several useful
numeric functions, which we divide into the following
categories: common numerical manipulation functions,
near value functions, null value functions, log and expo-
nential functions, ordinary trigonometry functions, and
hyperbolic trignometrical functions.
Common NumericalCommon Numerical
Manipulation FunctionsManipulation Functions

These are functions that are commonly used in numeri
-
cal manipulations. Examples of common numerical
manipulation functions include:
ABS — Returns the absolute value of a number or
value.
SQRT — Returns the square root of a number or
value.
MOD — Returns the remainder of n/m where both
n and m are integers.
SIGN — Returns 1 if the argument is positive; –1 if
the argument is negative; and 0 if the argument is
negative.
4
Common Oracle Functions: A Function Review
Next we present a discussion on the use of these com
-
mon numerical manipulation functions. Suppose we had
a table that looked like this:
DESC function_illustrator
Which would give:
Name Null? Type

LINENO NUMBER(2)
VALUE NUMBER(6,2)
Now, if we typed:
SELECT *
FROM function_illustrator
ORDER BY lineno
We would get:

LINENO VALUE

09
1 3.44
2 3.88
3 -6.27
4 -6.82
50
6 2.5
Now, suppose we use our functions to illustrate the
transformation for each value of VALUE:
SELECT lineno, value, ABS(value), SIGN(value), MOD(lineno,3)
FROM function_illustrator
ORDER BY lineno
5
Chapter
|
1
We would get:
LINENO VALUE ABS(VALUE) SIGN(VALUE) MOD(LINENO,3)

099 1 0
1 3.44 3.44 1 1
2 3.88 3.88 1 2
3 -6.27 6.27 -1 0
4 -6.82 6.82 -1 1
500 0 2
6 2.5 2.5 1 0
Notice the ABS returns the absolute value of VALUE.
SIGN tells us whether the value is positive, negative,

or zero. MOD gives us the remainder of LINENO/3.
All of the common numerical functions take one argu-
ment except MOD, which requires two.
Had we tried to include SQRT in this example our
query would look like this:
SELECT lineno, value, ABS(value), SQRT(value), SIGN(value),
MOD(lineno,2)
FROM function_illustrator
This would give us:
ERROR:
ORA-01428: argument '-6.27' is out of range
no rows selected
In this case, the problem is that there are negative
numbers in the value field and SQRT will not accept
such values in its domain.
Functions can be nested; we can have a function
operate on the value produced by another function. To
illustrate a nested function we can use the ABS func
-
tion to ensure that the SQRT function sees only a
positive domain. The following query handles both pos
-
itive and negative numbers:
6
Common Oracle Functions: A Function Review

×