sql the complete reference 3rd ed

911

Upload: raja-kumar

Post on 14-Jul-2015

126 views

Category:

Education


11 download

TRANSCRIPT

  • SQL The Complete Reference,

    Third Edition

  • This page intentionally left blank

  • SQL The Complete Reference,

    Third EditionPaul Weinberg

    James GroffAndrew 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, withoutthe 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. Wheresuch 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. Useof 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 thework, 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-Hills 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 THEACCURACY, 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 itslicensors 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 shallMcGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from theuse 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 shallapply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise.

  • About the AuthorsJames 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 Oracles 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 Rubiks 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 Beginners Guide and is coauthor of SQL: A Beginners Guide. He holds a BA in Computer Science from Transylvania University (Lexington, KY).

    About the Technical EditorAaron 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

    Contents at a Glance

    Part I An Overview of SQL

    1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    2 A Quick Tour of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    3 SQL in Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    4 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    Part II Retrieving Data

    5 SQL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

    6 Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

    7 Multitable Queries (Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

    8 Summary Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

    9 Subqueries and Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

    Part III Updating Data

    10 Database Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

    11 Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

    12 Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

    Part IV Database Structure

    13 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315

    14 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

    15 SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

    16 The System Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

    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

    Part VI SQL Today and Tomorrow

    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

    27 The Future of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819

    Part VII Appendixes

    A The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

    B DBMS Vendor Proles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

    C SQL Syntax Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857

    Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 865

  • C o n t e n t s ix

    ix

    ContentsAcknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiiiIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv

    Part I An Overview of SQL

    1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3The SQL Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The Role of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6SQL Success Factors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    Vendor Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Portability Across Computer Systems . . . . . . . . . . . . . . . . . . . . . . . . . . 8Ofcial SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Early IBM Commitment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Microsoft Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Relational Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9High-Level, English-Like Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Interactive, Ad Hoc Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Programmatic Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Multiple Views of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Complete Database Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Dynamic Data Denition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Enterprise Application Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Extensibility and Object Technology . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Internet Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Java Integration (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Open Source Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12Industry Infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

    2 A Quick Tour of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13A Simple Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Summarizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Updating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Protecting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21SQL and the Evolution of Database Management . . . . . . . . . . . . . . . . . . . . . . 21A Brief History of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    The Early Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22Early Relational Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22IBM Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24Commercial Acceptance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26The ANSI/ISO Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Other Early SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29ODBC and the SQL Access Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29JDBC and Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30SQL and Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

    SQL and Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Centralized Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32File Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34Multitier Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

    The Proliferation of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36SQL on Mainframes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36SQL on Minicomputers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36SQL on UNIX-Based Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37SQL on Personal Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37SQL and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38SQL and Workgroup Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39SQL, Data Warehousing, and Business Intelligence . . . . . . . . . . . . . . 40SQL and Internet Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    4 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Early Data Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    File Management Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Hierarchical Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Network Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

    The Relational Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

    Codds 12 Rules for Relational Databases* . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

  • C o n t e n t s xi C o n t e n t s xi

    Part II Retrieving Data

    5 SQL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

    Table Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

    Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

    Numeric Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Date and Time Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Symbolic Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

    Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Missing Data (NULL Values) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

    6 Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85The SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

    The SELECT Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87The FROM Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

    Query Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

    Calculated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Selecting All Columns (SELECT *) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Duplicate Rows (DISTINCT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94Row Selection (WHERE Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97The Comparison Test (=, , =) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

    The Range Test (BETWEEN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102The Pattern Matching Test (LIKE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104The Null Value Test (IS NULL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Compound Search Conditions (AND, OR, and NOT) . . . . . . . . . . . . 107

    Sorting Query Results (ORDER BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Rules for Single-Table Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

    Combining Query Results (UNION)* . . . . . . . . . . . . . . . . . . . . . . . . . . 113Unions and Duplicate Rows* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Unions and Sorting* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Multiple 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) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119A Two-Table Query Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119Simple Joins (Equi-Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

    Parent/Child Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123An Alternative Way to Specify Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Joins with Row Selection Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126Multiple Matching Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Natural Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Queries with Three or More Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Other Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

    Non-Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134SQL Considerations for Multitable Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

    Qualied Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135All-Column Selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136Self-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Table Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

    Multitable Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141The Structure of a Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

    Table Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Rules for Multitable Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . 143

    Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Left and Right Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148Older Outer Join Notation* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

    Joins and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Inner Joins in Standard SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Outer Joins in Standard SQL* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154Cross Joins in Standard SQL* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Multitable Joins in Standard SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

    8 Summary Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

    Computing a Column Total (SUM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165Computing a Column Average (AVG) . . . . . . . . . . . . . . . . . . . . . . . . . 166Finding Extreme Values (MIN and MAX) . . . . . . . . . . . . . . . . . . . . . . 166Counting Data Values (COUNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Column Functions in the Select List . . . . . . . . . . . . . . . . . . . . . . . . . . . 169NULL Values and Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 171Duplicate Row Elimination (DISTINCT) . . . . . . . . . . . . . . . . . . . . . . . 173

    Grouped Queries (GROUP BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173Multiple Grouping Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176Restrictions on Grouped Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179NULL 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) . . . . . . . . . . . . . . . . . . . . . . . . . . 182Restrictions on Group Search Conditions . . . . . . . . . . . . . . . . . . . . . . . 185NULL Values and Group Search Conditions . . . . . . . . . . . . . . . . . . . . 186HAVING Without GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186

    9 Subqueries and Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187

    What Is a Subquery? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188Subqueries in the WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Outer References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191

    Subquery Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192The Subquery Comparison Test (=, , =) . . . . . . . . . . . . . 192The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194The Existence Test (EXISTS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Quantied Tests (ANY and ALL)* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

    Subqueries and Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Nested Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Correlated Subqueries* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Subqueries in the HAVING Clause* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208Subquery Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209Advanced Queries* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

    Scalar-Valued Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213Row-Valued Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218Table-Valued Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

    SQL Queries: A Final Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

    Part III Updating Data

    10 Database Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

    The Single-Row INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232The Multirow INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235Bulk Load Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

    Deleting Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239Deleting All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240DELETE with Subquery* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

    Modifying Data in the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243Updating All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245UPDATE 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247What Is Data Integrity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248Required Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249Simple Validity Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

    Column Check Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

    Entity Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Other Uniqueness Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Uniqueness and NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

    Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255Referential Integrity Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256Delete and Update Rules* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Cascaded Deletes and Updates* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262Referential Cycles* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262Foreign Keys and NULL Values* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

    Advanced Constraint Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270SQL Constraint Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270Deferred Constraint Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

    Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274What Is a Trigger? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Triggers and Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Trigger Advantages and Disadvantages . . . . . . . . . . . . . . . . . . . . . . . . 277Triggers and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

    12 Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281What Is a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282The ANSI/ISO SQL Transaction Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

    The START TRANSACTION and SET TRANSACTION Statements . . . 284The SAVEPOINT and RELEASE SAVEPOINT Statements . . . . . . . . 286The COMMIT and ROLLBACK Statements . . . . . . . . . . . . . . . . . . . . . 286

    Transactions: Behind the Scenes* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289Transactions and Multiuser Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

    The Lost Update Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291The Uncommitted Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292The Inconsistent Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293The Phantom Insert Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294Concurrent Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

    Locking* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297Locking Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Shared and Exclusive Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Deadlocks* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Advanced Locking Techniques* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

  • C o n t e n t s xv C o n t e n t s xv

    Versioning* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307Versioning in Operation* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308Versioning Advantages and Disadvantages* . . . . . . . . . . . . . . . . . . . . 311

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

    Part IV Database Structure

    13 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315The Data Denition Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Table Denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

    Creating a Table (CREATE TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318Removing a Table (DROP TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327Changing a Table Denition (ALTER TABLE) . . . . . . . . . . . . . . . . . . . 328

    Constraint Denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

    Aliases and Synonyms (CREATE/DROP ALIAS) . . . . . . . . . . . . . . . . . . . . . . 333Indexes (CREATE/DROP INDEX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335Managing Other Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342

    Single-Database Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343Multidatabase Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344Multilocation Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346Databases on Multiple Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348

    Database Structure and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . 348Catalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354

    14 View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355What Is a View? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355How the DBMS Handles Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Advantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357Disadvantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358

    Creating a View (CREATE VIEW) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358Horizontal Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359Vertical Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Row/Column Subset Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361Grouped Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363Joined Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364

    Updating a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366View Updates and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . 367View Updates in Commercial SQL Products . . . . . . . . . . . . . . . . . . . . 368Checking 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) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371Materialized Views* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374

    15 SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375SQL Security Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

    User-Ids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376Security Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381

    Views and SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384Granting Privileges (GRANT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386

    Column Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388Passing Privileges (GRANT OPTION) . . . . . . . . . . . . . . . . . . . . . . . . . 389

    Revoking Privileges (REVOKE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391REVOKE and the GRANT OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . 393REVOKE and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . . . . 394

    Role-Based Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398

    16 The System Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399What Is the System Catalog? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399

    The Catalog and Query Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400The Catalog and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . 401Catalog Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

    Table Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403Column Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407View Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412Relationship Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415Privileges Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417The SQL Information Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418Other Catalog Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

    Part V Programming with SQL

    17 Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429Programmatic SQL Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

    DBMS Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431Embedded SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433Developing an Embedded SQL Program . . . . . . . . . . . . . . . . . . . . . . . 434Running 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439Declaring Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443Using Host Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451

    Data Retrieval in Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457Single-Row Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457Multirow Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464

    Cursor-Based Deletes and Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470Cursors and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476

    18 Dynamic SQL* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477Limitations of Static SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477Dynamic SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479Dynamic Statement Execution (EXECUTE IMMEDIATE) . . . . . . . . . . . . . . . 480Two-Step Dynamic Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

    The PREPARE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485The EXECUTE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486

    Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493The DESCRIBE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495The DECLARE CURSOR Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 500The Dynamic OPEN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500The Dynamic FETCH Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503The Dynamic CLOSE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504

    Dynamic SQL Dialects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504Dynamic SQL in Oracle* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504

    Dynamic SQL and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508Basic Dynamic SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508The Standard SQLDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510The SQL Standard and Dynamic SQL Queries . . . . . . . . . . . . . . . . . . 515

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

    19 SQL APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521API Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 522The dblib API (SQL Server) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

    Basic SQL Server Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524SQL Server Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 532Positioned Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 540

    ODBC and the SQL/CLI Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549The Call-Level Interface Standardization . . . . . . . . . . . . . . . . . . . . . . . 549CLI Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552CLI Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557CLI Errors and Diagnostic Information . . . . . . . . . . . . . . . . . . . . . . . . 575CLI Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 577CLI 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579The Structure of ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580ODBC and DBMS Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581ODBC Catalog Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581Extended ODBC Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 582

    The Oracle Call Interface (OCI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586OCI Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586Oracle Server Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 588Statement Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589Query Results Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590Descriptor Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 590

    Java Database Connectivity (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592JDBC History and Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 592JDBC Implementations and Driver Types . . . . . . . . . . . . . . . . . . . . . . 593The JDBC API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 598

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614

    Part VI SQL Today and Tomorrow

    20 Database Processing and Stored Procedural SQL . . . . . . . . . . . . . . . . . . . . . 617Procedural SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 618A Basic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 620Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 621

    Creating a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622Calling a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 624Stored Procedure Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625Statement Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 627Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 630Returning Values via Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634Repeated Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636Other Flow-of-Control Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638Cursor-Based Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639Handling Error Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643

    Advantages of Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645Stored Procedure Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646System-Dened Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647External Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 648

    Advantages and Disadvantages of Triggers . . . . . . . . . . . . . . . . . . . . . 649Triggers in Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649Triggers in Informix SPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651Triggers in Oracle PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653Other 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 . . . . . . . . . . 655The SQL/PSM Stored Procedures Standard . . . . . . . . . . . . . . . . . . . . 656The SQL/PSM Triggers Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 664

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666

    21 SQL and Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667Data Warehousing Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668

    Components of a Data Warehouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669The Evolution of Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . 670

    Database Architecture for Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671Fact Cubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672Star Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673Multilevel Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675SQL Extensions for Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . 676

    Warehouse Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678Load Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 679

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680

    22 SQL and Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681SQL and Web Sites: Early Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . 681Application Servers and Three-Tier Web Site Architectures . . . . . . . . . . . . . 682Database Access from Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . 684

    EJB Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685Session Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686Entity Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689EJB 2.0 Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692EJB 3.0 Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693Open Source Application Development . . . . . . . . . . . . . . . . . . . . . . . . 695

    Application Server Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 698

    23 SQL Networking and Distributed Databases . . . . . . . . . . . . . . . . . . . . . . . . 699The Challenge of Distributed Data Management . . . . . . . . . . . . . . . . . . . . . . 700Distributing Data: Practical Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704

    Remote Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705Remote Data Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708Table Extracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709Table Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 711Updateable Replicas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713Replication Trade-Offs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715Typical Replication Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715

    Distributed Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 719Remote Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720Remote Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722Distributed 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* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724Network Applications and Database Architecture . . . . . . . . . . . . . . . . . . . . . 727

    Client/Server Applications and Database Architecture . . . . . . . . . . . 728Client/Server Applications with Stored Procedures . . . . . . . . . . . . . 729Enterprise Applications and Data Caching . . . . . . . . . . . . . . . . . . . . . 730High-Volume Internet Data Management . . . . . . . . . . . . . . . . . . . . . . 731

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733

    24 SQL and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735Object-Oriented Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735

    Object-Oriented Database Characteristics . . . . . . . . . . . . . . . . . . . . . . 736Pros and Cons of Object-Oriented Databases . . . . . . . . . . . . . . . . . . . 737Objects and the Database Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738

    Object-Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739Large Object Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740LOBs in the Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740Specialized LOB Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742

    Abstract (Structured) Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744Dening Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746Manipulating Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748

    Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 749Table Inheritance: Implementing Object Classes . . . . . . . . . . . . . . . . . 751

    Sets, Arrays, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754Dening Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755Querying Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758Manipulating Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 759Collections and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760

    User-Dened Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 762Methods and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 763Object Support in the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 767

    25 SQL and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769What Is XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 769XML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771XML for Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773

    XML and SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774Elements vs. Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775

    Using XML with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 777XML Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778XML Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 782XML Data Exchange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 784XML 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788Document Type Denitions (DTDs) . . . . . . . . . . . . . . . . . . . . . . . . . . . 790XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791

    XML and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 797XQuery Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798Query Processing in XQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 800

    XML Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 803

    26 Specialty Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805Very Low Latency and In-Memory Databases . . . . . . . . . . . . . . . . . . . . . . . . . 805

    Anatomy of an In-Memory Database . . . . . . . . . . . . . . . . . . . . . . . . . . 806In-Memory Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808

    Caching with In-Memory Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . 808Complex Event-Processing and Stream Databases . . . . . . . . . . . . . . . . . . . . . 810

    Continuous Queries in Stream Databases . . . . . . . . . . . . . . . . . . . . . . 811Stream Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812Stream Database Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813

    Embedded Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 814Embedded Database Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . 815Embedded Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . 815

    Mobile Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816Mobile Database Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816Mobile Database Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 817

    Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818

    27 The Future of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819Database Market Trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820

    Enterprise Database Market Maturity . . . . . . . . . . . . . . . . . . . . . . . . . . 820Market Diversity and Segmentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 821Packaged Enterprise Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822Software-as-a-Service (SaaS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823Hardware Performance Gains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 823Database Server Appliances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824SQL Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825

    SQL in the Next Decade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826Distributed Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826Massive Data Warehousing for Business Optimization . . . . . . . . . . . 826Ultrahigh-Performance Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 827Internet and Network Services Integration . . . . . . . . . . . . . . . . . . . . . 828Embedded Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829Object Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 829Cloud-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

    A The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835

    B DBMS Vendor Proles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841

    C SQL Syntax Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857Data Denition Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858Access Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859Basic Data Manipulation Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859Transaction-Processing Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860Cursor-Based Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 860Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 862Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863Statement Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863Simple 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

    Its 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

  • This page intentionally left blank

  • 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 todays 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 vendorsdescribing their advantages, benefits, and trade-offsto 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 levelsa 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 OrganizedThe 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 Oracles OCI API.

    Part VI, SQL Today and Tomorrow, examines the use of SQL in several of todays 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 BookSQL: 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 YouSQL: 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 describesin simple, understandable language 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 to 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, 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 industryfrom 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.

  • IAn 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 SQLs prominence today. Chapter 4 describes the relational data model upon which SQL is based and compares it with earlier data models.

    CHAPTER 1Introduction

    CHAPTER 2A Quick Tour of SQL

    CHAPTER 3SQL in Perspective

    CHAPTER 4Relational Databases

    PART

  • This page intentionally left blank

  • 1Introduction

    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 LanguageSQL 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 queryhence 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 5PART 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 users 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 p