Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780596006327/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Preface
SQL is the language for accessing a relational database. SQL providesa set of statements for storing and retrieving data to and from arelational database. It has gained steadily in popularity ever sincethe first relational database was unleashed upon the world. Otherlanguages have been put forth, but SQL is now accepted as thestandard language for almost all relational database implementations,including Oracle.
SQL is different from other programming languages because it isnonprocedural. Unlike programs in other languages, where you specifythe sequence of steps to be performed, a SQL program (moreappropriately called a SQL statement) only expresses the desiredresult. The responsibility for determining how the data will beprocessed to generate the desired result is left to the databasemanagement system. The nonprocedural nature of SQL makes it easier toaccess data in application programs.
If you are using an Oracle database, SQL is the interface you use toaccess the data stored in your database. SQL allows you to createdatabase structures such as tables (to store your data), views, andindexes. SQL allows you to insert data into the database, and toretrieve that stored data in a desired format (for example, you mightsort it). Finally, SQL allows you to modify, delete, and otherwisemanipulate your stored data. SQL is the key to everything you do withthe database. It's important to know how to get themost out of that interface. Mastery over the SQL language is one ofthe most vital requirements of a database developer or databaseadministrator.
Why We Wrote This Book
Our motivation for writing this book stems from our own experienceslearning how to use the Oracle database and Oracle'simplementation of the SQL language. Oracle's SQLdocumentation consists of a reference manual thatdoesn't go into details about the practicalusefulness of the various SQL features that Oracle supports. Nor doesthe manual present complex, real-life examples.
When we looked for help with SQL in the computer book market, wefound that there are really two types of SQL books available. Mostare the reference type that describe features and syntax, but thatdon't tell you how to apply that knowledge toreal-life problems. The other type of book, very few-in-number,discusses the application of SQL in a dry and theoretical stylewithout using any particular vendor'simplementation. Since every database vendor implements their ownvariation of SQL, we find books based on"standard" SQL to be of limitedusefulness.
In writing this book, we decided to write a practical book focusedsquarely on Oracle's version of SQL. Oracle is themarket-leading database, and it's also the databaseon which we've honed our SQL expertise. In thisbook, we not only cover the most important and useful ofOracle's SQL features, but we show ways to applythem to solve specific problems.
What's New in Oracle SQL?
When we wrote the first edition of this book,Oracle9 i had just come out, and we managed tocover some of the interesting and new features in that release of thedatabase. Now, Oracle Database 10 g has just beenreleased, and there are even more new features to talk about:
A new, MODEL clause has been added to the SELECT statement, enablingyou to write queries that perform spreadsheet-like calculationsagainst multidimensional arrays created from data you select from thedatabase.
Oracle has added support for using regular expressions from SQL, andwith a vengeance. Not only can you use regular expressions to selectdata, but also to manipulate data in various, useful ways. Forexample, you can perform regular expression search-and-replaceoperations. No other database vendor that we know of offers suchpowerful, regular expression functionality.
XML is everywhere these days, and that hasn't goneunnoticed in the world of SQL. The ANSI/ISO folk have created theSQL/XML standard, which defines mechanisms for selecting relationaldata and presenting it in XML form. Oracle supports this standard,which involves several, new SQL functions. Oracle also now supportsXML as a native data type.
These are just the big features, which, of course, we cover in thissecond edition. In addition, we cover many small updates to OracleSQL, such as the multiset union operators that enable you to performset operations involving nested table collections.
Finally, we've worked carefully together as a team,not only with each other, but also with our editor, to ensure thatall examples in this book are drawn from a single data set.You'll be able to download that data set from thisbook's catalog page. You can then use it to followalong with our examples.
Objectives of This Book
The single most important objective of this book is to help youharness the power of Oracle SQL to the maximum extent possible. Youwill learn to:
Understand the features and capabilities of the SQL language, asimplemented by Oracle.
Use complex SQL features, such as outer joins, correlated subqueries,hierarchical queries, grouping operations, and analytical queries.
Use DECODE and CASE to implement conditional logic in your SQLqueries.
Write SQL statements that operate against partitions, objects, andcollections, such as nested tables and variable arrays.
Use the new SQL features introduced in Oracle Database10 g , such as regular expressions and interrowcalculations.
Use best-practices to write efficient, maintainable SQL queries.
One topic that is important to us and many of our readers, but whichis not explicitly discussed in this book is SQL tuning. Tuning tipsare sprinkled throughout the book, but we do not include a chapter ontuning for the following reasons:
Tuning is a large topic, and reasonable coverage of SQL tuning wouldeasily double or triple the size of this book.
There are already many excellent Oracle-specific and general-purposetuning books on the market, whereas there are very few books (in ouropinion, exactly one) that thoroughly explore the feature set ofOracle SQL.
In many ways, mastery of Oracle's SQL implementationis the most important tool in your tuning toolkit.
With this book under your belt, you will be less likely to write SQLstatements that perform badly, and you will be able to employmultiple strategies to rework existing statements.