Oracle PL/SQL Best Practices
Total Page:16
File Type:pdf, Size:1020Kb
Download from Wow! eBook <www.wowebook.com> Oracle PL/SQLBestPractices Other Oracle resources from O’Reilly Related titles Learning Oracle PL/SQL Oracle PL/SQL Language Oracle Essentials Pocket Reference Oracle in a Nutshell Oracle PL/SQL Oracle PL/SQL Programming Developer’s Workbook Toad Pocket Reference for Oracle PL/SQL for DBAs Oracle Oracle Books oracle.oreilly.com is a complete catalog of O’Reilly’s Resource Center books on Oracle and related technologies, including sam- ple chapters and code examples. oreillynet.com is the essential portal for developers inter- ested in open and emerging technologies, including new platforms, programming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in documenting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online reference library for programmers and IT professionals. Conduct searches across more than 1,000 books. Sub- scribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Book- shelf from cover to cover or simply flip to the page you need. Try it today for free. SECOND EDITION Oracle PL/SQL Best Practices Steven Feuerstein Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Oracle PL/SQL Best Practices, Second Edition by Steven Feuerstein Copyright © 2008 Steven Feuerstein. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA95472. O’Reilly 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 [email protected]. Editors: Deborah Russell and Mary Treseler Cover Designer: Karen Montgomery Production Editor: Rachel Monaghan Interior Designer: David Futato Proofreader: Rachel Monaghan Illustrator: Robert Romano Indexer: Angela Howard Printing History: April 2001: First Edition. October 2007: Second Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Oracle PL/SQL Best Practices, the image of red wood ants, and related trade dress are trademarks of O’Reilly Media, 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 Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN-10: 0-596-51410-7 ISBN-13: 978-0-596-51410-5 [M] To the many PL/SQL programmers around the world who have enriched my life and made PL/SQL the amazing success that it is. Table of Contents Preface . ix 1. The Big Picture . 1 Successful Applications Are Never an Accident 2 Best Practices for Successful Applications 5 2. Real Developers Follow Standards . 37 Best Practices for Developing and Using Standards 38 3. Life After Compilation . 51 Testing, Tracing, and Debugging 52 Best Practices for Testing, Tracing, and Debugging 53 4. What’s Code Without Variables? . 73 Best Practices for Declaring Variables and Data Structures 73 Best Practices for Using Variables and Data Structures 81 Best Practices for Declaring and Using Package Variables 88 5. Developer As Traffic Cop . 95 Best Practices for Conditional and Boolean Logic 95 Best Practices for Loop Processing 101 Best Practices for Branching Logic 111 6. Doing the Right Thing When Stuff Goes Wrong . 114 Best Practices for Understanding Error Handling 115 Best Practices for Nitty-Gritty, Everyday Exception Programming 131 Best Practices for Coding Defensively 135 vii 7. Break Your Addiction to SQL . 140 SQL Is Bad! 141 General SQL Best Practices 144 Best Practices for Querying Data from PL/SQL 158 Best Practices for Changing Data from PL/SQL 163 Best Practices for Dynamic SQL 168 8. Playing with Blocks (of Code) . 179 Best Practices for Parameters 180 Best Practices for Procedures and Functions 186 Best Practices for Packages 205 Best Practices for Triggers 211 9. My Code Runs Faster Than Your Code . 218 Best Practices for Finding Slow Code 219 Best Practices for High-Impact Tuning 225 Best Practices for Other Tuning 240 A. Best Practices Quick Reference . 247 B. Resources for PL/SQL Developers . 252 Index . 259 viii | Table of Contents Preface I love getting started on new projects (and I include working on new editions of existing books in that general category). It is the perfect opportunity to say to myself: “I am going to get it right this time!” That fantasy usually persists only for days (or maybe weeks) into a project before it fades, but in the case of the second edition of Oracle PL/SQL Best Practices,I managed to live out my fantasy all the way through. You are holding the result in your hands, and I hope you enjoy reading and learning from it as much as I enjoyed writing it. Here’s how I managed this remarkable feat: I took a vow to not let best practices get boring. Now, don’t get me wrong. I liked the first edition of this book, and so did its many readers. Yet in hindsight, I feel as if I took a right turn when I should have kept going straight. My first book, Oracle PL/SQL Programming, has been extremely popular over the years, and many people have told me how much they wnload from Wow! eBook <www.wowebook.com> like the sense of humor, the anecdotes, and the many detailed examples. o D Several years after the publication of Oracle PL/SQL Programming, I wrote Oracle PL/SQL Best Practices. And somehow, for reasons I cannot recall, I managed to make this book a somewhat preachy and rigidly structured text. Luckily for me, developers seem to like lots of structure and don’t mind too much being preached at by people they trust! But as I considered revamping this book for its second edition, I found myself thinking: best practices are really important, but that doesn’t mean they have to be serious—they can be fun and entertaining (as much for me to write as for you to read)! So that’s what I did: I had fun writing this book. I sacrificed some of the rigidity of structure, emphasized practicality over theoretical usefulness, and generally came down off my perch (don’t worry—there are still more than enough rants and soapboxing!). In this second edition, I’ve tried to make the discussion a lot more interesting by sharing many of my ideas about best practices through ix stories about the successes and failures of the employees of the fictitious company, My Flimsy Excuse, Inc., and the adventures of its development team (the members of the team are described later in the Preface). Of course, I have also updated the text to keep pace with Oracle Corporation’s implementation of new PL/SQL features, including those in Oracle Database 11g. Why Best Practices? My intention with this book is to provide you with a set of best practices—guide- lines for building programs and applications—that will improve (and, I hope, transform) the PL/SQL code you write. Some of these best practices are high-level recommendations for how to approach designing and writing your overall appli- cations. Other best practices are much more oriented to day-to-day programming practice. In most of my recommendations, I include references to code or tools that you can download that will make it easier for you to apply these recommendations. Chapter 1, The Big Picture, covers the high-level recommendations for writing high-quality code. The remaining chapters of the book explore how to apply specific guidance to the everyday realities of software development. Before we delve into these specific recommendations, though, I would like to take advan- tage of this Preface to offer some thoughts on the role of software developers— and the nature of our jobs—in the big, wide world “out there” beyond our code. Best Practices and the Real World Clearly, software developers want to (and should) pay attention to best practices so that they will be able to create more successful applications. I also believe, however, that the responsibility of developers goes well beyond the specific projects on which they are working. After all, you are one of the people who make software possible, who therefore make computer systems like the Internet possible. That is an awesome amount of power, but as Spider Man’s uncle tells us, “With great power comes great responsibility.” From this perspective, it is very important to understand that you are not just a cog in the corporate wheel. When you are told to write a program, you should engage fully with that request. You should make sure not only that you under- stand in detail what it is the user wants you to write, but also that you appreciate how this program will meet its business objectives. As a programmer, you are trained to think logically about the challenges in front of you. This rational thought process is not something to be taken for granted. Many other humans have never received such training and don’t know how to reason their way through complex and thorny problems.