1. Trang chủ
  2. » Công Nghệ Thông Tin

oracle sql recipes

578 918 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 578
Dung lượng 4,82 MB

Nội dung

this print for content only—size & color not accurate spine = x.xxx" xxx page count Allen Bryla Kuhn Oracle SQL Recipes The eXPeRT’s VOIce ® In ORacle Oracle SQL Recipes A Problem-Solution Approach cYan MaGenTa YellOW BlacK PanTOne 123 c Grant Allen, Bob Bryla, Darl Kuhn Companion eBook Available An example-based approach to solving common developer and administration problems with Oracle SQL BOOKs fOR PROfessIOnals BY PROfessIOnals ® Oracle SQL Recipes: A Problem-Solution Approach Dear Reader, Oracle SQL Recipes is designed to feed your appetite for solutions to all manner of query problems working with data in Oracle. If you’ve ever struggled to report on data using SQL alone, or marveled at the elegant and skillful way some write SQL for Oracle to solve seemingly intractable problems, you’ll want to delve into this book, as it will show you the techniques for creating queries like a pro. Using this book, you’ll learn the many intricacies of SQL in Oracle, and the sheer power and features that are at your command to tackle problems as diverse as statistical modeling, multi-level financial reports, and even dynami- cally generating web pages directly from your database using just SQL. Each issue we tackle is presented in problem/solution format. First we state a prob- lem to be solved. Then we show the solution in SQL. Lastly, we discuss in detail any caveats you need to know, as well as why the solution works. Oracle SQL Recipes is suitable for readers at any level of SQL proficiency, and includes reci- pes to expand the skills of SQL aficionados from beginner all the way through to seasoned experts. Whether you are a user of the data in an Oracle database, an application developer, or a DBA, there are topics in this book to cater to your needs. We’re excited to present you with this collection of recipes. As authors, we wrote this book to highlight the power and capability of SQL within Oracle, and how it can solve many of the problems that are often deemed too difficult to write outside of application code. Armed with the knowledge herein, you’ll master the latest and greatest Oracle database capabilities, and be improvising your own recipes in no time. We hope you find the recipes fit many of the real-world problems you encounter working with Oracle and SQL every day, and look forward to see- ing the recipes you create from this inspiration. Grant Allen, Bob Bryla, Darl Kuhn US $49.99 Shelve in Databases/Oracle User level: Beginner–Advanced THE APRESS ROADMAP Troubleshooting Oracle Performance Oracle SQL Recipes Beginning Oracle SQL www.apress.com Companion eBook See last page for details on $10 eBook version )3".        Darl Kuhn author of Linux Recipes for Oracle DBAs Grant Allen author of Beginning DB2 Bob Bryla author of Beginning PHP and Oracle www.it-ebooks.info this print for content only—size & color not accurate spine = x.xxx" xxx page count Allen Bryla Kuhn Oracle SQL Recipes The eXPeRT’s VOIce ® In ORacle Oracle SQL Recipes A Problem-Solution Approach cYan MaGenTa YellOW BlacK PanTOne 123 c Grant Allen, Bob Bryla, Darl Kuhn Companion eBook Available An example-based approach to solving common developer and administration problems with Oracle SQL BOOKs fOR PROfessIOnals BY PROfessIOnals ® Oracle SQL Recipes: A Problem-Solution Approach Dear Reader, Oracle SQL Recipes is designed to feed your appetite for solutions to all manner of query problems working with data in Oracle. If you’ve ever struggled to report on data using SQL alone, or marveled at the elegant and skillful way some write SQL for Oracle to solve seemingly intractable problems, you’ll want to delve into this book, as it will show you the techniques for creating queries like a pro. Using this book, you’ll learn the many intricacies of SQL in Oracle, and the sheer power and features that are at your command to tackle problems as diverse as statistical modeling, multi-level financial reports, and even dynami- cally generating web pages directly from your database using just SQL. Each issue we tackle is presented in problem/solution format. First we state a prob- lem to be solved. Then we show the solution in SQL. Lastly, we discuss in detail any caveats you need to know, as well as why the solution works. Oracle SQL Recipes is suitable for readers at any level of SQL proficiency, and includes reci- pes to expand the skills of SQL aficionados from beginner all the way through to seasoned experts. Whether you are a user of the data in an Oracle database, an application developer, or a DBA, there are topics in this book to cater to your needs. We’re excited to present you with this collection of recipes. As authors, we wrote this book to highlight the power and capability of SQL within Oracle, and how it can solve many of the problems that are often deemed too difficult to write outside of application code. Armed with the knowledge herein, you’ll master the latest and greatest Oracle database capabilities, and be improvising your own recipes in no time. We hope you find the recipes fit many of the real-world problems you encounter working with Oracle and SQL every day, and look forward to see- ing the recipes you create from this inspiration. Grant Allen, Bob Bryla, Darl Kuhn US $49.99 Shelve in Databases/Oracle User level: Beginner–Advanced THE APRESS ROADMAP Troubleshooting Oracle Performance Oracle SQL Recipes Beginning Oracle SQL www.apress.com Companion eBook See last page for details on $10 eBook version )3".        Darl Kuhn author of Linux Recipes for Oracle DBAs Grant Allen author of Beginning DB2 Bob Bryla author of Beginning PHP and Oracle www.it-ebooks.info Oracle SQL Recipes A Problem-Solution Approach ■ ■ ■ Grant Allen, Bob Bryla, and Darl Kuhn www.it-ebooks.info ii Oracle SQL Recipes: A Problem-Solution Approach Copyright © 2009 by Grant Allen, Bob Bryla, and Darl Kuhn 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-13 (pbk): 978-1-4302-2509-6 ISBN-13 (electronic): 978-1-4302-2510-2 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. President and Publisher: Paul Manning Lead Editor: Jonathan Gennick Technical Reviewer: Stephane Faroult Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Debra Kelly Copy Editor: Sharon Terdeman Compositor: LaurelTech Indexer: Becky Hornyak Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com. For information on translations, please e-mail info@apress.com, or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales– eBook Licensing web page at http://www.apress.com/info/bulksales. 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. www.it-ebooks.info To George, for teaching me everything he knows. –Grant To the gang at home, even with my long hours I was still able to make the football, volleyball, and basketball games! –Bob To Heidi, Brandi, and Lisa. –Darl www.it-ebooks.info www.it-ebooks.info v Contents at a Glance Contents vii About the Authors xviii About the Technical Reviewer xix Acknowledgments xxi Introduction xxiii PART 1    Foundation of Data Manipulation 1 Chapter 1: The Basics 3 Chapter 2: Summarizing and Aggregating Data 23 Chapter 3: Querying from Multiple Tables 59 Chapter 4: Creating and Deriving Data 83 Chapter 5: Common Query Patterns 109 PART 2    Data Types and Their Problems 141 Chapter 6: Working with Date and Time Values 143 Chapter 7: Strings 167 Chapter 8: Working with Numbers 187 PART3    Your Development Environment 207 Chapter 9: Managing Transactions 209 Chapter 10: Data Dictionary 229 www.it-ebooks.info ■ CONTENTS AT A GLANCE vi PART 4    Data Dictionary 261 Chapter 11: Common Reporting Problems 263 Chapter 12: Cleansing Data 287 Chapter 13: Tree-Structured Data 313 Chapter 14: Working with XML Data 335 Chapter 15: Partitioning 351 Chapter 16: LOBs 383 PART 5    Administration 401 Chapter 17: Database Administration 403 Chapter 18: Object Mamagement 425 Chapter 19: SQL Monitoring and Tuning 457 Chapter 20: Database Troubleshooting 497 Index 519 www.it-ebooks.info ■ CONTENTS vii Contents ■About the Authors xviii ■About the Technical Reviewer xix ■Acknowledgments xxi ■Introduction xxiii PART 1 ■ ■ ■ Foundation of Data Manipulation 1 ■Chapter 1: The Basics 3 1-1. Retrieving Data from a Table 3 1-2. Selecting All Columns from a Table 5 1-3. Sorting Your Results 6 1-4. Adding Rows to a Table 7 1-5. Copying Rows from One Table to Another 9 1-6. Copying Data in Bulk from One Table to Another 10 1-7. Changing Values in a Row 10 1-8. Updating Multiple Fields with One Statement 11 1-9. Removing Unwanted Rows from a Table 12 1-10. Removing All Rows from a Table 13 1-11. Selecting from the Results of Another Query 14 1-12. Basing a Where Condition on a Query 15 1-13. Finding and Eliminating NULLs in Queries 16 1-14. Sorting as a Person Expects 18 www.it-ebooks.info ■ CONTENTS viii 1-15. Enabling Other Sorting and Comparison Options 20 1-16. Conditional Inserting or Updating Based on Existence 21 ■Chapter 2: Summarizing and Aggregating Data 23 2-1. Summarizing the Values in a Column 23 2-2. Summarizing Data for Different Groups 26 2-3. Grouping Data by Multiple Fields 27 2-4. Ignoring Groups in Aggregate Data Sets 28 2-5. Aggregating Data at Multiple Levels 30 2-6. Using Aggregate Results in Other Queries 32 2-7. Counting Members in Groups and Sets 33 2-8. Finding Duplicates and Unique Values in a Table 35 2-9. Calculating Totals and Subtotals 37 2-10. Building Your Own Aggregate Function 39 2-11. Accessing Values from Subsequent or Preceding Rows 42 2-12. Assigning Ranking Values to Rows in a Query Result 45 2-13. Finding First and Last Values within a Group 47 2-14. Performing Aggregations over Moving Windows 49 2-15. Removing Duplicate Rows Based on a Subset of Columns 51 2-16. Finding Sequence Gaps in a Table 55 ■Chapter 3: Querying from Multiple Tables 59 3-1. Joining Corresponding Rows from Two or More Tables 60 3-2. Stacking Query Results Vertically 62 3-3. Writing an Optional Join 64 3-4. Making a Join Optional in Both Directions 65 3-5. Removing Rows Based on Data in Other Tables 67 3-6. Finding Matched Data Across Tables 68 www.it-ebooks.info [...]... aspect of writing SQL for Oracle databases Part 1 deals with the fundamentals of SQL for Oracle and the basics of all facets of SQL Part 2 covers the intricacies of Oracle data types and recipes to manage and manipulate data in different forms Part 3 discusses working with Oracle as a development environment and introduces recipes to help the developer and DBA solve complex tasks in SQL Part 4 is a compendium... keen users of it SQL Developer is part of the standard install as of Oracle Database 11g, so you may already have it without realizing! We’re happy for you to use any tool you like when working with these recipes, but if you are in any doubt, start using SQL Developer now, and you’ll be glad you did Working With Example Data for SQL Recipes For many of the examples we'll use in Oracle SQL Recipes, we'll... developers, report writers, or even end users No matter what level of expertise you currently possess for writing and using SQL, there are recipes within Oracle SQL Recipes that will appeal to you This book is not designed to teach you SQL or to act as a facsimile of the Oracle SQL reference Instead, what you’ll find are elegant and sometimes tricky solutions to real problems that you’ll never see presented... We’ve tailored the recipes to take you to the heights of a gourmet chef, at least as far as SQL for Oracle is concerned Though we’ve split the recipes into logical chapters covering topics like data types or report generation, you should feel free to sample and use the recipes in any order, as well as in any combination Who This Book Is For This book is for everyone working with Oracle SQL, be they DBAs,... 453 ■Chapter 19: SQL Monitoring and Tuning 457 19-1 Monitoring Real-Time SQL Execution Statistics 457 19-2 Displaying a Query's Progress in the Execution Plan 459 19-3 Determining How Much SQL Work Is Left 462 19-4 Identifying Resource-Intensive SQL Statements 463 19-5 Using Oracle Performance Reports to Identify Resource-Intensive SQL 465 19-6 Using the Operating... administrators and shows how SQL can be applied to many of the problems and tasks administrators face on a daily basis xxiii www.it-ebooks.info ■ INTRODUCTION Choosing Tools All of the recipes we’ve included in Oracle SQL Recipes are designed to work without the aid of special tools or applications You should be able to pick a recipe and use it in an ad-hoc fashion through SQL* Plus, or include it in... professional ■Darl Kuhn is an Oracle DBA and developer He has coauthored three other books: Linux Recipes for Oracle DBAs, RMAN Recipes for Oracle Database 11g, and Oracle RMAN Pocket Reference He also teaches advanced database courses at Regis University and performs volunteer DBA work for the Rocky Mountain Oracle Users Group He has a graduate degree from Colorado State University and currently lives... site, at www.oraclesqlrecipes.com To contact Bob Bryla directly, e-mail him at rjbdba@gmail.com xxv www.it-ebooks.info www.it-ebooks.info PART 1 ■■■ Foundations of Data Manipulation www.it-ebooks.info www.it-ebooks.info CHAPTER 1 ■■■ The Basics This chapter presents lots of basic recipes to get you started—or rekindle old memories—on the core building blocks of SQL statements We’ll show you recipes for... selecting, changing, and removing data from Oracle database tables, plus some common options you usually want to include when doing this kind of work Those of you with a firm grounding in SQL should feel free to delve into this chapter in an à la carte fashion We’ve included one or two clever recipes at this early stage to make sure you get the most from Oracle SQL Recipes from the very first chapter To... the Oracle OCI or similar client library Those of us who’ve been avid users of SQL* Plus for some time appreciate that its “vintage” approach to formatting and aesthetics–OK, we mean 1980’s throw-back output with all the beauty the Courier font can offer–sometimes takes something away from the recipes, especially those that show off neat tricks with output formatting For years, Oracle has made SQL Developer . and administration problems with Oracle SQL BOOKs fOR PROfessIOnals BY PROfessIOnals ® Oracle SQL Recipes: A Problem-Solution Approach Dear Reader, Oracle SQL Recipes is designed to feed your. and administration problems with Oracle SQL BOOKs fOR PROfessIOnals BY PROfessIOnals ® Oracle SQL Recipes: A Problem-Solution Approach Dear Reader, Oracle SQL Recipes is designed to feed your. accurate spine = x.xxx" xxx page count Allen Bryla Kuhn Oracle SQL Recipes The eXPeRT’s VOIce ® In ORacle Oracle SQL Recipes A Problem-Solution Approach cYan MaGenTa YellOW BlacK

Ngày đăng: 24/04/2014, 15:43

Xem thêm

TỪ KHÓA LIÊN QUAN

w