sql the complete reference, third edition

911

Upload: others

Post on 11-Sep-2021

28 views

Category:

Documents


0 download

TRANSCRIPT

SQL The Complete Reference,
Third Edition Paul Weinberg
James Groff Andrew Oppel
New York Chicago San Francisco Lisbon London Madrid Mexico City
Milan New Delhi San Juan Seoul Singapore Sydney Toronto
Copyright © 2010 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher.
ISBN: 978-0-07-159256-7
MHID: 0-07-159256-3
The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-159255-0, MHID: 0-07-159255-5.
All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training pro- grams. To contact a representative please e-mail us at [email protected].
Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human or mechan- ical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or completeness of any informa- tion and is not responsible for any errors or omissions or the results obtained from the use of such information.
TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, dis- seminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own non- commercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to com- ply with these terms.
THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DIS- CLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MER- CHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the func- tions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages result- ing therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.
About the Authors James R. Groff is CEO of PBworks, whose hosted collaboration software helps teams of people work together more effectively and efficiently. Earlier, Groff was CEO of TimesTen, the leading provider of in-memory SQL databases. He led TimesTen from its early days through eight years of growth and a successful acquisition by Oracle in 2005, where he served as a senior vice president, and Oracle TimesTen became Oracle’s flagship real-time database product. Groff was the cofounder, with Paul Weinberg, of Network Innovations Corporation, a developer of SQL-based networking software, and coauthor with him of Understanding UNIX: A Conceptual Guide as well as this book. Groff has also held senior division management and marketing positions at Apple Computer and Hewlett-Packard. He holds a BS in Mathematics from the Massachusetts Institute of Technology and an MBA from Harvard University.
Paul N. Weinberg is a senior vice president at SAP, where he runs core MDM (Master Data Management) development. Prior to working at SAP, Weinberg was president of A2i, Inc., which was acquired by SAP in 2004 for its enterprisewide platform for product content management and catalog publishing. Weinberg was the cofounder, with James Groff, of Network Innovations Corporation, a pioneer in client/server database access that was acquired by Apple Computer in 1988, and coauthor with him of Understanding UNIX: A Conceptual Guide as well as this book. He has also held software development and marketing positions at Bell Laboratories, Hewlett-Packard, and Plexus Computers. In 1981, he collaborated on The Simple Solution to Rubik’s Cube, the number-one best-selling book of that year, with over 6 million copies sold. He holds a BS from the University of Michigan and an MS from Stanford University, both in Computer Science.
Andrew J. (Andy) Oppel is lead data modeler at Blue Shield of California. In addition, he has served as a part-time instructor in database technology with the University of California at Berkeley, Extension for more than 20 years. Andy has designed and implemented hundreds of databases for a wide range of applications, including heath care, banking, insurance, apparel manufacturing, telecommunications, wireless communications, and human resources. He is the author of Databases Demystified, SQL Demystified, and Databases: A Beginner’s Guide and is coauthor of SQL: A Beginner’s Guide. He holds a BA in Computer Science from Transylvania University (Lexington, KY).
About the Technical Editor Aaron Davenport has been working with SQL-based RDBMS technologies for over ten years. He is currently a principal at LCS Technologies, Inc., a Sacramento and San Francisco Bay Area database consulting firm specializing in performance tuning, application development, and database architecture. Prior to joining LCS, Aaron had tenures at Yahoo!, Gap Inc., and Blue Shield of California.
This page intentionally left blank
vii
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3 SQL in Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Part II Retrieving Data
5 SQL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6 Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
8 Summary Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Part III Updating Data
10 Database Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
11 Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
12 Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Part IV Database Structure
14 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
vii
viii S Q L : T h e C o m p l e t e R e f e r e n c e
Part V Programming with SQL
17 Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
18 Dynamic SQL* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
19 SQL APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521
20 Database Processing and Stored Procedural SQL . . . . . . . . . . . . . . . . . . . . . 617
21 SQL and Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
22 SQL and Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681
23 SQL Networking and Distributed Databases . . . . . . . . . . . . . . . . . . . . . . . . 699
24 SQL and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
25 SQL and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769
26 Specialty Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805
Part VII Appendixes
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865
ix
Contents
Part I An Overview of SQL
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 The SQL Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 The Role of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 SQL Success Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Vendor Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Portability Across Computer Systems . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Official SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Early IBM Commitment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Microsoft Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Relational Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 High-Level, English-Like Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Interactive, Ad Hoc Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Programmatic Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Multiple Views of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Complete Database Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Dynamic Data Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Enterprise Application Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Extensibility and Object Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Internet Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Java Integration (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Open Source Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Industry Infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2 A Quick Tour of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 A Simple Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Summarizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Updating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Protecting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
ix
x S Q L : T h e C o m p l e t e R e f e r e n c e
3 SQL in Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 SQL and the Evolution of Database Management . . . . . . . . . . . . . . . . . . . . . . 21 A Brief History of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
The Early Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Early Relational Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 IBM Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Commercial Acceptance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 The ANSI/ISO Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Other Early SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 ODBC and the SQL Access Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 JDBC and Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 SQL and Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
SQL and Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Centralized Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 File Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Multitier Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
The Proliferation of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 SQL on Mainframes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 SQL on Minicomputers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 SQL on UNIX-Based Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 SQL on Personal Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 SQL and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 SQL and Workgroup Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 SQL, Data Warehousing, and Business Intelligence . . . . . . . . . . . . . . 40 SQL and Internet Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
File Management Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Hierarchical Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Network Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
The Relational Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Codd’s 12 Rules for Relational Databases* . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
C o n t e n t s xi C o n t e n t s xi
Part II Retrieving Data
Table Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Numeric Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Date and Time Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Symbolic Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Missing Data (NULL Values) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
6 Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 The SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
The SELECT Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 The FROM Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Query Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Calculated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Selecting All Columns (SELECT *) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Duplicate Rows (DISTINCT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Row Selection (WHERE Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 The Comparison Test (=, <>, <, <=, >, >=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
The Range Test (BETWEEN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 The Pattern Matching Test (LIKE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 The Null Value Test (IS NULL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Compound Search Conditions (AND, OR, and NOT) . . . . . . . . . . . . 107
Sorting Query Results (ORDER BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Rules for Single-Table Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Combining Query Results (UNION)* . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Unions and Duplicate Rows* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Unions and Sorting* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Multiple UNIONs* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
xii S Q L : T h e C o m p l e t e R e f e r e n c e
7 Multitable Queries (Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 A Two-Table Query Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Simple Joins (Equi-Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Parent/Child Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 An Alternative Way to Specify Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Joins with Row Selection Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Multiple Matching Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Natural Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Queries with Three or More Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Other Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Non-Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 SQL Considerations for Multitable Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Qualified Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 All-Column Selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Self-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Table Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Multitable Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 The Structure of a Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Table Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Rules for Multitable Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . 143
Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Left and Right Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Older Outer Join Notation* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Joins and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Inner Joins in Standard SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Outer Joins in Standard SQL* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Cross Joins in Standard SQL* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Multitable Joins in Standard SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
8 Summary Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Computing a Column Total (SUM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Computing a Column Average (AVG) . . . . . . . . . . . . . . . . . . . . . . . . . 166 Finding Extreme Values (MIN and MAX) . . . . . . . . . . . . . . . . . . . . . . 166 Counting Data Values (COUNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Column Functions in the Select List . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 NULL Values and Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Duplicate Row Elimination (DISTINCT) . . . . . . . . . . . . . . . . . . . . . . . 173
Grouped Queries (GROUP BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Multiple Grouping Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Restrictions on Grouped Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 NULL Values in Grouping Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
C o n t e n t s xiii C o n t e n t s xiii
Group Search Conditions (HAVING Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Restrictions on Group Search Conditions . . . . . . . . . . . . . . . . . . . . . . . 185 NULL Values and Group Search Conditions . . . . . . . . . . . . . . . . . . . . 186 HAVING Without GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
9 Subqueries and Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
What Is a Subquery? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Subqueries in the WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Outer References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Subquery Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 The Subquery Comparison Test (=, <>, <, <=, >, >=) . . . . . . . . . . . . . 192 The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 The Existence Test (EXISTS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Quantified Tests (ANY and ALL)* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Subqueries and Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Nested Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Correlated Subqueries* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Subqueries in the HAVING Clause* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Subquery Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 Advanced Queries* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Scalar-Valued Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Row-Valued Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Table-Valued Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
SQL Queries: A Final Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Part III Updating Data
10 Database Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
The Single-Row INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 The Multirow INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Bulk Load Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Deleting Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Deleting All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 DELETE with Subquery* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Modifying Data in the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Updating All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 UPDATE with Subquery* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
xiv S Q L : T h e C o m p l e t e R e f e r e n c e
11 Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 What Is Data Integrity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Required Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Simple Validity Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Column Check Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Entity Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Other Uniqueness Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Uniqueness and NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Referential Integrity Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Delete and Update Rules* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Cascaded Deletes and Updates* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Referential Cycles* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Foreign Keys and NULL Values* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Advanced Constraint Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 SQL Constraint Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Deferred Constraint Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 What Is a Trigger? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Triggers and Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 Trigger Advantages and Disadvantages . . . . . . . . . . . . . . . . . . . . . . . . 277 Triggers and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
12 Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 What Is a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 The ANSI/ISO SQL Transaction Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
The START TRANSACTION and SET TRANSACTION Statements . . . 284 The SAVEPOINT and RELEASE SAVEPOINT Statements . . . . . . . . 286 The COMMIT and ROLLBACK Statements . . . . . . . . . . . . . . . . . . . . . 286
Transactions: Behind the Scenes* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Transactions and Multiuser Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
The Lost Update Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 The Uncommitted Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 The Inconsistent Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 The Phantom Insert Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Concurrent Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Locking* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Locking Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 Shared and Exclusive Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Deadlocks* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 Advanced Locking Techniques* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
C o n t e n t s xv C o n t e n t s xv
Versioning* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Versioning in Operation* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Versioning Advantages and Disadvantages* . . . . . . . . . . . . . . . . . . . . 311
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Part IV Database Structure
13 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 The Data Definition Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Table Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Creating a Table (CREATE TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318 Removing a Table (DROP TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 Changing a Table Definition (ALTER TABLE) . . . . . . . . . . . . . . . . . . . 328
Constraint Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Aliases and Synonyms (CREATE/DROP ALIAS) . . . . . . . . . . . . . . . . . . . . . . 333 Indexes (CREATE/DROP INDEX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Managing Other Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Single-Database Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Multidatabase Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Multilocation Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 Databases on Multiple Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Database Structure and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . 348 Catalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
14 View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 What Is a View? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 How the DBMS Handles Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Advantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Disadvantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Creating a View (CREATE VIEW) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Horizontal Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Vertical Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Row/Column Subset Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Grouped Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Joined Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Updating a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366 View Updates and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . 367 View Updates in Commercial SQL Products . . . . . . . . . . . . . . . . . . . . 368 Checking View Updates (CHECK OPTION) . . . . . . . . . . . . . . . . . . . . 368
xvi S Q L : T h e C o m p l e t e R e f e r e n c e
Dropping a View (DROP VIEW) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Materialized Views* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
15 SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 SQL Security Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
User-Ids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Security Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Views and SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384 Granting Privileges (GRANT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Column Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 Passing Privileges (GRANT OPTION) . . . . . . . . . . . . . . . . . . . . . . . . . 389
Revoking Privileges (REVOKE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 REVOKE and the GRANT OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 REVOKE and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . . . . 394
Role-Based Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
16 The System Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 What Is the System Catalog? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
The Catalog and Query Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 The Catalog and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . 401 Catalog Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Table Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Column Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 View Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412 Relationship Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413 User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Privileges Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 The SQL Information Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 Other Catalog Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Part V Programming with SQL
17 Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 Programmatic SQL Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
DBMS Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431 Embedded SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 Developing an Embedded SQL Program . . . . . . . . . . . . . . . . . . . . . . . 434 Running an Embedded SQL Program . . . . . . . . . . . . . . . . . . . . . . . . . . 437
C o n t e n t s xvii C o n t e n t s xvii
Simple Embedded SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439 Declaring Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 Using Host Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Data Retrieval in Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 Single-Row Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 Multirow Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Cursor-Based Deletes and Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 Cursors and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
18 Dynamic SQL* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Limitations of Static SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 Dynamic SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479 Dynamic Statement Execution (EXECUTE IMMEDIATE) . . . . . . . . . . . . . . . 480 Two-Step Dynamic Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
The PREPARE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485 The EXECUTE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 The DESCRIBE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495 The DECLARE CURSOR Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 The Dynamic OPEN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500 The Dynamic FETCH Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503 The Dynamic CLOSE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Dynamic SQL Dialects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 Dynamic SQL in Oracle* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504
Dynamic SQL and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 Basic Dynamic SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508 The Standard SQLDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 The SQL Standard and Dynamic SQL Queries . . . . . . . . . . . . . . . . . . 515
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
19 SQL APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 API Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522 The dblib API (SQL Server) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Basic SQL Server Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 SQL Server Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532 Positioned Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539 Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540
ODBC and the SQL/CLI Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549 The Call-Level Interface Standardization . . . . . . . . . . . . . . . . . . . . . . . 549 CLI Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552 CLI Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 CLI Errors and Diagnostic Information . . . . . . . . . . . . . . . . . . . . . . . . 575 CLI Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577 CLI Information Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577
xviii S Q L : T h e C o m p l e t e R e f e r e n c e
The ODBC API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 The Structure of ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580 ODBC and DBMS Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 ODBC Catalog Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 Extended ODBC Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582
The Oracle Call Interface (OCI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 OCI Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586 Oracle Server Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588 Statement Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 Query Results Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590 Descriptor Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590 Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590
Java Database Connectivity (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592 JDBC History and Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592 JDBC Implementations and Driver Types . . . . . . . . . . . . . . . . . . . . . . 593 The JDBC API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614
Part VI SQL Today and Tomorrow
20 Database Processing and Stored Procedural SQL . . . . . . . . . . . . . . . . . . . . . 617 Procedural SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618 A Basic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620 Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621
Creating a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622 Calling a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624 Stored Procedure Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625 Statement Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630 Returning Values via Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631 Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634 Repeated Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636 Other Flow-of-Control Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 Cursor-Based Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639 Handling Error Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Advantages of Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 Stored Procedure Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646 System-Defined Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 External Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648
Advantages and Disadvantages of Triggers . . . . . . . . . . . . . . . . . . . . . 649 Triggers in Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649 Triggers in Informix SPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651 Triggers in Oracle PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 Other Trigger Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
C o n t e n t s xix C o n t e n t s xix
Stored Procedures, Functions, Triggers, and the SQL Standard . . . . . . . . . . 655 The SQL/PSM Stored Procedures Standard . . . . . . . . . . . . . . . . . . . . 656 The SQL/PSM Triggers Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
21 SQL and Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667 Data Warehousing Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Components of a Data Warehouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 The Evolution of Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . 670
Database Architecture for Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671 Fact Cubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672 Star Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673 Multilevel Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675 SQL Extensions for Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . 676
Warehouse Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 Load Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
22 SQL and Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 SQL and Web Sites: Early Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 Application Servers and Three-Tier Web Site Architectures . . . . . . . . . . . . . 682 Database Access from Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . 684
EJB Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685 Session Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686 Entity Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 EJB 2.0 Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 EJB 3.0 Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693 Open Source Application Development . . . . . . . . . . . . . . . . . . . . . . . . 695
Application Server Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698
23 SQL Networking and Distributed Databases . . . . . . . . . . . . . . . . . . . . . . . . 699 The Challenge of Distributed Data Management . . . . . . . . . . . . . . . . . . . . . . 700 Distributing Data: Practical Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Remote Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705 Remote Data Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708 Table Extracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709 Table Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711 Updateable Replicas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713 Replication Trade-Offs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 Typical Replication Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Distributed Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719 Remote Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720 Remote Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721 Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722 Distributed Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722
xx S Q L : T h e C o m p l e t e R e f e r e n c e
The Two-Phase Commit Protocol* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 Network Applications and Database Architecture . . . . . . . . . . . . . . . . . . . . . 727
Client/Server Applications and Database Architecture . . . . . . . . . . . 728 Client/Server Applications with Stored Procedures . . . . . . . . . . . . . 729 Enterprise Applications and Data Caching . . . . . . . . . . . . . . . . . . . . . 730 High-Volume Internet Data Management . . . . . . . . . . . . . . . . . . . . . . 731
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
Object-Oriented Database Characteristics . . . . . . . . . . . . . . . . . . . . . . 736 Pros and Cons of Object-Oriented Databases . . . . . . . . . . . . . . . . . . . 737 Objects and the Database Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Object-Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739 Large Object Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740 LOBs in the Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740 Specialized LOB Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Abstract (Structured) Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744 Defining Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746 Manipulating Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749 Table Inheritance: Implementing Object Classes . . . . . . . . . . . . . . . . . 751
Sets, Arrays, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754 Defining Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755 Querying Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758 Manipulating Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759 Collections and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
User-Defined Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762 Methods and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763 Object Support in the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767
25 SQL and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 What Is XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769 XML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771 XML for Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
XML and SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774 Elements vs. Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Using XML with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777 XML Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778 XML Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782 XML Data Exchange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784 XML Storage and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784
C o n t e n t s xxi C o n t e n t s xxi
XML and Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788 Document Type Definitions (DTDs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 790 XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791
XML and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797 XQuery Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798 Query Processing in XQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800
XML Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803
26 Specialty Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805 Very Low Latency and In-Memory Databases . . . . . . . . . . . . . . . . . . . . . . . . . 805
Anatomy of an In-Memory Database . . . . . . . . . . . . . . . . . . . . . . . . . . 806 In-Memory Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808
Caching with In-Memory Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . 808 Complex Event-Processing and Stream Databases . . . . . . . . . . . . . . . . . . . . . 810
Continuous Queries in Stream Databases . . . . . . . . . . . . . . . . . . . . . . 811 Stream Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812 Stream Database Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
Embedded Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814 Embedded Database Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . 815 Embedded Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . 815
Mobile Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816 Mobile Database Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816 Mobile Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
27 The Future of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819 Database Market Trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Enterprise Database Market Maturity . . . . . . . . . . . . . . . . . . . . . . . . . . 820 Market Diversity and Segmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 821 Packaged Enterprise Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822 Software-as-a-Service (SaaS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823 Hardware Performance Gains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823 Database Server Appliances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824 SQL Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825
SQL in the Next Decade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 Distributed Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 Massive Data Warehousing for Business Optimization . . . . . . . . . . . 826 Ultrahigh-Performance Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827 Internet and Network Services Integration . . . . . . . . . . . . . . . . . . . . . 828 Embedded Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829 Object Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829 Cloud-Based and Horizontally Scalable Databases . . . . . . . . . . . . . . 830
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
xxii S Q L : T h e C o m p l e t e R e f e r e n c e
Part VII Appendixes
C SQL Syntax Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857 Data Definition Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858 Access Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859 Basic Data Manipulation Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859 Transaction-Processing Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860 Cursor-Based Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860 Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860 Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 Statement Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863 Simple Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865
I n t r o d u c t i o n xxiii
xxiii
Acknowledgments
Special thanks to Andy Oppel, our new coauthor for this third edition of SQL: The Complete Reference. His impressive high-level mastery of the subject matter coupled with his meticulous attention to detail made this a better book, and we are fortunate
to have had his involvement.
—Jim and Paul
It’s an honor to join such an accomplished team of authors for this edition of SQL: The Complete Reference. My thanks for the excellent support of the entire McGraw-Hill team for their tireless support in this effort. In particular I wish to thank technical editor Aaron Davenport and copy editor Jan Jue for their persistence and attention to detail, which contributed so much to the overall quality of this book.
—Andy
xxiii
xxv
Introduction
SQL: The Complete Reference, Third Edition provides a comprehensive, in-depth treatment of the SQL language for both technical and nontechnical users, programmers, data processing professionals, and managers who want to understand
the impact of SQL in today’s computer industry. This book offers a conceptual framework for understanding and using SQL, describes the history of SQL and SQL standards, and explains the role of SQL in various computer industry segments, from enterprise data processing to data warehousing to web site architectures. This new edition contains new chapters specially focused on the role of SQL in application server architectures, and the integration of SQL with XML and other object-based technologies.
This book will show you, step-by-step, how to use SQL features, with many illustrations and realistic examples to clarify SQL concepts. The book also compares SQL products from leading DBMS vendors—describing their advantages, benefits, and trade-offs—to help you select the right product for your application. Most of the examples in this book are based on the sample database described in Appendix A. The sample database contains data that supports a simple order-processing application for a small distribution company. Appendix A also contains instructions for downloading the SQL statements required to create and populate the sample database tables in a DBMS of you choice, such as Oracle, SQL Server, MySQL, and DB2. This allows you to try the examples in the book yourself and gain actual experience writing and running SQL statements.
In some of the chapters, the subject matter is explored at two different levels—a fundamental description of the topic, and an advanced discussion intended for computer professionals who need to understand some of the internals behind SQL. The more advanced information is covered in sections marked with an asterisk (*). You do not need to read these sections to obtain an understanding of what SQL is and what it does.
xxv
xxvi S Q L : T h e C o m p l e t e R e f e r e n c e
How This Book Is Organized The book is divided into six parts that cover various aspects of the SQL language:
• Part I, “An Overview of SQL,” provides an introduction to SQL and a market perspective of its role as a database language. Its four chapters describe the history of SQL, the evolution of SQL standards, and how SQL relates to the relational data model and to earlier database technologies. Part I also contains a quick tour of SQL that briefly illustrates its most important features and provides you with an overview of the entire language early in the book.
• Part II, “Retrieving Data,” describes the features of SQL that allow you to perform database queries. The first chapter in this part describes the basic structure of the SQL language. The next four chapters start with the simplest SQL queries and progressively build to more complex queries, including multitable queries, summary queries, and queries that use subqueries.
• Part III, “Updating Data,” shows how you can use SQL to add new data to a database, delete data from a database, and modify existing database data. It also describes the database integrity issues that arise when data is updated, and how SQL addresses these issues. The last of the three chapters in this part discusses the SQL transaction concept and SQL support for multiuser transaction processing.
• Part IV, “Database Structure,” deals with creating and administering a SQL-based database. Its four chapters tell you how to create the tables, views, and indexes that form the structure of a relational database. It also describes the SQL security scheme that prevents unauthorized access to data, and the SQL system catalog that describes the structure of a database. This part also discusses the significant differences between the database structures supported by various SQL-based DBMS products.
• Part V, “Programming with SQL,” describes how application programs use SQL for database access. It discusses the embedded SQL specified by the ANSI standard and used by IBM, Oracle, Ingres, Informix, and many other SQL-based DBMS products. It also describes the dynamic SQL interface that is used to build general-purpose database tables, such as report writers and database browsing programs. Finally, this part describes the popular SQL APIs, including ODBC, the ISO-standard Call- Level Interface, and JDBC, the standard call-level interface for Java, as well as proprietary call-level interfaces such as Oracle’s OCI API.
• Part VI, “SQL Today and Tomorrow,” examines the use of SQL in several of today’s “hottest” application areas, and the current state of SQL-based DBMS products. Two chapters describe the use of SQL stored procedures and triggers for online transaction processing, and the contrasting use of SQL for data warehousing. Four additional chapters describe SQL-based distributed databases, the influence of object technologies on SQL, specialty databases, and the integration of SQL with XML technologies. Finally, the last chapter explores the future of SQL and some of the most important trends in SQL-based data management.
I n t r o d u c t i o n xxvii I n t r o d u c t i o n xxvii
Conventions Used in This Book SQL: The Complete Reference, Third Edition describes the SQL features and functions available in the most popular SQL-based DBMS products and those described in the ANSI/ISO SQL standards. Whenever possible, the SQL statement syntax described in this book and used in the examples applies to all dialects of SQL. When the dialects differ, the differences are pointed out in the text, and the examples follow the most common practice. In these cases, you may have to modify the SQL statements in the examples slightly to suit your particular brand of DBMS.
Throughout the book, technical terms appear in italics the first time they are used and defined. SQL language elements, including SQL keywords, table and column names, and sample SQL statements, appear in an UPPERCASE MONOSPACE font. SQL API function names appear in a lowercase monospace font. Program listings also appear in monospace font and use the normal case conventions for the particular programming language (uppercase for COBOL and FORTRAN, lowercase for C and Java). Note that these conventions are used solely to improve readability; most SQL implementations will accept either uppercase or lowercase statements. Many of the SQL examples include query results, which appear immediately following the SQL statement, as they would in an interactive SQL session. In some cases, long query results are truncated after a few rows; this is indicated by a vertical ellipsis (…) following the last row of query results.
Why This Book Is for You SQL: The Complete Reference, Third Edition is the right book for anyone who wants to understand and learn SQL, including database users, data processing professionals and architects, programmers, students, and managers. It describes—in simple, understandable language liberally illustrated with figures and examples—what SQL is, why it is important, and how you use it. This book is not specific to one particular brand or dialect of SQL. Rather, it describes the standard, central core of the SQL language and then goes on to describe the differences among the most popular SQL products, including Oracle, Microsoft SQL Server, IBM’s DB2 Universal Database and Informix, Sybase, and MySQL. It also explains the importance of SQL-based standards, such as ODBC and JDBC, and the ANSI/ ISO standards for SQL and SQL-related technologies. This third edition contains new chapters and sections that cover the latest SQL innovations, in the areas of object-relational technologies, XML, and application server architectures.
If you are new to SQL, this book offers comprehensive, step-by-step treatment of the language, building from simple queries to more advanced concepts. The structure of the book will allow you to quickly start using SQL, but the book will continue to be valuable as you begin to use the more complex features of the language. You can create the sample database using an SQL script available on the McGraw-Hill website (see Appendix A) and use it to try out the examples and build your SQL skills.
If you are a data processing professional, architect, or manager, this book will give you a perspective on the impact that SQL is having across the information technology industry—from personal computers to mainframes to data warehousing to Internet web sites and Internet-based distributed applications. The early chapters describe the history of SQL, its role in the market, and its evolution from earlier database technologies. Later chapters describe the future of SQL and the development of new database technologies, such as distributed databases, object-oriented extensions to SQL, business intelligence databases, and database/XML integration.
xxviii S Q L : T h e C o m p l e t e R e f e r e n c e
If you are a programmer, this book offers a very complete treatment of programming with SQL. Unlike the reference manuals of many DBMS products, it offers a conceptual framework for SQL programming, explaining the why as well as the how of developing a SQL-based application. It contrasts the SQL programming interfaces offered by all of the leading SQL products, including embedded SQL, dynamic SQL, ODBC, JDBC, and proprietary APIs such as the Oracle Call Interface. The description and comparison of programming techniques provides a perspective not found in any other book.
If you are selecting a DBMS product, this book offers a comparison of the SQL features, advantages, and benefits offered by the various DBMS vendors. The differences between the leading DBMS products are explained, not only in technical terms, but also in terms of their impact on applications and their evolving competitive position in the marketplace. The “sample database” can be used to try these features in a prototype of your own application.
In short, both technical and nontechnical users can benefit from this book. It is the most comprehensive source of information available about the SQL language, SQL features and benefits, popular SQL-based products, the history of SQL, and the impact of SQL on the future direction of the information technology industry.
I An Overview of SQL
The first four chapters of this book provide a perspective and a quick introduction to SQL. Chapter 1 describes what SQL is and explains its major features and benefits. In Chapter 2,
a quick tour of SQL shows you many of its capabilities with simple, rapid-fire examples. Chapter 3 offers a market perspective of SQL by tracing its history, describing the SQL standards and the major vendors of SQL-based products, and by identifying the reasons for SQL’s prominence today. Chapter 4 describes the relational data model upon which SQL is based and compares it with earlier data models.
CHAPTER 1 Introduction
CHAPTER 3 SQL in Perspective
CHAPTER 4 Relational Databases
1 Introduction
The SQL language and relational database systems based on it constitute one of the most important foundation technologies in the computer industry. Over the last three decades, SQL has grown from its first commercial use into a computer product and
services market segment worth tens of billions of dollars per year, and SQL stands today as the standard computer database language. Hundreds of database products now support SQL, running on computer systems from mainframes to personal computers. A SQL-based database may even be embedded in your mobile phone or PDA, or in the entertainment system of your car. An official international SQL standard has been adopted and expanded several times. Every major enterprise software product relies on SQL for its data management, and SQL is at the core of the flagship database products from Microsoft, Oracle, and IBM, three of the largest software companies in the world. SQL is also at the heart of open-source database products such as MySQL and Postgres that are helping to fuel the popularity of Linux and the open source movement. From its obscure beginnings as an IBM research project, SQL has grown to become both an important piece of information technology and a powerful market force.
What, exactly, is SQL? Why is it important? What can it do, and how does it work? If SQL is really a standard, why do we have so many different versions and dialects? How do popular SQL products like SQL Server, Oracle, MySQL, Sybase, and DB2 compare? How does SQL relate to Microsoft standards such as ODBC and .NET? How does JDBC link SQL to the world of Java and object technology? What role does it play in the Service-Oriented Architecture (SOA) and web services being embraced by enterprise IT organizations? Does SQL really scale from mainframes to handheld devices? Has it really delivered the performance needed for high-volume transaction processing? How will SQL impact the way you use computers, and how can you get the most out of this important data management tool? This book answers those questions by giving you a complete perspective and a solid working knowledge of SQL.
33
CHAPTER
4 P a r t I : A n O v e r v i e w o f S Q L 4 P a r t I : A n O v e r v i e w o f S Q L
The SQL Language SQL is a tool for organizing, managing, and retrieving data stored by a computer database. The original name given it by IBM was Structured English Query Language, shortened to the acronym SEQUEL. When IBM discovered that SEQUEL was a trademark owned by the Hawker Siddeley Aircraft Company of the United Kingdom, they shortened the acronym to SQL. The word “English” was then dropped from the spelled-out name to match the new acronym. To this day, you will hear the acronym SQL pronounced as either a word (“sequel”) or as a string of letters (“S-Q-L”), and while the latter is generally preferred, both are considered correct. As the name implies, SQL is a computer language that you use to interact with a database. In fact, SQL works with one specific type of database, called a relational database, which has become the mainstream way to organize data across a very broad range of computer applications.
Figure 1-1 shows how SQL works. The computer system in the figure has a database that stores important information. If the computer system is in a business, the database might store inventory, production, sales, or payroll data. On a personal computer, the database might store data about the checks you have written, lists of people and their phone numbers, or data extracted from a larger computer system. The computer program that controls the database is called a database management system (DBMS).
When you need to retrieve data from a database, you use the SQL to make the request. The DBMS processes the SQL request, retrieves the requested data, and returns it to you. This process of requesting data from a database and receiving the results is called a database query—hence the name Structured Query Language.
“Structured Query Language” is actually somewhat of a misnomer. First of all, SQL is far more than a query tool, although that was its original purpose, and retrieving data is still one of its most important functions. SQL is used to control all of the functions that a DBMS provides for its users, including
FIGURE 1-1 Using SQL for database access
C h a p t e r 1 : I n t r o d u c t i o n 5 C h a p t e r 1 : I n t r o d u c t i o n 5 PART I
• Data definition SQL lets a user define the structure and organization of the stored data and relationships among the stored data items.
• Data retrieval SQL allows a user or an application program to retrieve stored data from the database and use it.
• Data manipulation SQL allows a user or an application program to update the database by adding new data, removing old data, and modifying previously stored data.
• Access control SQL can be used to restrict a user’s ability to retrieve, add, and modify data, protecting stored data against unauthorized access.
• Data sharing SQL is used to coordinate data sharing by concurrent users, ensuring that changes made by one user do not inadvertently wipe out changes made at nearly the same time by another user.
• Data integrity SQL defines integrity constraints in the database, protecting it from corruption due to inconsistent updates or system failures.
SQL is thus a comprehensive language for controlling and interacting with a database management system.
Second, SQL is not really a complete computer language like COBOL, C, C++, or Java. Instead, SQL is a database sublanguage, consisting of about 40 statements specialized for database management tasks. These SQL statements can be embedded into another language such as COBOL or C to extend that language for use in database access. Alternatively, the statements can be explicitly sent to a database management system for processing, via a call-level interface from a language such as C, C++, or Java, or via messages sent over a computer network.
SQL also differs from other computer languages because it describes what the user wants the computer to do rather than how the computer should do it. (In more technical terms, SQL is a declarative or descriptive language rather than a procedural one.) SQL contains no IF statement for testing conditions, and no GOTO, DO, or FOR statements for program flow control. Rather, SQL statements describe how a collection of data is to be organized, or what data is to be retrieved or added to the database. The sequence of steps to do those tasks is left for the DBMS to determine.
Finally, SQL is not a particularly structured language, especially when compared with highly structured languages such as C, Pascal, or Java. Instead, SQL statements resemble English sentences, complete with “noise words” that don’t add to the meaning of the statement but make it read more naturally. The SQL has quite a few inconsistencies and also some special rules to prevent you from constructing SQL statements that look perfectly legal but that don’t make sense.
Despite the inaccuracy of its name, SQL has emerged as the standard language for using relational databases. SQL is both a powerful language and one that is relatively easy to learn. The quick tour of SQL in Chapter 2 will give you a good overview of the language and its capabilities.
6 P a r t I : A n O v e r v i e w o f S Q L 6 P a r t I : A n O v e r v i e w o f S Q L
The Role of SQL SQL is not itself a database management system, nor is it a stand-alone product. You cannot go to a computer retailer or a web site selling computer software and buy SQL. Instead, SQL is an integral part of a database management system, a language and a tool for communicating with the DBMS. Figure 1-2 shows some of the components of a typical DBMS and how SQL links them together.
The database engine is the heart of the DBMS, responsible for actually structuring, storing, and retrieving the data in the database. It accepts SQL requests from other DBMS components (such as a forms facility, report writer, or interactive query facility), from user-written application programs, and even from other computer systems. As the figure shows, SQL plays many different roles:
• SQL is an interactive query language. Users type SQL commands into an interactive SQL program to retrieve data and display it on the screen, providing a convenient, easy-to-use tool for ad hoc database queries.
• SQL is a database programming language. Programmers embed SQL commands into their application programs to access the data in a database. Both user-written programs and database utility programs (such as report writers and data entry tools) use this technique for database access.
FIGURE 1-2 Components of a typical database management system
C h a p t e r 1 : I n t r o d u c t i o n 7 C h a p t e r 1 : I n t r o d u c t i o n 7 PART I
• SQL