100% found this document useful (12 votes)
60 views

Instant ebooks textbook SQL The Complete Reference 2nd Edition James R. Groff download all chapters

Complete

Uploaded by

enssledous
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (12 votes)
60 views

Instant ebooks textbook SQL The Complete Reference 2nd Edition James R. Groff download all chapters

Complete

Uploaded by

enssledous
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Download the full version of the ebook at ebookname.

com

SQL The Complete Reference 2nd Edition James R.


Groff

https://ebookname.com/product/sql-the-complete-
reference-2nd-edition-james-r-groff/

OR CLICK BUTTON

DOWNLOAD EBOOK

Download more ebook instantly today at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Web Design Complete Reference 2nd Edition Thomas A. Powell

https://ebookname.com/product/web-design-complete-reference-2nd-
edition-thomas-a-powell/

ebookname.com

C The Complete Reference Herbert Schildt

https://ebookname.com/product/c-the-complete-reference-herbert-
schildt/

ebookname.com

Red Hat Linux 7 2 The Complete Reference 2nd Edition


Richard Petersen

https://ebookname.com/product/red-hat-linux-7-2-the-complete-
reference-2nd-edition-richard-petersen/

ebookname.com

Web Graphics Perl TK Best of the Perl Journal 1st Edition


Jon Orwant Ph.D.

https://ebookname.com/product/web-graphics-perl-tk-best-of-the-perl-
journal-1st-edition-jon-orwant-ph-d/

ebookname.com
Collaboration in Language Testing and Assessment 1st
Edition Dina Tsagari

https://ebookname.com/product/collaboration-in-language-testing-and-
assessment-1st-edition-dina-tsagari/

ebookname.com

The Book of Nothing John D. Barrow

https://ebookname.com/product/the-book-of-nothing-john-d-barrow/

ebookname.com

Drugs and crime 3rd ed Edition Philip Bean

https://ebookname.com/product/drugs-and-crime-3rd-ed-edition-philip-
bean/

ebookname.com

Stoicism Ancient Philosophies 1st Edition John Sellars

https://ebookname.com/product/stoicism-ancient-philosophies-1st-
edition-john-sellars/

ebookname.com

A Textbook of Computer Oriented Numerical Methods and


Linear Programming 2003rd Edition G.K.Ranganath.
B.Sooryanarayana
https://ebookname.com/product/a-textbook-of-computer-oriented-
numerical-methods-and-linear-programming-2003rd-edition-g-k-ranganath-
b-sooryanarayana/
ebookname.com
Understanding Community Politics Policy and Practice 2nd
Edition Peter Somerville

https://ebookname.com/product/understanding-community-politics-policy-
and-practice-2nd-edition-peter-somerville/

ebookname.com
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9
Blind Folio i

SQL:
The Complete Reference,
Second Edition

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:05 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9
Blind Folio ii

About the Authors


James R. Groff is president and CEO of TimesTen,
a software company that delivers real-time event
processing software to telecom, financial services,
and e-business customers. The core of the TimesTen
software is a high-performance, memory-optimized,
SQL-based DBMS. Prior to joining TimesTen, Groff
was the cofounder, with Paul Weinberg, of Network
Innovations Corporation, a developer of SQL-based
networking software, and coauthor of Understanding
UNIX: A Conceptual Guide and SQL: The Complete
Reference. He has also held senior division management
and marketing positions at Apple Computer and
Hewlett-Packard. Groff holds a B.S. in mathematics
from the Massachusetts Institute of Technology and
an M.B.A. from Harvard University.

Paul N. Weinberg is president and CEO of A2i,


