IN A NUTSHELL A Desktop Quick Reference SOSOLL SOSOL sql_titlepg.qxd 10/13/00 5:27 PM Page 1 sql_titlepg.qxd 10/13/00 5:27 PM Page 2 Kevin Kline with Daniel Kline Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo IN A NUTSHELL A Desktop Quick Reference SOSOLL SOSOL sql_titlepg.qxd 10/13/00 5:27 PM Page 3 SQL in a Nutshell by Kevin Kline with Daniel Kline Copyright © 2001 O’Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472. Editor: Gigi Estabrook Production Editor: Mary Sheehan Cover Designer: Ellie Volckhausen Printing History: January 2001: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly & Associates, Inc. The association between the image of a chameleon and the topic of SQL is a trademark of O’Reilly & Associates, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher assumes no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Library of Congress Cataloging-in-Publication Data Kline, Kevin E., 1966- SQL in a nutshell : a desktop quick reference/Kevin Kline with Daniel Kline. p. cm. Includes index. ISBN 1-56592-744-3 1. SQL server 2. SQL (Computer program language) 3. Client/server computing. I. Kline, Daniel. QA76.73.S67 K55 2000 005.75'85 dc21 00-065206 ISBN: 1-56592-744-3 [4/01] [M] ,COPYRIGHT.12247 Page iv Friday, March 23, 2001 10:05 AM About the Author Kevin Kline is the team leader for Information Architecture within Shared Informa- tion Services at Deloitte & Touche LLP. Kevin and his team perform data and infrastructure architecture in support of major knowledge management and transac- tion processing systems for Deloitte’s Client Service Technology organization. Kevin is also the author of Transact-SQL Programming (O’Reilly, 1999) (http:// www.oreilly.com/catalog/wintrnssql/ ) and numerous magazine articles on Microsoft SQL Server. When he’s not pulling his hair out over work issues, Kevin likes to romance his wife, play with his three kids, tinker with his ’66 Chevy pickup, and garden. Other than being Kevin’s brother, Daniel Kline is an Assistant Professor of English at the University of Alaska, Anchorage, where he specializes in medieval literature, liter- ary and cultural theory, and computer-assisted pedagogy. He completed his Ph.D. at Indiana University, Bloomington, and in addition to numerous scholarly presentations, Dan recently has published academic essays in Literary and Linguistic Computing, Philological Quarterly, Chaucer Review, and Essays in Medieval Studies. When he’s not spending time with his wife and two boys, Dan frets over his pet project, the Electronic Canterbury Tales (http://cwolf.uaa.alaska.edu/~afdtk/ext_main.htm). Dan can be reached at afdtk@ uaa.alaska.edu. Colophon Our look is the result of reader comments, our own experimentation, and feedback from distribution channels. Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects. The animal on the cover of SQL in a Nutshell is a chameleon. There are approxi- mately 85 species of chameleons existing in the world today. They are mostly indigenous to Africa, although there are a few species found in Asia and in Europe. Most are tree dwellers. The chameleon is relatively small; the average adult size is between 6 inches and 12 inches. It lives mostly on insects, and uses its long tongue to capture its prey. Indeed, the tongue is a critical tool. It can stretch up to 1.5 times the lizard’s body length, and there is an adhesive pad on the end, which the insects are trapped on. There are several other characteristics common to all species of cha- meleons. For example, its eyes are large and protruding, and the lizard can see 360 degrees around without moving its head or body. Its toes are on either side of its feet, usually with three on one side and two on the other. This is ideal for moving quickly and efficiently through tree branches. Chameleons are best known for their ability to change their appearance to adapt to their physical environment. Actually, several types of reptiles can change their skin color, but the chameleon is far and away the most accomplished. This skill, which is moderated by the nervous system, obviously is invaluable for hunting prey and avoiding predators, and also helps to stablize body temperature. The extent of this camouflage capability is related to the gender, age, and species of the lizard. ,AUTHOR.COLO.12098 Page 1 Friday, March 23, 2001 10:05 AM Mary Sheehan was the production editor and proofreader for SQL in a Nutshell, and Jeffrey Holcomb was the copyeditor. Emily Quill and Colleen Gorman provided quality control. Linley Dolby provided production assistance. Brenda Miller wrote the index. Ellie Volckhausen designed the cover of this book, based on a series design by Edie Freedman. The cover image is a 19th-century engraving from the Dover Pictorial Archive. Emma Colby produced the cover layout with QuarkXPress 4.1 using Adobe’s ITC Garamond font. Melanie Wang designed the interior layout based on a series design by Nancy Priest. The text and heading fonts are ITC Garamond Light and Garamond Book. The illus- trations that appear in the book were produced by Robert Romano using Macromedia FreeHand 8 and Adobe Photoshop 5. This colophon was written by Mary Sheehan. Whenever possible, our books use a durable and flexible lay-flat binding. If the page count exceeds this binding’s limit, perfect binding is used. ,AUTHOR.COLO.12098 Page 2 Friday, March 23, 2001 10:05 AM iii Computer Crime: A Crimefighter’s Handbok, eMatter Edition Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved. Table of Contents Preface v Chapter 1—SQL, Vendor Implementations, and Some History 1 The Relational Database Model 1 The Databases Described in This Book 2 The SQL Standard 2 Dialects of SQL 6 Principles of Relational Databases 7 Chapter 2—Foundational Concepts 9 Row Processing Versus Set Processing 9 The Relational Model 10 SQL99 and Vendor-Specific Datatypes 10 Processing NULLS 18 Categories of Syntax 19 Using SQL 23 Conclusion 26 Chapter 3—SQL Statements Command Reference 27 Recommended Reading Approach 27 Quick SQL Command Reference 27 DROP Statements 96 Conclusion 162 ,sql_ianTOC.fm.14129 Page iii Wednesday, November 29, 2000 4:45 PM iv Table of Contents Computer Crime: A Crimefighter’s Handbok, eMatter Edition Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved. Chapter 4—SQL Functions 163 Deterministic and Nondeterministic Functions 163 Types of Functions 164 Vendor Extensions 175 Chapter 5—Unimplemented SQL99 Commands 194 Appendix—SQL99 and Vendor-Specific Keywords 197 Index 205 ,sql_ianTOC.fm.14129 Page iv Wednesday, November 29, 2000 4:45 PM v Book Title, eMatter Edition Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved. Preface The explosive growth of the information technology industry and the constantly growing need to compile, store, access, and manipulate increasingly larger masses of data have required the development of ever more sophisticated database management tools. Since its first incarnation in the 1970s, Structured Query Language (SQL) has been developed hand in hand with the information boom, and as a result, is the most widely used database manipulation language in business and industry. A number of different software companies and program developers, including those in the open source movement, have concurrently developed their own SQL dialects in response to specific needs. All the while, standards bodies have developed a growing list of common features. SQL in a Nutshell identifies the differences between the various vendor implemen- tations of SQL. Readers will find a concise explanation of the Relational Database Management System (RDBMS) model, a clear-cut explanation of foundational RDBMS concepts, and thorough coverage of basic SQL syntax and commands. Most importantly, programmers and developers who use SQL in a Nutshell will find a concise guide both to the most popular commercial database packages on the market (Microsoft SQL Server and Oracle8i), and to two of the best known open source (http://www.opensource.org) database products (MySQL and PostgreSQL). SQL in a Nutshell’s attention to open source SQL products is an affir- mation of the growing importance of the open source movement within the computing community. As a result, SQL in a Nutshell benefits several distinct groups of users: the knowl- edgeable programmer who requires a concise and handy reference tool, the developer who needs to migrate from one SQL dialect to another, and the user who comes to SQL from another programming language and wants to learn the basics of SQL programming. ,ch00.13241 Page v Wednesday, November 29, 2000 4:41 PM vi Preface How This Book Is Organized Book Title, eMatter Edition Copyright © 2000 O’Reilly & Associates, Inc. All rights reserved. How This Book Is Organized This book is divided into five chapters and one appendix: Chapter 1, SQL, Vendor Implementations, and Some History This chapter discusses the Relational Database Model, describes the current and previous SQL standards, and introduces the SQL vendor implementations covered in this book. Chapter 2, Foundational Concepts This chapter describes the fundamental concepts necessary for understanding relational databases and SQL commands. Chapter 3, SQL Statements Command Reference This chapter is an alphabetical command reference. It details each SQL99 command, as well as the implementations of each command by Oracle, Microsoft SQL Server, MySQL, and PostgreSQL. Chapter 4, SQL Functions This chapter is an alphabetical reference of the SQL99 functions, describing vendor implementations of these functions and vendor extensions. Chapter 5, Unimplemented SQL99 Commands This chapter lists commands that are included in the SQL standards, but have not yet been implemented by any of the vendors. Appendix, SQL99 and Vendor-Specific Keywords The appendix provides a table that displays keywords declared in SQL99 and by the various database vendors. Conventions Used in This Book Constant Width Used to indicate programming syntax, code fragments, and examples. Italic Used to introduce new terms, for emphasis, and to indicate commands or user-specified file and directory names. Bold Used to display the names of database objects, such as tables, columns, and stored procedures. UPPERCASE Used to indicate SQL keywords. The owl icon indicates a tip, suggestion, or general note. ,ch00.13241 Page vi Wednesday, November 29, 2000 4:41 PM [...]... Therefore, differences in binding style may be one more reason DBMSs function differently Binding styles go deep into the heart of the database code In general, the SQL commands discussed in this book utilize the Direct SQL Invocation binding style However, when the situation warrants, other relevant binding styles are discussed within the command reference of each specific command Principles of Relational... 23 Wednesday, November 29, 2000 4:41 PM Using SQL Table 2-6: SQL Delimiters and Operators (continued) Usage List item separator Local variable indicator Global variable indicator Identifier qualifier separator Character string indicators Quoted identifier indicators Single-line comment delimiter Beginning multiline comment delimiter Ending multiline comment indicator Concepts Symbol , @ @@ ‘’ “” “”... float8 float(p), 7 . resulting from the use of the information contained herein. Library of Congress Cataloging -in- Publication Data Kline, Kevin E., 1966- SQL in a nutshell : a desktop quick reference/Kevin Kline with. Kline Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo IN A NUTSHELL A Desktop Quick Reference SOSOLL SOSOL sql_ titlepg.qxd 10/13/00 5:27 PM Page 3 SQL in a Nutshell by Kevin Kline with Daniel Kline Copyright © 2001 O’Reilly & Associates, Inc Basic object support • Overloading SQL- invoked functions and procedures • User-defined types with single inheritance; basic SQL routines on user-defined types (including dynamic dispatch) • Reference