Mastering Oracle SQL and SQL*Plus LEX DE HAAN... No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, reco
Trang 2Mastering Oracle SQL and SQL*Plus
LEX DE HAAN
Trang 3Mastering Oracle SQL and SQL*Plus
Copyright © 2005 by Lex de Haan
All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher
ISBN (pbk): 1-59059-448-7
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark
Lead Editor: Tony Davis
Technical Reviewers: Cary Millsap and Joakim Treugut
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, John Franklin, Jason Gilmore, Chris Mills, Dominic Shakeshaft, Jim Sumser
Project Manager: Beckie Stones
Copy Edit Manager: Nicole LeClerc
Copy Editor: Marilyn Smith
Production Manager: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor: Dina Quan
Proofreader: Liz Welch
Indexer: Michael Brinkman
Artist: Kinetic Publishing
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
Distributed to the book trade in the United States by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013, and outside the United States by Springer-Verlag GmbH & Co KG, Tiergartenstr 17, 69112 Heidelberg, Germany
In the United States: phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders@springer-ny.com, or visit http://www.springer-ny.com Outside the United States: fax +49 6221 345229, e-mail orders@springer.de,
or visit http://www.springer.de
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710 Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com The information in this book is distributed on an “as is” basis, without warranty Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work
The source code for this book is available to readers at http://www.apress.com in the Downloads section, and also on the author’s web site, at http://www.naturaljoin.nl
Trang 4Contents at a Glance
Foreword ix
About the Author xi
About the Technical Reviewers xiii
Acknowledgments xv
Introduction xvii
CHAPTER 1 Relational Database Systems and Oracle 1
CHAPTER 2 Introduction to SQL, iSQL*Plus, and SQL*Plus 25
CHAPTER 3 Data Definition, Part I 65
CHAPTER 4 Retrieval: The Basics 77
CHAPTER 5 Retrieval: Functions 113
CHAPTER 6 Data Manipulation 141
CHAPTER 7 Data Definition, Part II 159
CHAPTER 8 Retrieval: Multiple Tables and Aggregation 191
CHAPTER 9 Retrieval: Some Advanced Features 233
CHAPTER 10 Views 265
CHAPTER 11 SQL*Plus and iSQL*Plus 289
CHAPTER 12 Object-Relational Features 327
APPENDIX A Quick Reference to SQL and SQL*Plus 349
APPENDIX B Data Dictionary Overview 373
APPENDIX C The Seven Case Tables 381
APPENDIX D Answers to the Exercises 393
APPENDIX E Oracle Documentation, Web Sites, and Bibliography 443
INDEX 449
iii
Trang 6UNIQUE constraint 67, 167
unique identifiers 20
case entities 382
unique indexes 176
UNIQUE option
CREATE INDEX command 176
unnesting 332, 336–337
updatable join views 276
rules and restrictions 277
updatable views 17
UPDATE command 26–27, 337, 359
components 145
example 146
example using a subquery 147
read consistency 156
restrictions on updatable join views 277
via an inline updatable join 282
UPDATE object privilege 30–31
UPPER function 118, 355, 364
USER function 34, 297, 367
user-defined types 338
adding methods 328
constructor methods 328
creating 338–339
setting DEPTH attribute of DESCRIBE
command 339
user-defined variables, SQL*Plus 290, 292
declaring with DEFINE command 292
implicit user-defined variables 293
user-friendly prompting 294
example 294
USER_ data dictionary views 375
USER_CONSTRAINTS view 169
USER_CONS_COLUMNS view 169
USER_INDEXES view 75
USER_OBJECTS view 75
USER_RECYCLEBIN view 75
USER_SEQUENCES view 75
USER_SYNONYMS view 75
USER_TABLES view 75
USER_TAB_COLUMNS view 75
USER_TYPES data dictionary view
querying to view type definitions 330
USER_UPDATABLE_COLUMNS view 278
USER_VIEWS data dictionary view 75
querying 270
USING command 358
USING keyword 201
V
V$ data dictionary views 378
VALUES clause 358
INSERT command 27, 141
VAR function 317
VARCHAR datatype 67, 360
VARCHAR2 recommended 162
VARCHAR2 datatype 67–68 limits 161
recommends use of 162 VARIABLE command 301, 355 variables 34
variables, SQL*Plus 290 VARIANCE function 209, 357, 365 varrays 329
as collection datatypes 328 creating the array 329 populating the array with values 331 querying array columns 332, 334 VERIFY system variable 297, 355 suppressing setting 292 VERSIONS BETWEEN option 261, 358 flashback features 260
VERSIONS_ENDTIME pseudo column 261 VERSIONS_STARTTIME pseudo column 261 VIEW command 360
views compared to tables 267 creating from queries 268 creation 266
data manipulation 274–275 inline views 282
updatable join views 276 WITH CHECK OPTION clause 279–281 DESCRIBE command 270
getting information about views from data dictionary 269
introduction 265 joining three tables 268 materialized views 284 properties of 285 query rewrite 285 nonupdatable views 277 performance 283 replacing and dropping views 271 retrieving view definitions from data dictionary 270
uses maintaining logical data independence 274 simplifying data retrieval 271–272, 274 virtual tables, views as 266
volume 2 VSIZE function 367
W
WARNING option 351, 353 waterfall methods 6 WHEN THEN ELSE construct INTO clause 144
WHEN operator 363 WHENEVER command 353
■I N D E X 467
Trang 7WHERE clause 78
+ operator 204
ampersand (&) character 124
bitmap indexes and performance 176
compared to HAVING clause 216
correlated subqueries 282
equijoins 195
example 79
example of using with MOD function 116
example with simple condition 85
specifying conditions 85
UPDATE command 145
using functions 113
using parentheses 91
WHERE clause 28, 358
white space 79, 91
WIDTH_BUCKET function 364
wildcards 96, 362
windows 251
WITH CHECK OPTION clause
creating views 267, 280
restrictions on updatable join views 277
WITH clause 274, 358 subqueries 244–245 WITH GRANT OPTION 361 WITH READ ONLY option creating views 267 nonupdatable views 277 WORD_WRAP setting 356 WORD_WRAPPED option COLUMN command 310
workspace screen, iSQL*Plus 41
WRAP option 355, 356 WRAPPED option COLUMN command 310 WRAPPED system variable 354
Y
YEAR 127–128 year
formatting 367 specifying in SQL 33
■I N D E X
468