Inc., a software company that develops and markets
a system for enterprise-wide product content
management and single-source, cross-media catalog
publishing. Before that, he was the cofounder, with
Jim Groff, of Network Innovations Corporation, an
early pioneer in client/server database access that
was acquired by Apple Computer, Inc., in 1988, and
the coauthor of Understanding UNIX: A Conceptual
Guide and SQL: The Complete Reference. He has also
held software development and marketing positions
at Bell Laboratories, Hewlett-Packard, and Plexus
Computers. In 1981, he collaborated on The Simple
Solution to Rubik’s Cube, the number-one best-selling
book of that year with over six million copies sold.
He holds a B.S. from the University of Michigan
and an M.S. from Stanford University, both in
Computer Science.

About the Technical Editor


Greg Guntle has been doing technical editing of
computer books for 20 years. He has written several
programming books as well.

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:06 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9
Blind Folio iii

SQL:
The Complete Reference,
Second Edition
James R. Groff and Paul N. Weinberg

McGraw-Hill/Osborne
New York Chicago San Francisco
Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:06 PM
ebook_copyright 6x9.qxd 12/19/02 4:06 PM Page 1

Copyright © 2002 by The McGraw-HIll Companies, Inc. All rights reserved. Manufactured in the
United States of America. 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 data-
base or retrieval system, without the prior written permission of the publisher.

0-07-222817-2

The material in this eBook also appears in the print version of this title: 0-07-222559-9

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after
every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit
of the trademark owner, with no intention of infringement of the trademark. Where such designations
appear in this book, they have been printed with initial caps.
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales pro-
motions, or for use in corporate training programs. For more information, please contact George
Hoare, Special Sales, at george_hoare@mcgraw-hill.com or (212) 904-4069.

TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors
reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted
under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not
decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon,
transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without
McGraw-Hill’s prior consent. You may use the work for your own noncommercial 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 comply with these terms.
THE WORK IS PROVIDED “AS IS”. McGRAW-HILL AND ITS LICENSORS MAKE NO GUAR-
ANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF
OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMA-
TION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE,
AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the func-
tions contained in the work will meet your requirements or that its operation will be uninterrupted or
error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any 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 the
work, even if any of them has been advised of the possibility of such damages. This limitation of lia-
bility shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort
or otherwise.

DOI: 10.1036/0072228172
DOI Page 6x9 10/2/02 1:33 PM Page 1

Want to learn more?


,
We hope you enjoy this McGraw-Hill eBook! If you d like
more information about this book, its author, or related books
and websites, please click here.
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter
FM:v

For more information about this title, click here.

Contents
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

Part I
An Overview of SQL
1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The SQL Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
The Role of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
SQL Features and Benefits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Vendor Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Portability Across Computer Systems . . . . . . . . . . . . . . . . . . . . . . . 9
SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
IBM Endorsement and Commitment (DB2) . . . . . . . . . . . . . . . . . . 9
Microsoft Commitment (SQL Server, ODBC, and ADO) . . . . . . . 9
Relational Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
High-Level, English-Like Structure . . . . . . . . . . . . . . . . . . . . . . . . . 10
Interactive, Ad Hoc Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Programmatic Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Multiple Views of Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Complete Database Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Dynamic Data Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Enterprise Application Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

v
Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:06 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

vi SQL: The Complete Reference

Extensibility and Object Technology . . . . . . . . . . . . . . . . . . . . . . . . 12


