Transcript
  • 1. SQL:The Complete Reference,Second Edition

2. About the AuthorsJames R. Groff is president and CEO of TimesTen,a software company that delivers real-time eventprocessing software to telecom, financial services,and e-business customers. The core of the TimesTensoftware is a high-performance, memory-optimized,SQL-based DBMS. Prior to joining TimesTen, Groffwas the cofounder, with Paul Weinberg, of NetworkInnovations Corporation, a developer of SQL-basednetworking software, and coauthor of UnderstandingUNIX: A Conceptual Guide and SQL: The CompleteReference. He has also held senior division managementand marketing positions at Apple Computer andHewlett-Packard. Groff holds a B.S. in mathematicsfrom the Massachusetts Institute of Technology andan M.B.A. from Harvard University.Paul N. Weinberg is president and CEO of A2i,Inc., a software company that develops and marketsa system for enterprise-wide product contentmanagement and single-source, cross-media catalogpublishing. Before that, he was the cofounder, withJim Groff, of Network Innovations Corporation, anearly pioneer in client/server database access thatwas acquired by Apple Computer, Inc., in 1988, andthe coauthor of Understanding UNIX: A ConceptualGuide and SQL: The Complete Reference. He has alsoheld software development and marketing positionsat Bell Laboratories, Hewlett-Packard, and PlexusComputers. In 1981, he collaborated on The SimpleSolution to Rubiks Cube, the number-one best-sellingbook of that year with over six million copies sold.He holds a B.S. from the University of Michiganand an M.S. from Stanford University, both inComputer Science.About the Technical EditorGreg Guntle has been doing technical editing ofcomputer books for 20 years. He has written severalprogramming books as well. 3. SQL:The Complete Reference,Second EditionJames R. Groff and Paul N. WeinbergMcGraw-Hill/OsborneNew York Chicago San FranciscoLisbon London Madrid Mexico CityMilan New Delhi San JuanSeoul Singapore Sydney Toronto 4. Copyright 2002 by The McGraw-HIll Companies, Inc. All rights reserved. Manufactured in theUnited States of America. Except as permitted under the United States Copyright Act of 1976, no partof this publication may be reproduced or distributed in any form or by any means, or stored in a data-baseor retrieval system, without the prior written permission of the publisher.0-07-222817-2The material in this eBook also appears in the print version of this title: 0-07-222559-9All trademarks are trademarks of their respective owners. Rather than put a trademark symbol afterevery occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefitof the trademark owner, with no intention of infringement of the trademark. Where such designationsappear 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 pro-motions,or for use in corporate training programs. For more information, please contact GeorgeHoare, Special Sales, at [email protected] or (212) 904-4069.TERMS OF USEThis is a copyrighted work and The McGraw-Hill Companies, Inc. (McGraw-Hill) and its licensorsreserve all rights in and to the work. Use of this work is subject to these terms. Except as permittedunder the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may notdecompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon,transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it withoutMcGraw-Hills prior consent. You may use the work for your own noncommercial and personal use;any other use of the work is strictly prohibited. Your right to use the work may be terminated if youfail to comply with these terms.THE WORK IS PROVIDED AS IS. McGRAW-HILL AND ITS LICENSORS MAKE NO GUAR-ANTEESOR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OFOR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMA-TIONTHAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUTNOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR APARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the func-tionscontained in the work will meet your requirements or that its operation will be uninterrupted orerror free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inac-curacy,error or omission, regardless of cause, in the work or for any damages resulting 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 thework, even if any of them has been advised of the possibility of such damages. This limitation of lia-bilityshall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tortor otherwise.DOI: 10.1036/0072228172 5. For more information about this title, click here.ContentsAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiPart IAn Overview of SQL1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The SQL Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The Role of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6SQL Features and Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Vendor Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Portability Across Computer Systems . . . . . . . . . . . . . . . . . . . . . . . 9SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9IBM Endorsement and Commitment (DB2) . . . . . . . . . . . . . . . . . . 9Microsoft Commitment (SQL Server, ODBC, and ADO) . . . . . . . 9Relational Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10High-Level, English-Like Structure . . . . . . . . . . . . . . . . . . . . . . . . . 10Interactive, Ad Hoc Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Programmatic Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Multiple Views of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Complete Database Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Dynamic Data Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Enterprise Application Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11vCopyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use. 6. vi S Q L : T h e C o m p l e t e R e f e r e n c eExtensibility and Object Technology . . . . . . . . . . . . . . . . . . . . . . . . 12Internet Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Java Integration (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Industry Infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 A Quick Tour of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15A Simple Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Summarizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Updating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Protecting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 SQL in Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25SQL and Database Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26A Brief History of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27The Early Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Early Relational Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29IBM Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Commercial Acceptance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32The ANSI/ISO Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Other SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34ODBC and the SQL Access Group . . . . . . . . . . . . . . . . . . . . . . . . . . 34SQL and Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35SQL and Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Centralized Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37File Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39Multitier Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40The Proliferation of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42SQL and IBMs Unified Database Strategy . . . . . . . . . . . . . . . . . . . 42SQL on Minicomputers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42SQL on UNIX-Based Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43SQL on Personal Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43SQL and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 45SQL and Workgroup Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45SQL and Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46SQL and Distributed Internet Applications . . . . . . . . . . . . . . . . . . 48Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Early Data Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52File Management Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Hierarchical Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Network Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 7. C o n t e n t s viiThe Relational Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Codds 12 Rules * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67Part IIRetrieving Data5 SQL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Table Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84Numeric Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Date and Time Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86Symbolic Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Missing Data (NULL Values) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936 Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95The SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96The SELECT Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99The FROM Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Query Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Calculated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Selecting All Columns (SELECT *) . . . . . . . . . . . . . . . . . . . . . . . . . . 106Duplicate Rows (DISTINCT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Row Selection (WHERE Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110The Comparison Test (=, , =) . . . . . . . . . . . . . . . . . . . 111The Range Test (BETWEEN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118The Pattern Matching Test (LIKE) . . . . . . . . . . . . . . . . . . . . . . . . . . 120The Null Value Test (IS NULL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Compound Search Conditions (AND, OR, and NOT) . . . . . . . . . . . . 124Sorting Query Results (ORDER BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Rules for Single-Table Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Combining Query Results (UNION) * . . . . . . . . . . . . . . . . . . . . . . . . 131Unions and Duplicate Rows * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 8. viii S Q L : T h e C o m p l e t e R e f e r e n c eUnions and Sorting * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Multiple UNIONs* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1387 Multitable Queries (Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139A Two-Table Query Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140Simple Joins (Equi-Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Parent/Child Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Joins with Row Selection Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . 146Multiple Matching Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147Queries with Three or More Tables . . . . . . . . . . . . . . . . . . . . . . . . . 148Other Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152Non-Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153SQL Considerations for Multitable Queries . . . . . . . . . . . . . . . . . . . . . . . . . . 154Qualified Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154All-Column Selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Self-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157Table Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160Multitable Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162The Structure of a Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Table Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Rules for Multitable Query Processing . . . . . . . . . . . . . . . . . . . . . . 164Outer Joins * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165Left and Right Outer Joins * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Outer Join Notation * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173Joins and the SQL2 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Inner Joins in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175Outer Joins in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178Cross Joins and Union Joins in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . 180Multitable Joins in SQL2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1858 Summary Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188Computing a Column Total (SUM) . . . . . . . . . . . . . . . . . . . . . . . . . . 190Computing a Column Average (AVG) . . . . . . . . . . . . . . . . . . . . . . . 191Finding Extreme Values (MIN and MAX) . . . . . . . . . . . . . . . . . . . . . 191Counting Data Values (COUNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193Column Functions in the Select List . . . . . . . . . . . . . . . . . . . . . . . . . 195NULL Values and Column Functions . . . . . . . . . . . . . . . . . . . . . . . . 197Duplicate Row Elimination (DISTINCT) . . . . . . . . . . . . . . . . . . . . . 199Grouped Queries (GROUP BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Multiple Grouping Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Restrictions on Grouped Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 207NULL Values in Grouping Columns . . . . . . . . . . . . . . . . . . . . . . . . . 209Group Search Conditions (HAVING Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . 211Restrictions on Group Search Conditions . . . . . . . . . . . . . . . . . . . . 214NULL Values and Group Search Conditions . . . . . . . . . . . . . . . . . . 215HAVING Without GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 9. C o n t e n t s ix9 Subqueries and Query Expressions . . . . . . . . . . . . . . . . . . . . . 217Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218What Is a Subquery? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Subqueries in the WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220Outer References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Subquery Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223The Subquery Comparison Test (=, , =) . . . . . . . . . . 223The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226The Existence Test (EXISTS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Quantified Tests (ANY and ALL) * . . . . . . . . . . . . . . . . . . . . . . . . . . . 230Subqueries and Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236Nested Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237Correlated Subqueries * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239Subqueries in the HAVING Clause * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241Subquery Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244Advanced Queries in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244Scalar-Valued Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . 247Row-Valued Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Table-Valued Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . 257Query Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261SQL Queries: A Final Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265Part IIIUpdating Data10 Database Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270The Single-Row INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . 271The Multirow INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Bulk Load Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278Deleting Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279Deleting All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281DELETE with Subquery * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282Modifying Data in the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285Updating All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287UPDATE with Subquery * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28911 Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291What Is Data Integrity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292Required Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293Simple Validity Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294Column Check Constraints (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . 295Domains (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296Entity Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Other Uniqueness Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Uniqueness and NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 10. x S Q L : T h e C o m p l e t e R e f e r e n c eReferential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299Referential Integrity Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Delete and Update Rules * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302Cascaded Deletes and Updates * . . . . . . . . . . . . . . . . . . . . . . . . . . . 305Referential Cycles * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308Foreign Keys and NULL Values * . . . . . . . . . . . . . . . . . . . . . . . . . . . 312Advanced Constraint Capabilities (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . 313Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314SQL2 Constraint Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315Deferred Constraint Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319What Is a Trigger? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320Triggers and Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . 322Trigger Advantages and Disadvantages . . . . . . . . . . . . . . . . . . . . . 323Triggers and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32512 Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327What Is a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328COMMIT and ROLLBACK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329The ANSI/ISO Transaction Model . . . . . . . . . . . . . . . . . . . . . . . . . . 332Other Transaction Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334Transactions: Behind the Scenes * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336Transactions and Multiuser Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338The Lost Update Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338The Uncommitted Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 339The Inconsistent Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341The Phantom Insert Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342Concurrent Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342Locking * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345Locking Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345Shared and Exclusive Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348Deadlocks * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348Advanced Locking Techniques * . . . . . . . . . . . . . . . . . . . . . . . . . . . 351Versioning * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Versioning in Operation * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Versioning Advantages and Disadvantages * . . . . . . . . . . . . . . . . 360Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Part IVDatabase Structure13 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365The Data Definition Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367Table Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369Creating a Table (CREATE TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . 369Removing a Table (DROP TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . . 379Changing a Table Definition (ALTER TABLE) . . . . . . . . . . . . . . . . 380 11. C o n t e n t s xiConstraint Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385Aliases and Synonyms (CREATE/DROP ALIAS) . . . . . . . . . . . . . . . . . . . . . . 386Indexes (CREATE/DROP INDEX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387Managing Other Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395Single-Database Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396Multidatabase Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397Multilocation Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399Databases on Multiple Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401Database Structure and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . 401SQL2 Catalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404SQL2 Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40814 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409What Is a View? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410How the DBMS Handles Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412Advantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412Disadvantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Creating a View (CREATE VIEW) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Horizontal Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414Vertical Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416Row/Column Subset Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418Grouped Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418Joined Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421Updating a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423View Updates and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . 424View Updates in Commercial SQL Products . . . . . . . . . . . . . . . . . 425Checking View Updates (CHECK OPTION) . . . . . . . . . . . . . . . . . . . 425Dropping a View (DROP VIEW) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428Materialized Views * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43215 SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433SQL Security Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434User-Ids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436Security Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440Views and SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443Granting Privileges (GRANT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446Column Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448Passing Privileges (GRANT OPTION) . . . . . . . . . . . . . . . . . . . . . . . . 449Revoking Privileges (REVOKE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451REVOKE and the GRANT OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . 454REVOKE and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . . 456Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 12. xii S Q L : T h e C o m p l e t e R e f e r e n c e16 The System Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459What Is the System Catalog? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460The Catalog and Query Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461The Catalog and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . 461Catalog Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462Table Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464Column Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468View Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474Relationship Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478Privileges Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480The SQL2 Information Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481Other Catalog Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487Part VProgramming with SQL17 Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491Programmatic SQL Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492DBMS Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494Embedded SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496Developing an Embedded SQL Program . . . . . . . . . . . . . . . . . . . . 496Running an Embedded SQL Program . . . . . . . . . . . . . . . . . . . . . . . 500Simple Embedded SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502Declaring Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506Using Host Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515Data Retrieval in Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524Single-Row Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524Multirow Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531Cursor-Based Deletes and Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539Cursors and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54518 Dynamic SQL * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547Limitations of Static SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548Dynamic SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550Dynamic Statement Execution (EXECUTE IMMEDIATE) . . . . . . . . . . . . . . . . 551Two-Step Dynamic Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554The PREPARE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558The EXECUTE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566The DESCRIBE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572The DECLARE CURSOR Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 574The Dynamic OPEN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575The Dynamic FETCH Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578The Dynamic CLOSE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 13. C o n t e n t s xiiiDynamic SQL Dialects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579Dynamic SQL in Oracle * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580Dynamic SQL and the SQL2 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584Basic Dynamic SQL2 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 584SQL2 and the SQLDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586SQL2 and Dynamic SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 593Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59719 SQL APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599API Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600The dblib API (SQL Server) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602Basic SQL Server Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603SQL Server Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610Positioned Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617ODBC and the SQL/CLI Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622The Call-Level Interface Standardization . . . . . . . . . . . . . . . . . . . . 622CLI Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628CLI Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631CLI Errors and Diagnostic Information . . . . . . . . . . . . . . . . . . . . . . 651CLI Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651CLI Information Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653The ODBC API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655The Structure of ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656ODBC and DBMS Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . 657ODBC Catalog Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658Extended ODBC Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659The Oracle Call Interface (OCI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663OCI Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663Oracle Server Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665Statement Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666Query Results Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667Descriptor Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669Java Database Connectivity (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671JDBC History and Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671JDBC Implementations and Driver Types . . . . . . . . . . . . . . . . . . . . 672The JDBC API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697Part VISQL Today and Tomorrow20 Database Processing and Stored Procedures . . . . . . . . . . . . . 701Stored Procedure Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703A Basic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706Creating a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706Calling a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708 14. xiv S Q L : T h e C o m p l e t e R e f e r e n c eStored Procedure Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710Statement Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715Returning Values via Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . 717Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721Repeated Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723Other Flow-of-Control Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . 725Cursor-Based Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726Handling Error Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730Advantages of Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733Stored Procedure Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734System-Defined Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735External Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736Advantages and Disadvantages of Triggers . . . . . . . . . . . . . . . . . . 737Triggers in Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738Triggers in Informix SPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740Triggers in Oracle PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742Other Trigger Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744Stored Procedures, Triggers, and the SQL Standard . . . . . . . . . . . . . . . . . . . 744The SQL/PSM Stored Procedures Standard . . . . . . . . . . . . . . . . . . 745Trigger Standards in SQL:1999 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75721 SQL and Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . 759Data Warehousing Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760Components of a Data Warehouse . . . . . . . . . . . . . . . . . . . . . . . . . . 762The Evolution of Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . 763Database Architecture for Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764Fact Cubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764Star Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766Multilevel Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768SQL Extensions for Data Warehousing . . . . . . . . . . . . . . . . . . . . . . 770Warehouse Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771Load Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77422 SQL and Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . 775SQL and Web Sites: Early Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . 776Application Servers and Three-Tier Web Site Architectures . . . . . . . . . . . . 777Database Access from Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . 779EJB Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780Session Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781Entity Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785EJB 2.0 Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789Application Server Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794 15. C o n t e n t s xv23 SQL Networking and Distributed Databases . . . . . . . . . . . . . 795The Challenge of Distributed Data Management . . . . . . . . . . . . . . . . . . . . . 796Distributing Data: Practical Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801Remote Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802Remote Data Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806Table Extracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807Table Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809Updateable Replicas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812Replication Trade-Offs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813Typical Replication Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . 814Distributed Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818Remote Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819Remote Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821Distributed Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822The Two-Phase Commit Protocol * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824Network Applications and Database Architecture . . . . . . . . . . . . . . . . . . . . 827Client/Server Applications and Database Architecture . . . . . . . . 828Client/Server Applications with Stored Procedures . . . . . . . . . . . 829Enterprise Applications and Data Caching . . . . . . . . . . . . . . . . . . . 830High-Volume Internet Data Management . . . . . . . . . . . . . . . . . . . 832Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83224 SQL and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835Object-Oriented Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836Object-Oriented Database Characteristics . . . . . . . . . . . . . . . . . . . . 837Pros and Cons of Object-Oriented Databases . . . . . . . . . . . . . . . . . 838Objects and the Database Market . . . . . . . . . . . . . . . . . . . . . . . . . . . 839Object-Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840Large Object Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841BLOBs in the Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842Specialized BLOB Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843Abstract (Structured) Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846Defining Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848Manipulating Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 850Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851Table Inheritance: Implementing Object Classes . . . . . . . . . . . . . . 854Sets, Arrays, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858Defining Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859Querying Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863Manipulating Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864Collections and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . 865User-Defined Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868Methods and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869Object Support in the SQL:1999 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873 16. xvi S Q L : T h e C o m p l e t e R e f e r e n c e25 SQL and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875What Is XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876XML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877XML for Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880XML and SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881Elements vs. Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882Using XML with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885XML Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886XML Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888XML Data Exchange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890XML Storage and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890XML and Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895Document Type Definitions (DTDs) . . . . . . . . . . . . . . . . . . . . . . . . 897XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899XML and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906XQuery Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907Query Processing in XQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909XML Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91226 The Future of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913Database Market Trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915Enterprise Database Market Maturity . . . . . . . . . . . . . . . . . . . . . . . 915Market Diversity and Segmentation . . . . . . . . . . . . . . . . . . . . . . . . 916Packaged Enterprise Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 917Hardware Performance Gains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918Database Server Appliances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919Benchmark Wars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920SQL Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921SQL in the Next Decade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924Distributed Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925Massive Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925Ultra-High-Performance Databases . . . . . . . . . . . . . . . . . . . . . . . . . 926Internet and Network Services Integration . . . . . . . . . . . . . . . . . . . 927Embedded Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928Object Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930Part VIIAppendixesA The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933B Database Vendor Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941A2i, Inc. (www.a2i.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943Arbor Software (www.hyperion.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943Birdstep Technology (www.birdstep.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . 943Computer Associates (www.cai.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944Computer Corporation of America (www.cca-int.com) . . . . . . . . . . . . . . . . 945Empress Software (www.empress.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946 17. C o n t e n t s xviieXcelon (www.exln.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946Gupta Technologies (www.guptaworldwide.com) . . . . . . . . . . . . . . . . . . . . 947Hewlett Packard (www.hp.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947IBM Corporation (www.ibm.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948Informix Software (See IBM Corporation) . . . . . . . . . . . . . . . . . . . . . . . . . . . 949Microsoft Corporation (www.microsoft.com) . . . . . . . . . . . . . . . . . . . . . . . . 950MySQL AB (www.mysql.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951Objectivity (www.objectivity.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951Oracle Corporation (www.oracle.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952Persistence Software (www.persistence.com) . . . . . . . . . . . . . . . . . . . . . . . . 953Pervasive Software (www.pervasive.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . 954PointBase (www.pointbase.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955PostgreSQL (www.postgresql.org) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955Quadbase Systems (www.quadbase.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . 955Red Brick Systems (See IBM Corporation) . . . . . . . . . . . . . . . . . . . . . . . . . . . 956Sybase, Inc. (www.sybase.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957TimesTen Performance Software (www.timesten.com) . . . . . . . . . . . . . . . . 958Versant Corporation (www.versant.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959C SQL Syntax Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961Data Definition Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962Basic Data Manipulation Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964Transaction Processing Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965Cursor-Based Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968Statement Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969Simple Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969D SQL Call-Level Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971CLI Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974General Handle Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974SQL Environment Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 975SQL Connection Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975SQL Statement Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977SQL Statement Execution Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978Query Results Processing Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978Query Results Description Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 980Query Results Descriptor Management Routines . . . . . . . . . . . . . . . . . . . . . . 980Deferred Dynamic Parameter Processing Routines . . . . . . . . . . . . . . . . . . . . . 982Error, Status, and Diagnostic Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982CLI Implementation Information Routines . . . . . . . . . . . . . . . . . . . . . . . . . . 983CLI Parameter Value Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984E SQL Information Schema Standard . . . . . . . . . . . . . . . . . . . . . 991The SCHEMATA View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993The TABLES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994The COLUMNS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994The VIEWS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996 18. xviii S Q L : T h e C o m p l e t e R e f e r e n c eThe VIEW_TABLE_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997The VIEW_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . . . . 997The TABLE_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . . . . . . . . . 998The REFERENTIAL_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . 999The CHECK_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000The KEY_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000The ASSERTIONS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001The CONSTRAINT_TABLE_USAGE View . . . . . . . . . . . . . . . . . . . . . 1001The CONSTRAINT_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . 1002The TABLE_PRIVILEGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003The COLUMN_PRIVILEGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003The USAGE_PRIVILEGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004The DOMAINS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005The DOMAIN_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . . . . . . . 1006The DOMAIN_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . 1007The CHARACTER_SETS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008The COLLATIONS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009The TRANSLATIONS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009The SQL_LANGUAGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010F CD-ROM Installation Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013Installing the SQL DBMS Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014Microsoft SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015Hardware and Software Requirements . . . . . . . . . . . . . . . . . . . . . . 1016Installation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016SQL Server 2000 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017Starting SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018Uninstalling SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018IBM DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018Hardware and Software Requirements . . . . . . . . . . . . . . . . . . . . . . 1019Installation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019DB2 Enterprise Edition Installation . . . . . . . . . . . . . . . . . . . . . . . . . 1019Starting DB2 Enterprise Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021Uninstalling DB2 Enterprise Edition . . . . . . . . . . . . . . . . . . . . . . . . 1021MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021Hardware and Software Requirements . . . . . . . . . . . . . . . . . . . . . . 1021Installation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022MySQL Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022Starting MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023Uninstalling MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023Downloading the Oracle DBMS Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025 19. AcknowledgmentsSpecial thanks to Matan Arazi for once again doing an exceptional job of assemblingthe bonus CD-ROM, pulling off another miracle by squeezing three DBMS productsonto a single CD, and doing so against impossibly tight deadlines.Thanks also to the team at Osborne, including Jane Brownlow, Jennifer Malnick,Martin Przybyla, Greg Guntle, and Chrisa Hotchkiss.xixCopyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use. 20. This page intentionally left blank. 21. IntroductionSQL: The Complete Reference, Second Edition provides a comprehensive, in-depthtreatment of the SQL language for both technical and nontechnical users,programmers, data processing professionals, and managers who want tounderstand the impact of SQL in todays computer industry. This book offers aconceptual framework for understanding and using SQL, describes the history ofSQL and SQL standards, and explains the role of SQL in various computer industrysegments, from enterprise data processing to data warehousing to web site architectures.This new edition contains new chapters specially focused on the role of SQL inapplication server architectures, and the integration of SQL with XML and otherobject-based technologies.This book will show you, step-by-step, how to use SQL features, with manyillustrations and realistic examples to clarify SQL concepts. The book also comparesSQL products from leading DBMS vendorsdescribing their advantages benefits andtrade-offsto help you select the right product for your application. The accompanyingCD-ROM contains actual trial versions of three of the leading brands of SQL DBMS, plusinstructions on how to download a trial version of a fourth version, so you can try themfor yourself and gain actual experience in using the major DBMS products from Oracle,Microsoft, and IBM, and the popular open source DBMS MySQL.xxiCopyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use. 22. xxii S Q L : T h e C o m p l e t e R e f e r e n c eIn some of the chapters, the subject matter is explored at two different levelsafundamental description of the topic, and an advanced discussion intended for computerprofessionals who need to understand some of the internals behind SQL. The moreadvanced information is covered in sections marked with an asterisk (*). You do notneed to read these sections to obtain an understanding of what SQL is and what it does.How This Book Is OrganizedThe book is divided into six parts that cover various aspects of the SQL language: Part One, An Overview of SQL, provides an introduction to SQL and amarket perspective of its role as a database language. Its four chapters describethe history of SQL, the evolution of SQL standards, and how SQL relates tothe relational data model and to earlier database technologies. Part One alsocontains a quick tour of SQL that briefly illustrates its most important featuresand provides you with an overview of the entire language early in the book. Part Two, Retrieving Data, describes the features of SQL that allow youto perform database queries. The first chapter in this part describes the basicstructure of the SQL language. The next four chapters start with the simplestSQL queries and progressively build to more complex queries, includingmultitable queries, summary queries, and queries that use subqueries. Part Three, Updating Data, shows how you can use SQL to add new datato 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 thispart discusses the SQL transaction concept and SQL support for multiusertransaction processing. Part Four, Database Structure, deals with creating and administering aSQL-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 describesthe SQL security scheme that prevents unauthorized access to data, and theSQL system catalog that describes the structure of a database. This part alsodiscusses the significant differences between the database structures supportedby various SQL-based DBMS products. Part Five, Programming with SQL, describes how application programsuse SQL for database access. It discusses the embedded SQL specified by theANSI standard and used by IBM, Oracle, Ingres, Informix, and many otherSQL-based DBMS products. It also describes the dynamic SQL interface thatis used to build general-purpose database tables, such as report writers anddatabase browsing programs. Finally, this part describes the popular SQLAPIs, including ODBC, the ISO-standard Call-Level Interface, and JDBC, the 23. I n t r o d u c t i o n xxiiistandard call-level interface for Java, as well as proprietary call-level interfacessuch as Oracles OCI API. Part Six, SQL Today and Tomorrow, examines the use of SQL in severalof todays hottest application areas, and the current state of SQL-basedDBMS products. Two chapters describe the use of SQL stored proceduresand triggers for online transaction processing, and the contrasting use of SQLfor data warehousing. Four additional chapters describe SQL-based distributeddatabases, the influence of object technologies on SQL, and the integration ofSQL with XML technologies. Finally, the last chapter explores the future of SQLand some of the most important trends in SQL-based data management.Conventions Used In This BookSQL: The Complete Reference, Second Edition describes the SQL features and functionsavailable in the most popular SQL-based DBMS products and those described in theANSI/ISO SQL standards. Whenever possible, the SQL statement syntax described inthis book and used in the examples applies to all dialects of SQL. When the dialectsdiffer, the differences are pointed out in the text, and the examples follow the mostcommon practice. In these cases, you may have to modify the SQL statements in theexamples slightly to suit your particular brand of DBMS.Throughout the book, technical terms appear in italics the first time they are usedand defined. SQL language elements, including SQL keywords, table, and columnnames, and sample SQL statements, appear in an UPPERCASE MONOSPACE font. SQLAPI function names appear in a lowercase monospace font. Program listings alsoappear in monospace font, and use the normal case conventions for the particularprogramming language (uppercase for COBOL and FORTRAN, lowercase for C andJava). Note that these conventions are used solely to improve readability; most SQLimplementations will accept either uppercase or lowercase statements. Many of theSQL examples include query results, which appear immediately following the SQLstatement as they would in an interactive SQL session. In some cases, long queryresults 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 YouSQL: The Complete Reference, Second Edition is the right book for anyone who wants tounderstand and learn SQL, including database users, data processing professionals andarchitects, programmers, students, and managers. It describesin simple, understandablelanguage liberally illustrated with figures and exampleswhat 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 24. xxiv S Q L : T h e C o m p l e t e R e f e r e n c eto describe the differences among the most popular SQL products, including Oracle,Microsoft SQL Server, IBMs DB2 Universal Database and Informix, Sybase, and MySQL.It also explains the importance of SQL-based standards, such as ODBC and JDBC, andthe ANSI/ISO standards for SQL and SQL-related technologies. This second editioncontains new chapters and sections that cover the latest SQL innovations, in the areasof object-relational technologies, XML, and application server architectures.If you are a new to SQL, this book offers comprehensive, step-by-step treatment ofthe language, building from simple queries to more advanced concepts. The structure ofthe book will allow you to quickly start using SQL, but the book will continue to bevaluable as you begin to use the more complex features of the language. You can usethe SQL software on the companion CD-ROM to try out the examples and build yourSQL skills.If you are a data processing professional, architect, or manager, this book will giveyou a perspective on the impact that SQL is having across the information technologyindustryfrom personal computers to mainframes to data warehousing to Internetweb sites and Internet-based distributed applications. The early chapters describe thehistory 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 databasetechnologies, such as distributed databases, object-oriented extensions to SQL, businessintelligence databases, and database/XML integration.If you are a programmer, this book offers a very complete treatment of programmingwith SQL. Unlike the reference manuals of many DBMS products, it offers a conceptualframework for SQL programming, explaining the why as well as the how of developinga SQL-based application. It contrasts the SQL programming interfaces offered by all ofthe leading SQL products, including embedded SQL, dynamic SQL, ODBC, JDBC, andproprietary APIs such as the Oracle Call Interface. The description and comparison ofprogramming 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 betweenthe leading DBMS products are explained, not only in technical terms, but also in termsof their impact on applications and their evolving competitive position in the marketplace.The DBMS software on the companion CD-ROM can be used to try out these featuresin a prototype of your own application.In short, both technical and nontechnical users can benefit from this book. It is themost comprehensive source of information available about the SQL language, SQLfeatures and benefits, popular SQL-based products, the history of SQL, and the impactof SQL on the future direction of the information technology industry. 25. Part IAn Overview of SQLThe first four chapters of this book provide a perspective and a quickintroduction to SQL. Chapter 1 describes what SQL is and explains itsmajor features and benefits. In Chapter 2, a quick tour of SQL showsyou many of its capabilities with simple, rapid-fire examples. Chapter 3offers a market perspective of SQL by tracing its history, describingtheSQL standards and the major vendors of SQL-based products,and identifying the reasons for SQLs prominence today. Chapter 4describes the relational data model upon which SQL is based andcompares it to earlier data models.Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use. 26. This page intentionally left blank. 27. Chapter 1Introduction3Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use. 28. 4 S Q L : T h e C o m p l e t e R e f e r e n c eThe SQL language and relational database systems based on it are one of the mostimportant foundation technologies in the computer industry. Over the last twodecades, SQL has grown from its first commercial use into a computer productand services market segment worth tens of billions of dollars per year, and SQL standstoday as the standard computer database language. Literally hundreds of databaseproducts now support SQL, running on computer systems from mainframes to personalcomputers and even handheld devices. An official international SQL standard has beenadopted and expanded twice. Virtually every major enterprise software product relieson SQL for its data management, and SQL is at the core of the database products fromMicrosoft, Oracle, and IBM, the three largest software companies in the world. SQL isalso at the heart of open-source database products that are helping to fuel the popularityof Linux and the open-source movement. From its obscure beginnings as an IBM researchproject, SQL has leaped to prominence as both an important computer technology anda 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 are there so many different versions and dialects? Howdo popular SQL products like SQL Server, Oracle, Informix, Sybase, and DB2 compare?How does SQL relate to Microsoft standards, such as ODBC and COM? How doesJDBC link SQL to the world of Java and object technology? What role does it play in theemerging web services architecture, and the competing web services architecturesfrom the Microsoft and Java-based camps? Does SQL really scale from mainframes tohandheld devices? Has it really delivered the performance needed for high-volumetransaction processing? How will SQL impact the way you use computers, and howcan you get the most out of this important data management tool?The SQL LanguageSQL is a tool for organizing, managing, and retrieving data stored by a computerdatabase. The acronym SQL is an abbreviation for Structured Query Language. Forhistorical reasons, SQL is usually pronounced sequel, but the alternate pronunciationS.Q.L. is also used. As the name implies, SQL is a computer language that you use tointeract with a database. In fact, SQL works with one specific type of database, called arelational database.Figure 1-1 shows how SQL works. The computer system in the figure has a databasethat stores important information. If the computer system is in a business, the databasemight store inventory, production, sales, or payroll data. On a personal computer, thedatabase might store data about the checks you have written, lists of people and theirphone numbers, or data extracted from a larger computer system. The computerprogram that controls the database is called a database management system, or DBMS.When you need to retrieve data from a database, you use the SQL language to makethe request. The DBMS processes the SQL request, retrieves the requested data, andreturns it to you. This process of requesting data from a database and receiving back theresults is called a database queryhence the name Structured Query Language. 29. C h a p t e r 1 : I n t r o d u c t i o n 5AN OVERVIEW OF SQLFigure 1-1. Using SQL for database accessThe name Structured Query Language is actually somewhat of a misnomer. Firstof all, SQL is far more than a query tool, although that was its original purpose, andretrieving data is still one of its most important functions. SQL is used to control all ofthe functions that a DBMS provides for its users, including: Data definition. SQL lets a user define the structure and organization of thestored data and relationships among the stored data items. Data retrieval. SQL allows a user or an application program to retrieve storeddata from the database and use it. Data manipulation. SQL allows a user or an application program to update thedatabase by adding new data, removing old data, and modifying previouslystored data. Access control. SQL can be used to restrict a users ability to retrieve, add, andmodify data, protecting stored data against unauthorized access. Data sharing. SQL is used to coordinate data sharing by concurrent users,ensuring that they do not interfere with one another. Data integrity. SQL defines integrity constraints in the database, protecting itfrom corruption due to inconsistent updates or system failures.SQL is thus a comprehensive language for controlling and interacting with a databasemanagement system.Second, SQL is not really a complete computer language like COBOL, C, C++, orJava. SQL contains no IF statement for testing conditions, and no GOTO, DO, or FORstatements for program flow control. Instead, SQL is a database sublanguage, consistingof about 40 statements specialized for database management tasks. These SQL statements 30. 6 S Q L : T h e C o m p l e t e R e f e r e n c ecan be embedded into another language, such as COBOL or C, to extend that languagefor use in database access. Alternatively, they can be explicitly sent to a databasemanagement 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.Finally, SQL is not a particularly structured language, especially when compared tohighly structured languages such as C, Pascal, or Java. Instead, SQL statements resembleEnglish sentences, complete with noise words that dont add to the meaning of thestatement but make it read more naturally. There are quite a few inconsistencies inthe SQL language, and there are also some special rules to prevent you from constructingSQL statements that look perfectly legal, but dont make sense.Despite the inaccuracy of its name, SQL has emerged as the standard language forusing relational databases. SQL is both a powerful language and one that is relativelyeasy to learn. The quick tour of SQL in Chapter 2 will give you a good overview of thelanguage and its capabilities.The Role of SQLSQL is not itself a database management system, nor is it a stand-alone product. Youcannot go into a computer store and buy SQL. Instead, SQL is an integral part of adatabase 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 acts as theglue that 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 otherDBMS componentssuch as a forms facility, report writer, or interactive queryfacilityfrom user-written application programs, and even from other computersystems. As the figure shows, SQL plays many different roles: SQL is an interactive query language. Users type SQL commands into aninteractive 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 commandsinto their application programs to access the data in a database. Both user-writtenprograms and database utility programs (such as report writers anddata entry tools) use this technique for database access. SQL is a database administration language. The database administrator responsiblefor managing a minicomputer or mainframe database uses SQL to define thedatabase structure and control access to the stored data. SQL is a client/server language. Personal computer programs use SQL tocommunicate over a network with database servers that store shared data.This client/server architecture has become very popular for enterprise-classapplications. 31. C h a p t e r 1 : I n t r o d u c t i o n 7AN OVERVIEW OF SQL SQL is an Internet data access language. Internet web servers that interact withcorporate data and Internet applications servers all use SQL as a standardlanguage for accessing corporate databases. SQL is a distributed database language. Distributed database management systemsuse SQL to help distribute data across many connected computer systems. TheDBMS software on each system uses SQL to communicate with the othersystems, sending requests for data access. SQL is a database gateway language. In a computer network with a mix ofdifferent DBMS products, SQL is often used in a gateway that allows one brandof DBMS to communicate with another brand.SQL has thus emerged as a useful, powerful tool for linking people, computerprograms, and computer systems to the data stored in a relational database.Figure 1-2. Components of a typical database management system 32. 8 S Q L : T h e C o m p l e t e R e f e r e n c eSQL Features and BenefitsSQL is both an easy-to-understand language and a comprehensive tool for managingdata. Here are some of the major features of SQL and the market forces that have madeit successful: Vendor independence Portability across computer systems SQL standards IBM endorsement and commitment (DB2) Microsoft commitment (SQL Server, ODBC, and ADO) Relational foundation High-level, English-like structure Interactive, ad hoc queries Programmatic database access Multiple views of data Complete database language Dynamic data definition Client/server architecture Enterprise application support Extensibility and object technology Internet database access Java integration (JDBC) Industry infrastructureThese are the reasons why SQL has emerged as the standard tool for managingdata on personal computers, minicomputers, and mainframes. They are described inthe sections that follow.Vendor IndependenceSQL is offered by all of the leading DBMS vendors, and no new database product overthe last decade has been highly successful without SQL support. A SQL-based databaseand the programs that use it can be moved from one DBMS to another vendors DBMSwith minimal conversion effort and little retraining of personnel. Database tools, such asquery tools, report writers, and application generators, work with many different brandsof SQL databases. The vendor independence thus provided by SQL was one of the mostimportant reasons for its early popularity and remains an important feature today. 33. AN OVERVIEW OF SQLPortability Across Computer SystemsC h a p t e r 1 : I n t r o d u c t i o n 9SQL-based database products run on computer systems ranging from mainframes andmidrange systems to personal computers, workstations, a wide range of specializedserver computers, and even handheld devices. They operate on stand-alone computersystems, in departmental local area networks, and in enterprisewide or Internetwidenetworks. SQL-based applications that begin on single-user or departmental serversystems can be moved to larger server systems as they grow. Data from corporateSQL-based databases can be extracted and downloaded into departmental or personaldatabases. Finally, economical personal computers can be used to prototype aSQL-based database application before moving it to an expensive multiuser system.SQL StandardsAn official standard for SQL was initially published by the American National StandardsInstitute (ANSI) and the International Standards Organization (ISO) in 1986, and wasexpanded in 1989 and again in 1992 and 1999. SQL is also a U.S. Federal InformationProcessing Standard (FIPS), making it a key requirement for large government computercontracts. Over the years, other international, government, and vendor groups havepioneered the standardization of new SQL capabilities, such as call-level interfaces orobject-based extensions. Many of these new initiatives have been incorporated into theANSI/ISO standard over time. The evolving standards serve as an official stamp ofapproval for SQL and have speeded its market acceptance.IBM Endorsement and Commitment (DB2)SQL was originally invented by IBM researchers and has since become a strategicproduct for IBM based on its flagship DB2 database. SQL support is available on allmajor IBM product families, from personal computers through midrange systems(AS/400 and UNIX-based servers) to IBM mainframes. IBMs initial work provided aclear signal of IBMs direction for other database and system vendors to follow early inthe development of SQL and relational databases. Later, IBMs commitment and broadsupport speeded the market acceptance of SQL. IBMs SQL reach today extends wellbeyond its own computer systems business. SQL-based products that IBM has developedor acquired now run across a broad range of hardware, in many cases from competingcomputer vendors such as Sun or Hewlett-Packard.Microsoft Commitment (SQL Server, ODBC, and ADO)Microsoft has long considered database access a key part of its Windows personalcomputer software architecture. Both desktop and server versions of Windows providestandardized relational database access through Open Database Connectivity (ODBC),a SQL-based call-level API. Leading Windows software applica


Top Related