Mastering Oracle SQLby Sanjay Mishra and Alan Beaulieu Copyright © 2002 O’Reilly & Associates, Inc.. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered t
Trang 1Mastering Oracle SQL
Sanjay Mishra and Alan Beaulieu
Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo
Trang 2Mastering Oracle SQL
by Sanjay Mishra and Alan Beaulieu
Copyright © 2002 O’Reilly & Associates, Inc All rights reserved
Printed in the United States of America
Published by O’Reilly & Associates, Inc., 1005 Gravenstein Highway North,
Sebastopol, CA 95472
O’Reilly & Associates books may be purchased for educational, business, or sales promotional
use Online editions are also available for most titles (safari.oreilly.com) For more information contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor: Jonathan Gennick
Production Editor: Colleen Gorman
Cover Designers: Ellie Volckhausen and Emma Colby
Interior Designer: David Futato
Printing History:
April 2002: First Edition
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered
trademarks of O’Reilly & Associates, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear
in this book, and O’Reilly & Associates, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps Oracle® and all Oracle-based trademarks and logos are trademarks or registered trademarks of Oracle Corporation, Inc., in the United States and other countries O’Reilly & Associates, Inc., is independent of Oracle Corporation The association between the image of a lantern fly and the topic of mastering Oracle SQL is a trademark of O’Reilly & Associates, Inc
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions,or for damages resulting from the use of the information contained herein
ISBN: 0-596-00129-0
[M]
Trang 3About the Authors
Sanjay Mishra is a certified Oracle database administrator with more than ten years of
IT experience He has been involved in the design, architecture, and implementation of many mission-critical and decision support databases He has worked extensively in database architecture, database management, backup/recovery, performance tuning, Oracle Parallel Server, and parallel execution He has a Bachelor of Science degree in Electrical Engineering and a Master of Engineering degree in Systems Science and
Auto-mation He is the coauthor of Oracle Parallel Processing and Oracle SQL Loader: The
Definitive Guide (both published by O’Reilly) Presently, he works as a database
archi-tect at Dallas-based i2 Technologies, and can be reached at Sanjay_Mishra@i2.com.
Alan Beaulieu has been designing, building, and implementing custom database
appli-cations for over 13 years He currently runs his own consulting company that specializes in designing Oracle databases and supporting services in the fields of finan-cial services and telecommunications In building large databases for both OLTPand OLAP environments, Alan utilizes such Oracle features as Parallel Query, Partitioning, and Parallel Server Alan has a Bachelor of Science degree in Operations Research from the Cornell University School of Engineering He lives in Massachusetts with his wife
and two daughters and can be reached at albeau_mosql@yahoo.com.
Colophon
Our look is the result of reader comments, our own experimentation, and feedback from distribution channels Distinctive covers complement our distinctive approach
to technical topics, breathing personality and life into potentially dry subjects.
The insect on the cover of Mastering Oracle SQL is a lantern fly The lantern fly is
mostly tropical, with a wingspan of up to six inches The lantern fly’s elongated head is
an evolutionary adaptation called automimicry, in which parts of the body are disguised
or artifically shifted to other areas to confuse predators: the lantern fly’s head looks like
a tail, and its tail looks like a head On the rear it has artificial eyes and antennae.
Colleen Gorman was the production editor and copyeditor for Mastering Oracle SQL.
Sheryl Avruch and Ann Schirmer provided quality control Tom Dinse wrote the index Ellie Volckhausen and Emma Colby designed the cover of this book, based on a series design by Edie Freedman The cover image is a 19th-century engraving from Johnson’s Natural History Emma Colby produced the cover layout with Quark-XPress 4.1 using Adobe’s ITC Garamond font.
David Futato designed the interior layout Neil Walls converted the files from Microsoft Word to FrameMaker 5.5.6 using tools written in Perl by Erik Ray, Jason McIntosh, and Neil Walls, as well as tools written by Mike Sierra The text font is Linotype Birka; the heading font is Adobe Myriad Condensed; and the code font is LucasFont’s TheSans Mono Condensed The illustrations that appear in the book were produced by Robert Romano and Jessamyn Read using Macromedia FreeHand
9 and Adobe Photoshop 6 The tip and warning icons were drawn by Christopher Bing This colophon was written by Colleen Gorman.
Trang 4I dedicate this book to my father.
I wish he were alive to see this book.
—Sanjay Mishra
To my daughters, Michelle and Nicole.
—Alan Beaulieu
Trang 6This is the Title of the Book, eMatter Edition Copyright © 2002 O’Reilly & Associates, Inc All rights reserved.
320 | Index
tables (continued)
comparing, set operations, 149–151
customer, SELECT statement example, 6
entities, 4
equi-joins compared to
non-equi-joins, 29 inner joins, 26
joins, subqueries, 42
key-preserved, join views and, 43
modifying
UPDATE statement, 16 without WHERE clause, 14 NOT NULL columns, INSERT statement
and, 11 object type, 204
outer joins, 30–32
full, 34–37 restrictions, 32 partitioning, overview, 192
self joins, 37
non-equi, 39–42 outer, 38 self-referential integrity constraints, 158
terminology, 3
hierarchical queries, 159
text, date formatting, case-sensitivity
rules, 106 time
A.M./P.M indicators, 105
fractional seconds
overview, 126 TIMESTAMP datatype, 126 TIMESTAMP WITH LOCAL TIME ZONE datatype, 129
TIMESTAMP WITH TIME ZONE datatype, 127
functions, 135–143
rounding and truncating dates, 117
time zones
database, 125
default, 125
NEW_TIME function, 119
overview, 125
session, 126
TIMESTAMP datatype, 126
TIMESTAMP WITH LOCAL TIME ZONE
datatype, 129 TIMESTAMP WITH TIME ZONE
datatype, 127 TO_CHAR function, 96
combining with TO_DATE function, 100
overview, 100
TO_DATE function, 96 combining with TO_CHAR function, 100 default date format, 97
overview, 96 specifying format, 98 TO_DSINTERVAL function, 142 top-n/bottom-n queries (analytic ranking
functions), 277 TO_TIMESTAMP function, 138 TO_TIMESTAMP_TZ function, 139 TO_YMINTERVAL function, 142 traversing hierarchical trees, 163 TRUNC function, 116
date pivot tables, 121 date ranges and, 121 truncating dates, 116–119 TRUST keyword, stored procedures, 222 two-digit years, 107
TZ_OFFSET function, 143
U
underscore (_), pattern-matching
character, 21 UNION ALL set operator, 145, 146 comparing tables, 149
UNION clause, data sets, creating
custom, 80 union compatibility conditions, 144 UNION operation compared to UNION
ALL, 300 UNION queries, 230 ANSI join syntax and, 54 full outer joins, 36 UNION set operator, 145, 146 UPDATE statement, 12 CASE expression, 184 collections and, 211 DECODE function, 184 DML (Data Manipulation Language), 1 inline views, 86
join views, 47 multiple-column subqueries, 74 optional updates, 185
selective aggregation, 186–189 WHERE clause and, 16 USER_UPDATABLE_COLUMNS data
dictionary view, 48 USING clause, 50
UTC (Coordinated Universal Time), 125
Trang 7This is the Title of the Book, eMatter Edition Copyright © 2002 O’Reilly & Associates, Inc All rights reserved.
Index | 321
V
VALUE function, returning objects, 204
VARCHAR2 datatype, TO_DATE
datatype, 96 variable arrays, 208
variables, blind, 303
varrays, 208
vertical joins, 144
vertical partitioning, 193
viewing all in schema, 48
views
data dictionary, USER_UPDATABLE_
COLUMNS, 48 hierarchical queries, 174
inline, 77
aggregate queries, overcoming limitations of, 85
creating data sets, 80–83 DML statements, 86 execution, 78 hiding columns with WITH CHECK OPTION, 88
hierarchical queries, overcoming limitations of, 83
overview, 77 selective aggregation, 188 joins, DML statements and, 43–49
outer joins, 33
partitions, 199
stored functions, 218
W
weekends, date math and, 123
weeks
date math, 112
ISO standard, 109
WHERE clause, 64
capabilities of, 15
columns, restricting access, 88
compared to HAVING clause, 299 conditions, 15
components of, 18 equality/inequality, 19 matching, 21
membership, 20 range, 20 evaluation, 16–18 conditions, 15 GROUP BY clause, 64 HAVING clause and, 66 hierarchical queries, filtering, 168 join conditions, 24, 28
logical operators, 16 new join syntax and, 49 noncorrelated subqueries, 70 NULL expression, 22–24 outer join operator (+), 31 partition pruning, 199 subqueries, 69 tips for using, 24 UPDATE statement and, 16 value of, 14
WHERE clause (SELECT statements), 6 WIDTH_BUCKET analytic function, 280 windowing analytic functions, 286–289 WITH CHECK OPTION clause, 46 WITH CHECK OPTION, hiding
columns, 88 working days, calculating, 123
WW (ISO week) indicator, 109
Y
years A.D./B.C indicators, 104 finding number between dates, 115 ISO standard, 110
two-digit, 107
YY (year) indicator, 107