Internet Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Java Integration (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Industry Infrastructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 A Quick Tour of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
A Simple Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Retrieving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Summarizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Updating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Protecting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3 SQL in Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
SQL and Database Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
A Brief History of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Early Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Early Relational Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
IBM Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Commercial Acceptance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
The ANSI/ISO Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Other SQL Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
ODBC and the SQL Access Group . . . . . . . . . . . . . . . . . . . . . . . . . . 34
SQL and Portability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
SQL and Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Centralized Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
File Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Client/Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Multitier Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
The Proliferation of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
SQL and IBM’s Unified Database Strategy . . . . . . . . . . . . . . . . . . . 42
SQL on Minicomputers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
SQL on UNIX-Based Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
SQL on Personal Computers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
SQL and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
SQL and Workgroup Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
SQL and Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
SQL and Distributed Internet Applications . . . . . . . . . . . . . . . . . . 48
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Early Data Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
File Management Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Hierarchical Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Network Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:07 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

Contents vii

The Relational Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57


The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Codd’s 12 Rules * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Part II
Retrieving Data
5 SQL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Table Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Numeric Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
String Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Date and Time Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Symbolic Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Built-In Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Missing Data (NULL Values) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6 Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
The SELECT Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The FROM Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Query Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Simple Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Calculated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Selecting All Columns (SELECT *) . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Duplicate Rows (DISTINCT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Row Selection (WHERE Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
The Comparison Test (=, <>, <, <=, >, >=) . . . . . . . . . . . . . . . . . . . 111
The Range Test (BETWEEN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
The Pattern Matching Test (LIKE) . . . . . . . . . . . . . . . . . . . . . . . . . . 120
The Null Value Test (IS NULL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Compound Search Conditions (AND, OR, and NOT) . . . . . . . . . . . . 124
Sorting Query Results (ORDER BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Rules for Single-Table Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Combining Query Results (UNION) * . . . . . . . . . . . . . . . . . . . . . . . . 131
Unions and Duplicate Rows * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:07 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

viii SQL: The Complete Reference

Unions and Sorting * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135


Multiple UNIONs* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

7 Multitable Queries (Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139


A Two-Table Query Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Simple Joins (Equi-Joins) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Parent/Child Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Joins with Row Selection Criteria . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Multiple Matching Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Queries with Three or More Tables . . . . . . . . . . . . . . . . . . . . . . . . . 148
Other Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Non-Equi-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
SQL Considerations for Multitable Queries . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Qualified Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
All-Column Selections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Self-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Table Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Multitable Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
The Structure of a Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Table Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Rules for Multitable Query Processing . . . . . . . . . . . . . . . . . . . . . . 164
Outer Joins * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Left and Right Outer Joins * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Outer Join Notation * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Joins and the SQL2 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Inner Joins in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Outer Joins in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Cross Joins and Union Joins in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . 180
Multitable Joins in SQL2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
8 Summary Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Column Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Computing a Column Total (SUM) . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Computing a Column Average (AVG) . . . . . . . . . . . . . . . . . . . . . . . 191
Finding Extreme Values (MIN and MAX) . . . . . . . . . . . . . . . . . . . . . 191
Counting Data Values (COUNT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Column Functions in the Select List . . . . . . . . . . . . . . . . . . . . . . . . . 195
NULL Values and Column Functions . . . . . . . . . . . . . . . . . . . . . . . . 197
Duplicate Row Elimination (DISTINCT) . . . . . . . . . . . . . . . . . . . . . 199
Grouped Queries (GROUP BY Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Multiple Grouping Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
Restrictions on Grouped Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
NULL Values in Grouping Columns . . . . . . . . . . . . . . . . . . . . . . . . . 209
Group Search Conditions (HAVING Clause) . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Restrictions on Group Search Conditions . . . . . . . . . . . . . . . . . . . . 214
NULL Values and Group Search Conditions . . . . . . . . . . . . . . . . . . 215
HAVING Without GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:07 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

Contents ix

9 Subqueries and Query Expressions . . . . . . . . . . . . . . . . . . . . . 217


Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
What Is a Subquery? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Subqueries in the WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Outer References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Subquery Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
The Subquery Comparison Test (=, <>, <, <=, >, >=) . . . . . . . . . . 223
The Set Membership Test (IN) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
The Existence Test (EXISTS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Quantified Tests (ANY and ALL) * . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Subqueries and Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Nested Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Correlated Subqueries * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Subqueries in the HAVING Clause * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Subquery Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Advanced Queries in SQL2 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Scalar-Valued Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . 247
Row-Valued Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Table-Valued Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Query Expressions (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
SQL Queries: A Final Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

Part III
Updating Data
10 Database Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Adding Data to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
The Single-Row INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . 271
The Multirow INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Bulk Load Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Deleting Data from the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Deleting All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
DELETE with Subquery * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Modifying Data in the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Updating All Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
UPDATE with Subquery * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
11 Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
What Is Data Integrity? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Required Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Simple Validity Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Column Check Constraints (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . 295
Domains (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Entity Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Other Uniqueness Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Uniqueness and NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:08 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

x SQL: The Complete Reference

Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299


Referential Integrity Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Delete and Update Rules * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302
Cascaded Deletes and Updates * . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Referential Cycles * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Foreign Keys and NULL Values * . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Advanced Constraint Capabilities (SQL2) . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
SQL2 Constraint Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Deferred Constraint Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316
Business Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
What Is a Trigger? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320
Triggers and Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Trigger Advantages and Disadvantages . . . . . . . . . . . . . . . . . . . . . 323
Triggers and the SQL Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
12 Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
What Is a Transaction? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
COMMIT and ROLLBACK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
The ANSI/ISO Transaction Model . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Other Transaction Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Transactions: Behind the Scenes * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Transactions and Multiuser Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
The Lost Update Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
The Uncommitted Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
The Inconsistent Data Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
The Phantom Insert Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Concurrent Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Locking * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Locking Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Shared and Exclusive Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Deadlocks * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Advanced Locking Techniques * . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Versioning * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Versioning in Operation * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Versioning Advantages and Disadvantages * . . . . . . . . . . . . . . . . 360
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361

Part IV
Database Structure
13 Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
The Data Definition Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Creating a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Table Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Creating a Table (CREATE TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . 369
Removing a Table (DROP TABLE) . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Changing a Table Definition (ALTER TABLE) . . . . . . . . . . . . . . . . 380

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:08 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

Contents xi

Constraint Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384


Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Domains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Aliases and Synonyms (CREATE/DROP ALIAS) . . . . . . . . . . . . . . . . . . . . . . 386
Indexes (CREATE/DROP INDEX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Managing Other Database Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Single-Database Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Multidatabase Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Multilocation Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Databases on Multiple Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Database Structure and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . 401
SQL2 Catalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
SQL2 Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
14 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
What Is a View? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
How the DBMS Handles Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Advantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Disadvantages of Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Creating a View (CREATE VIEW) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Horizontal Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Vertical Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Row/Column Subset Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Grouped Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Joined Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421
Updating a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
View Updates and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . 424
View Updates in Commercial SQL Products . . . . . . . . . . . . . . . . . 425
Checking View Updates (CHECK OPTION) . . . . . . . . . . . . . . . . . . . 425
Dropping a View (DROP VIEW) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Materialized Views * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
15 SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
SQL Security Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
User-Ids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Security Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Views and SQL Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Granting Privileges (GRANT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Column Privileges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Passing Privileges (GRANT OPTION) . . . . . . . . . . . . . . . . . . . . . . . . 449
Revoking Privileges (REVOKE) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
REVOKE and the GRANT OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
REVOKE and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . . . . 456
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:08 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

xii SQL: The Complete Reference

16 The System Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459


What Is the System Catalog? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
The Catalog and Query Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
The Catalog and the ANSI/ISO Standard . . . . . . . . . . . . . . . . . . . . 461
Catalog Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Table Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
Column Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
View Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Relationship Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
User Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478
Privileges Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480
The SQL2 Information Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Other Catalog Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487

Part V
Programming with SQL
17 Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Programmatic SQL Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
DBMS Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494
Embedded SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Developing an Embedded SQL Program . . . . . . . . . . . . . . . . . . . . 496
Running an Embedded SQL Program . . . . . . . . . . . . . . . . . . . . . . . 500
Simple Embedded SQL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
Declaring Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
Using Host Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515
Data Retrieval in Embedded SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Single-Row Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
Multirow Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531
Cursor-Based Deletes and Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Cursors and Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 545
18 Dynamic SQL * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 547
Limitations of Static SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
Dynamic SQL Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550
Dynamic Statement Execution (EXECUTE IMMEDIATE) . . . . . . . . . . . . . . . . 551
Two-Step Dynamic Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554
The PREPARE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
The EXECUTE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
The DESCRIBE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 572
The DECLARE CURSOR Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 574
The Dynamic OPEN Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575
The Dynamic FETCH Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
The Dynamic CLOSE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:09 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

Contents xiii

Dynamic SQL Dialects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579


Dynamic SQL in Oracle * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 580
Dynamic SQL and the SQL2 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
Basic Dynamic SQL2 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 584
SQL2 and the SQLDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586
SQL2 and Dynamic SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . 593
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597
19 SQL APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599
API Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 600
The dblib API (SQL Server) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 602
Basic SQL Server Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603
SQL Server Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 610
Positioned Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616
Dynamic Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617
ODBC and the SQL/CLI Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
The Call-Level Interface Standardization . . . . . . . . . . . . . . . . . . . . 622
CLI Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 628
CLI Statement Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631
CLI Errors and Diagnostic Information . . . . . . . . . . . . . . . . . . . . . . 651
CLI Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651
CLI Information Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 653
The ODBC API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
The Structure of ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656
ODBC and DBMS Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . 657
ODBC Catalog Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 658
Extended ODBC Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659
The Oracle Call Interface (OCI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
OCI Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663
Oracle Server Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665
Statement Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666
Query Results Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667
Descriptor Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668
Transaction Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
Java Database Connectivity (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
JDBC History and Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671
JDBC Implementations and Driver Types . . . . . . . . . . . . . . . . . . . . 672
The JDBC API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 678
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697

Part VI
SQL Today and Tomorrow
20 Database Processing and Stored Procedures . . . . . . . . . . . . . 701
Stored Procedure Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703
A Basic Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 704
Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
Creating a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 706
Calling a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 708

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:09 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

xiv SQL: The Complete Reference

Stored Procedure Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 710


Statement Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713
Returning a Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
Returning Values via Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 721
Repeated Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
Other Flow-of-Control Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . 725
Cursor-Based Repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726
Handling Error Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 730
Advantages of Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 733
Stored Procedure Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 734
System-Defined Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
External Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 736
Advantages and Disadvantages of Triggers . . . . . . . . . . . . . . . . . . 737
Triggers in Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738
Triggers in Informix SPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 740
Triggers in Oracle PL/SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 742
Other Trigger Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744
Stored Procedures, Triggers, and the SQL Standard . . . . . . . . . . . . . . . . . . . 744
The SQL/PSM Stored Procedures Standard . . . . . . . . . . . . . . . . . . 745
Trigger Standards in SQL:1999 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 755
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 757
21 SQL and Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . 759
Data Warehousing Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 760
Components of a Data Warehouse . . . . . . . . . . . . . . . . . . . . . . . . . . 762
The Evolution of Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . 763
Database Architecture for Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Fact Cubes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 764
Star Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 766
Multilevel Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768
SQL Extensions for Data Warehousing . . . . . . . . . . . . . . . . . . . . . . 770
Warehouse Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
Load Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 771
Query Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 773
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774
22 SQL and Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . 775
SQL and Web Sites: Early Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . 776
Application Servers and Three-Tier Web Site Architectures . . . . . . . . . . . . 777
Database Access from Application Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . 779
EJB Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 780
Session Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 781
Entity Bean Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785
EJB 2.0 Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 789
Application Server Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 794

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:09 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

Contents xv

23 SQL Networking and Distributed Databases . . . . . . . . . . . . . 795


The Challenge of Distributed Data Management . . . . . . . . . . . . . . . . . . . . . 796
Distributing Data: Practical Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 801
Remote Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802
Remote Data Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806
Table Extracts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807
Table Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 809
Updateable Replicas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 812
Replication Trade-Offs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
Typical Replication Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . 814
Distributed Database Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818
Remote Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 819
Remote Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 820
Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821
Distributed Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 822
The Two-Phase Commit Protocol * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 824
Network Applications and Database Architecture . . . . . . . . . . . . . . . . . . . . 827
Client/Server Applications and Database Architecture . . . . . . . . 828
Client/Server Applications with Stored Procedures . . . . . . . . . . . 829
Enterprise Applications and Data Caching . . . . . . . . . . . . . . . . . . . 830
High-Volume Internet Data Management . . . . . . . . . . . . . . . . . . . 832
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 832
24 SQL and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835
Object-Oriented Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 836
Object-Oriented Database Characteristics . . . . . . . . . . . . . . . . . . . . 837
Pros and Cons of Object-Oriented Databases . . . . . . . . . . . . . . . . . 838
Objects and the Database Market . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
Object-Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 840
Large Object Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841
BLOBs in the Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842
Specialized BLOB Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 843
Abstract (Structured) Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 846
Defining Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848
Manipulating Abstract Data Types . . . . . . . . . . . . . . . . . . . . . . . . . 850
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 851
Table Inheritance: Implementing Object Classes . . . . . . . . . . . . . . 854
Sets, Arrays, and Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858
Defining Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 859
Querying Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 863
Manipulating Collection Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864
Collections and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . 865
User-Defined Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868
Methods and Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869
Object Support in the SQL:1999 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . 872
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 873

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:10 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

xvi SQL: The Complete Reference

25 SQL and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 875


What Is XML? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 876
XML Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 877
XML for Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 880
XML and SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 881
Elements vs. Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 882
Using XML with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 885
XML Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886
XML Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 888
XML Data Exchange . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890
XML Storage and Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 890
XML and Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895
Document Type Definitions (DTDs) . . . . . . . . . . . . . . . . . . . . . . . . 897
XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
XML and Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906
XQuery Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 907
Query Processing in XQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909
XML Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912
26 The Future of SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 913
Database Market Trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915
Enterprise Database Market Maturity . . . . . . . . . . . . . . . . . . . . . . . 915
Market Diversity and Segmentation . . . . . . . . . . . . . . . . . . . . . . . . 916
Packaged Enterprise Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 917
Hardware Performance Gains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 918
Database Server Appliances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 919
Benchmark Wars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 920
SQL Standardization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921
SQL in the Next Decade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924
Distributed Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
Massive Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925
Ultra-High-Performance Databases . . . . . . . . . . . . . . . . . . . . . . . . . 926
Internet and Network Services Integration . . . . . . . . . . . . . . . . . . . 927
Embedded Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
Object Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 928
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 930

Part VII
Appendixes
A The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 933
B Database Vendor Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941
A2i, Inc. (www.a2i.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
Arbor Software (www.hyperion.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
Birdstep Technology (www.birdstep.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . 943
Computer Associates (www.cai.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 944
Computer Corporation of America (www.cca-int.com) . . . . . . . . . . . . . . . . 945
Empress Software (www.empress.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:10 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

Contents xvii

eXcelon (www.exln.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 946


Gupta Technologies (www.guptaworldwide.com) . . . . . . . . . . . . . . . . . . . . 947
Hewlett Packard (www.hp.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 947
IBM Corporation (www.ibm.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 948
Informix Software (See IBM Corporation) . . . . . . . . . . . . . . . . . . . . . . . . . . . 949
Microsoft Corporation (www.microsoft.com) . . . . . . . . . . . . . . . . . . . . . . . . 950
MySQL AB (www.mysql.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
Objectivity (www.objectivity.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951
Oracle Corporation (www.oracle.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952
Persistence Software (www.persistence.com) . . . . . . . . . . . . . . . . . . . . . . . . 953
Pervasive Software (www.pervasive.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . 954
PointBase (www.pointbase.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
PostgreSQL (www.postgresql.org) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
Quadbase Systems (www.quadbase.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . 955
Red Brick Systems (See IBM Corporation) . . . . . . . . . . . . . . . . . . . . . . . . . . . 956
Sybase, Inc. (www.sybase.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 957
TimesTen Performance Software (www.timesten.com) . . . . . . . . . . . . . . . . 958
Versant Corporation (www.versant.com) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959
C SQL Syntax Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 961
Data Definition Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 962
Basic Data Manipulation Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964
Transaction Processing Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
Cursor-Based Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965
Query Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 966
Search Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 968
Statement Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
Simple Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969
D SQL Call-Level Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 971
CLI Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
General Handle Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974
SQL Environment Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
SQL Connection Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975
SQL Statement Management Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977
SQL Statement Execution Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Query Results Processing Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 978
Query Results Description Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 980
Query Results Descriptor Management Routines . . . . . . . . . . . . . . . . . . . . . . 980
Deferred Dynamic Parameter Processing Routines . . . . . . . . . . . . . . . . . . . . . 982
Error, Status, and Diagnostic Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 982
CLI Implementation Information Routines . . . . . . . . . . . . . . . . . . . . . . . . . . 983
CLI Parameter Value Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 984
E SQL Information Schema Standard . . . . . . . . . . . . . . . . . . . . . 991
The SCHEMATA View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 993
The TABLES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
The COLUMNS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994
The VIEWS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 996

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:10 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

xviii SQL: The Complete Reference

The VIEW_TABLE_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 997


The VIEW_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . . . . 997
The TABLE_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . . . . . . . . . 998
The REFERENTIAL_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . 999
The CHECK_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
The KEY_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1000
The ASSERTIONS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001
The CONSTRAINT_TABLE_USAGE View . . . . . . . . . . . . . . . . . . . . . 1001
The CONSTRAINT_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . 1002
The TABLE_PRIVILEGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003
The COLUMN_PRIVILEGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003
The USAGE_PRIVILEGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1004
The DOMAINS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1005
The DOMAIN_CONSTRAINTS View . . . . . . . . . . . . . . . . . . . . . . . . . 1006
The DOMAIN_COLUMN_USAGE View . . . . . . . . . . . . . . . . . . . . . . . . 1007
The CHARACTER_SETS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1008
The COLLATIONS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
The TRANSLATIONS View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1009
The SQL_LANGUAGES View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1010
F CD-ROM Installation Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013
Installing the SQL DBMS Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1014
Microsoft SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1015
Hardware and Software Requirements . . . . . . . . . . . . . . . . . . . . . . 1016
Installation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1016
SQL Server 2000 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1017
Starting SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
Uninstalling SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
IBM DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018
Hardware and Software Requirements . . . . . . . . . . . . . . . . . . . . . . 1019
Installation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1019
DB2 Enterprise Edition Installation . . . . . . . . . . . . . . . . . . . . . . . . . 1019
Starting DB2 Enterprise Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021
Uninstalling DB2 Enterprise Edition . . . . . . . . . . . . . . . . . . . . . . . . 1021
MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1021
Hardware and Software Requirements . . . . . . . . . . . . . . . . . . . . . . 1021
Installation Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022
MySQL Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022
Starting MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023
Uninstalling MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023
Downloading the Oracle DBMS Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1023

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1025

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:11 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter
FM:xix

Acknowledgments
pecial thanks to Matan Arazi for once again doing an exceptional job of assembling

S the bonus CD-ROM, pulling off another miracle by squeezing three DBMS products
onto 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.

xix
Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:11 PM
This page intentionally left blank.
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter
FM:xxi

Introduction
QL: The Complete Reference, Second Edition provides a comprehensive, in-depth

S treatment of the SQL language for both technical and nontechnical users,
programmers, data processing professionals, and managers who want to
understand the impact of SQL in today’s computer industry. This book offers a
conceptual framework for understanding and using SQL, describes the history of
SQL and SQL standards, and explains the role of SQL in various computer industry
segments, from enterprise data processing to data warehousing to web site architectures.
This new edition contains new chapters specially focused on the role of SQL in
application server architectures, and the integration of SQL with XML and other
object-based technologies.
This book will show you, step-by-step, how to use SQL features, with many
illustrations and realistic examples to clarify SQL concepts. The book also compares
SQL products from leading DBMS vendors—describing their advantages benefits and
trade-offs—to help you select the right product for your application. The accompanying
CD-ROM contains actual trial versions of three of the leading brands of SQL DBMS, plus
instructions on how to download a trial version of a fourth version, so you can try them
for yourself and gain actual experience in using the major DBMS products from Oracle,
Microsoft, and IBM, and the popular open source DBMS MySQL.

xxi
Copyright 2002 by The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:11 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

xxii SQL: The Complete Reference

In some of the chapters, the subject matter is explored at two different levels—a
fundamental description of the topic, and an advanced discussion intended for computer
professionals who need to understand some of the internals behind SQL. The more
advanced information is covered in sections marked with an asterisk (*). You do not
need to read these sections to obtain an understanding of what SQL is and what it does.

How This Book Is Organized


The 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 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 One 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 Two, “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 Three, “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 Four, “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 Five, “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

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:11 PM
Color profile: Generic CMYK printer profile
Composite Default screen Complete Reference / SQL: TCR / Groff, Weinberg / 222559-9 / Front Matter

Introduction xxiii

standard call-level interface for Java, as well as proprietary call-level interfaces


such as Oracle’s OCI API.
■ Part Six, “SQL Today and Tomorrow,” examines the use of SQL in several
of today’s “hottest” application areas, and the current state of SQL-based
DBMS products. Two chapters describe the use of SQL stored procedures
and triggers for online transaction processing, and the contrasting use of SQL
for data warehousing. Four additional chapters describe SQL-based distributed
databases, the influence of object technologies on SQL, 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.

Conventions Used In This Book


SQL: The Complete Reference, Second Edition describes the SQL features and functions
available in the most popular SQL-based DBMS products and those described in the
ANSI/ISO SQL standards. Whenever possible, the SQL statement syntax described in
this book and used in the examples applies to all dialects of SQL. When the dialects
differ, the differences are pointed out in the text, and the examples follow the most
common practice. In these cases, you may have to modify the SQL statements in the
examples slightly to suit your particular brand of DBMS.
Throughout the book, technical terms appear in italics the first time they are used
and defined. SQL language elements, including SQL keywords, table, and column
names, and sample SQL statements, appear in an UPPERCASE MONOSPACE font. SQL
API function names appear in a lowercase monospace font. Program listings also
appear in monospace font, and use the normal case conventions for the particular
programming language (uppercase for COBOL and FORTRAN, lowercase for C and
Java). Note that these conventions are used solely to improve readability; most SQL
implementations will accept either uppercase or lowercase statements. Many of the
SQL examples include query results, which appear immediately following the SQL
statement as they would in an interactive SQL session. In some cases, long query
results are truncated after a few rows; this is indicated by a vertical ellipsis (…)
following the last row of query results.

Why This Book Is For You


SQL: The Complete Reference, Second Edition is the right book for anyone who wants to
understand and learn SQL, including database users, data processing professionals and
architects, programmers, students, and managers. It describes—in simple, understandable
language liberally illustrated with figures and examples—what SQL is, why it is important,
and how you use it. This book is not specific to one particular brand or dialect of SQL.
Rather, it describes the standard, central core of the SQL language and then goes on

P:\010Comp\CompRef8\559-9\fm.vp
Friday, July 26, 2002 2:25:12 PM
Random documents with unrelated
content Scribd suggests to you:
credit card donations. To donate, please visit:
www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could
be freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose
network of volunteer support.

Project Gutenberg™ eBooks are often created from several


printed editions, all of which are confirmed as not protected by
copyright in the U.S. unless a copyright notice is included. Thus,
we do not necessarily keep eBooks in compliance with any
particular paper edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg
Literary Archive Foundation, how to help produce our new
eBooks, and how to subscribe to our email newsletter to hear
about new eBooks.

You might also like