This book is dedicated to my family.Even though you’re far away, you are still in my heart.
About the AuthorJason Price is a freelance consultant and former product manager of OracleCorporation. He has contributed to many of Oracle’s products, including thedatabase, the application server, and several of the CRM applications. Jasonis an Oracle Certified Database Administrator and Application Developer,and has more than 15 years of experience in the software industry. Jason haswritten many books on Oracle, Java, and .NET. Jason holds a Bachelor ofScience degree (with honors) in physics from the University of Bristol, England.About the Technical EditorScott Mikolaitis is an applications architect at Oracle Corporation and has workedat Oracle for over ten years. He performs prototyping and standards developmentfor the SOA technology in Oracle Fusion. Scott also enjoys working with webservices in Java as well as Jabber for human and system interaction patterns.He spends his spare time on DIY home improvement and gas-fueled RC cars.
Acknowledgments hanks to the wonderful people at McGraw-Hill, including Lisa McClain,T Mandy Canales, Carl Wikander, and Laura Stone. Thanks also to Scott Mikolaitis for his thorough technical review. xxiii
Introduction oday’s database management systems are accessed using a standard language T known as Structured Query Language, or SQL. Among other things, SQL allows you to retrieve, add, update, and delete information in a database. In this book, you’ll learn how to master SQL, and you’ll find a wealth of practical examples. You can also get all the scripts and programs featured in this bookonline (see the last section, “Retrieving the Examples,” for details). With this book, you will ■ Master standard SQL, as well as the extensions developed by Oracle Corporation for use with the specific features of the Oracle database. ■ Explore PL/SQL (Procedural Language/SQL), which is built on top of SQL and enables you to write programs that contain SQL statements. ■ Use SQL*Plus to execute SQL statements, scripts, and reports; SQL*Plus is a tool that allows you to interact with the database. ■ Execute queries, inserts, updates, and deletes against a database. ■ Create database tables, sequences, indexes, views, and users. ■ Perform transactions containing multiple SQL statements. ■ Define database object types and create object tables to handle advanced data. ■ Use large objects to handle multimedia files containing images, music, and movies. ■ Perform complex calculations using analytic functions. ■ Use all the very latest Oracle Database 11g features such as PIVOT and UNPIVOT, flashback archives, and much more. ■ Implement high-performance tuning techniques to make your SQL statements really fly. xxv
xxvi Oracle Database 11g SQL ■ Write Java programs to access an Oracle database using JDBC. ■ Explore the XML capabilities of the Oracle database. This book contains 17 chapters and one appendix. Chapter 1: Introduction In this chapter, you’ll learn about relational databases, be introduced to SQL, see a few simple queries, use SQL*Plus and SQL Developer to execute queries, and briefly see PL/SQL. Chapter 2: Retrieving Information from Database Tables You’ll explore how to retrieve information from one or more database tables using SELECT statements, use arithmetic expressions to perform calculations, filter rows using a WHERE clause, and sort the rows retrieved from a table. Chapter 3: Using SQL*Plus In this chapter, you’ll use SQL*Plus to view a table’s structure, edit a SQL statement, save and run scripts, format column output, define and use variables, and create reports. Chapter 4: Using Simple Functions In this chapter, you’ll learn about some of the Oracle database’s built-in functions. A function can accept input parameters and returns an output parameter. Functions allow you to perform tasks such as computing averages and square roots of numbers. Chapter 5: Storing and Processing Dates and Times You’ll learn how the Oracle database processes and stores dates and times, collectively known as datetimes. You’ll also learn about timestamps that allow you to store a specific date and time, and time intervals that allow you to store a length of time. Chapter 6: Subqueries You’ll learn how to place a SELECT statement within an outer SQL statement. The inner SELECT statement is known as a subquery. You’ll learn about the different types of subqueries and see how subqueries allow you to build up very complex statements from simple components. Chapter 7: Advanced Queries In this chapter, you’ll learn how to perform queries containing advanced operators and functions such as: set operators that combine rows returned by multiple queries, the TRANSLATE() function to convert characters in one string to characters in another string, the DECODE() function to search a set of values for a certain value, the CASE expression to perform if-then-else logic, and the ROLLUP and CUBE clauses to return rows containing subtotals. You’ll learn about the analytic functions that enable you to perform complex calculations such as finding the top-selling product type for each month, the top salespersons, and so on. You’ll see how to perform queries against data that is organized into a hierarchy. You’ll also explore the MODEL clause, which performs inter-row calculations. Finally, you’ll see the new Oracle Database 11g PIVOT and UNPIVOT clauses, which are useful for seeing overall trends in large amounts of data.
Introduction xxviiChapter 8: Changing Table ContentsYou’ll learn how to add, modify, and remove rows using the INSERT, UPDATE, and DELETEstatements, and how to make the results of your transactions permanent using the COMMITstatement or undo their results entirely using the ROLLBACK statement. You’ll also learn howan Oracle database can process multiple transactions at the same time.Chapter 9: Users, Privileges, and RolesIn this chapter, you’ll learn about database users and see how privileges and roles are used toenable users to perform specific tasks in the database.Chapter 10: Creating Tables, Sequences, Indexes, and ViewsYou’ll learn about tables and sequences, which generate a series of numbers, and indexes, whichact like an index in a book and allow you quick access to rows. You’ll also learn about views,which are predefined queries on one or more tables; among other benefits, views allow you tohide complexity from a user, and implement another layer of security by only allowing a view toaccess a limited set of data in the tables. You’ll also examine flashback data archives, which arenew for Oracle Database 11g. A flashback data archive stores changes made to a table over aperiod of time.Chapter 11: Introducing PL/SQL ProgrammingIn this chapter, you’ll explore PL/SQL, which is built on top of SQL and enables you to writestored programs in the database that contain SQL statements. PL/SQL contains standardprogramming constructs.Chapter 12: Database ObjectsYou’ll learn how to create database object types, which may contain attributes and methods.You’ll use object types to define column objects and object tables, and see how to manipulateobjects using SQL and PL/SQL.Chapter 13: CollectionsIn this chapter, you’ll learn how to create collection types, which may contain multiple elements.You’ll use collection types to define columns in tables. You’ll see how to manipulate collectionsusing SQL and PL/SQL.Chapter 14: Large ObjectsYou’ll learn about large objects, which can be used to store up to 128 terabytes of character andbinary data or point to an external file. You’ll also learn about the older LONG types, which arestill supported in Oracle Database 11g for backward compatibility.Chapter 15: Running SQL Using JavaIn this chapter, you’ll learn the basics of running SQL using Java through the Java DatabaseConnectivity (JDBC) applications programming interface, which is the glue that allows a Javaprogram to access a database.
xxviii Oracle Database 11g SQL Chapter 16: SQL Tuning You’ll see SQL tuning tips that you can use to shorten the length of time your queries take to execute. You’ll also learn about the Oracle optimizer and examine how to pass hints to the optimizer. Chapter 17: XML and the Oracle Database The Extensible Markup Language (XML) is a general-purpose markup language. XML enables you to share structured data across the Internet, and can be used to encode data and other documents. In this chapter, you’ll see how to generate XML from relational data and how to save XML in the database. Appendix: Oracle Data Types This appendix shows the data types available in Oracle SQL and PL/SQL. Intended Audience This book is suitable for the following readers: ■ Developers who need to write SQL and PL/SQL. ■ Database administrators who need in-depth knowledge of SQL. ■ Business users who need to write SQL queries to get information from their organization’s database. ■ Technical managers or consultants who need an introduction to SQL and PL/SQL. No prior knowledge of the Oracle database, SQL, or PL/SQL is assumed; you can find everything you need to know to become a master in this book. Retrieving the Examples All the SQL scripts, programs, and other files used in this book can be downloaded from the Oracle Press website at www.OraclePressBooks.com. The files are contained in a Zip file. Once you’ve downloaded the Zip file, you need to extract its contents. This will create a directory named sql_book that contains the following subdirectories: ■ Java Contains the Java programs used in Chapter 15 ■ sample_files Contains the sample files used in Chapter 14 ■ SQL Contains the SQL scripts used throughout the book, including scripts to create and populate the example database tables ■ xml_files Contains the XML used in Chapter 17 I hope you enjoy this book!