• • • • • • Table of Contents Index Reviews Reader Reviews Errata Academic SQL in a Nutshell, 2nd Edition By Kevin E Kline Publisher : O'Reilly Pub Date : September 2004 ISBN : 0-596-00481-8 Pages : 710 This essential desktop reference drills down and documents every SQL command and how to use it in both commercial (Oracle, DB2, and Microsoft SQL Server) and open source implementations (PostgreSQL, and MySQL) It includes the command syntax (by vendor, if the syntax differs across implementations), a description, and practical examples And it also explains how the leading commercial and open sources database product implement SQL This wealth of information is packed into a succinct, comprehensive, and extraordinarily easy-to-use format • • • • • • Table of Contents Index Reviews Reader Reviews Errata Academic SQL in a Nutshell, 2nd Edition By Kevin E Kline Publisher : O'Reilly Pub Date : September 2004 ISBN : 0-596-00481-8 Pages : 710 Copyright Preface Why This Book? Who Should Read This Book? How This Book Is Organized How to Use This Book Safari Enabled Changes in the Second Edition Conventions Used in This Book How to Contact Us Resources Acknowledgments Chapter 1 SQL History and Implementations Section 1.1 The Relational Model and ANSI SQL Section 1.2 History of the SQL Standard Section 1.3 SQL Dialects Chapter 2 Foundational Concepts Section 2.1 Database Platforms Described in This Book Section 2.2 Categories of Syntax Section 2.3 SQL2003 and Platform-Specific Datatypes Section 2.4 Constraints Chapter 3 SQL Statement Command Reference Section 3.1 How to Use This Chapter Section 3.2 SQL Platform Support Section 3.3 SQL Command Reference Chapter 4 SQL Functions Section 4.1 Types of Functions Section 4.2 ANSI SQL Aggregate Functions Section 4.3 ANSI SQL Window Functions Section 4.4 ANSI SQL Scalar Functions Section 4.5 Platform-Specific Extensions Chapter 5 Database Programming Section 5.1 Database Programming Overview Section 5.2 Opening a Database Connection Section 5.4 Managing Transactions Section 5.6 Retrieving Data Section 5.8 Error Handling Section 5.3 Closing a Database Connection Section 5.5 Executing Statements Section 5.7 Bound Parameters Section 5.9 Examples Appendix A Sybase Adaptive Server Section A.1 Sybase Adaptive Server Datatypes Section A.2 Sybase Adaptive Server SQL Statements Section A.3 Sybase Adaptive Server SQL Functions Section A.4 Sybase Adaptive Server Keywords Appendix B Shared and Platform-Specific Keywords Colophon Index Copyright © 2004 O'Reilly Media, Inc All rights reserved Printed in the United States of America Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O'Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://safari.oreilly.com) For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc The In a Nutshell series designations, SQL in a Nutshell, the image of a chameleon, and related trade dress are trademarks of O'Reilly Media, 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 Media, 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 and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein Preface Since its first incarnation in the 1970s, the 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 professional needs All the while, standards bodies have developed a growing list of common features SQL in a Nutshell, Second Edition, describes the latest ANSI standard, SQL2003, version of each SQL command, and then documents each platform's implementation of that command In this book, you 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 SQL syntax and commands Most importantly, at least if you're a programmer or developer, is that SQL in a Nutshell, Second Edition, provides a concise guide both to the most popular commercial database packages on the market (Microsoft SQL Server, IBM's DB2 Universal Database, Sybase Adaptive Server, and Oracle), 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 platforms is an affirmation of the growing importance of the open source movement within the computing community The database platforms covered in this book include: IBM DB2 UDB Version 8.0 for Linux, Unix, and Windows MySQL Version 4 Oracle Database 10g PostgreSQL Version 7.0 Microsoft SQL Server 2000 Sybase Adaptive Server Version 12.5 Why This Book? The primary source of information for relational databases is the documentation and help files provided by the vendors themselves While each vendor's documentation is an indispensable resource that most database programmers and database administrators turn to first, it has a number of limitations: It covers only a single, specific vendor's product There is no coverage for translation, migration, or integration issues It describes programming methods, usually in a multitude of small, disconnected documents or help files It covers individual commands, often in confusing detail, obscuring the simple and direct uses of commands that are used every day by programmers and administrators In other words, the documentation included with the vendor database is an exhaustive explanation of every aspect of their platform After all, help texts are naturally geared toward delivering the main facts about the product They'll tell you a command's specific syntax (and all its obscure variants) and, in general terms, how to implement it However, if you move between RDMS's and you need to be productive very quickly, you will rarely use those obscure command variations and instead utilize the common capabilities in real-life situations This book begins where the vendor documentation ends by distilling the experiences of professional database administrators and developers who have used these SQL variants day in and day out to support complex enterprise applications You'll get the benefit of their experience in a compact and easily usable format Whether SQL is new to you or you have been using SQL since its earliest days, there are always new tips and techniques to learn And, when moving between different implementations, it's always important to find out about the implementations that can bite you if you're not careful and informed Who Should Read This Book? SQL in a Nutshell, Second Edition, benefits several groups of users It is useful for programmers who require a concise and handy SQL reference tool It is useful for developers who need to migrate from one SQL dialect to another Finally, it is useful for database administrators (DBAs) who need to both execute a myriad of SQL statements to keep their enterprise databases up and running, and create and manage objects such as tables, indexes, and views This book is a reference work, not a tutorial The writing is not expository For example, we won't explain the concept of an elementary loop Experienced developers already know such things You want the meat So we will explain, for example, the detailed workings of an ANSI standard cursor, how it works on each of the database platforms we cover, the special capabilities of cursors on each database platform, and the various pitfalls of cursors and how to get around them While we don't intend for SQL in a Nutshell, Second Edition, to be a tutorial on SQL or a handbook for database design, we do provide some brief coverage of those topics, and we hope you find that helpful Chapter 1 and Chapter 2 provide a concise introduction to SQL, covering the general origins, essential structure, and basic operation of the language If you're new to SQL, the introductions in Chapter 1 and Chapter 2 will help you to get started [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] TABLE datatype platform comparison SQL Server table subqueries nested table-level constraints TABLE_NAME function (DB2) TABLE_SCHEMA function (DB2) tables TABLESAMPLE clause TAN function DB2 MySQL Oracle PostgreSQL SQL Server TANH function DB2 Oracle testing expressions against range of values TEXT datatype MySQL platform comparison PostgreSQL SQL Server TEXT function (PostgreSQL) TEXTPTR function (SQL Server) TEXTVALID function (SQL Server) TIME datatype DB2 platform comparison PostgreSQL TIME function (DB2) TIME_FORMAT function (MySQL) TIME_TO_SEC function (MySQL) TIMESPAN datatype platform comparison PostgreSQL TIMESTAMP datatype DB2 Oracle platform comparison PostgreSQL SQL Server Sybase adaptive server TIMESTAMP function DB2 PostgreSQL TIMESTAMP_FORMAT function (DB2) TIMESTAMP_ISO function (DB2) TIMESTAMP_TO_SCN function (Oracle) TIMESTAMPDIFF function (DB2) TIMETZ datatype platform comparison PostgreSQL TINYINT datatype platform comparison SQL Server TO_BINARY_DOUBLE function (Oracle) TO_BINARY_FLOAT function (Oracle) TO_CHAR function DB2 Oracle 2nd PostgreSQL TO_CLOB function (Oracle) TO_DATE function DB2 Oracle PostgreSQL TO_DAYS function (MySQL) TO_DSINTERVAL function (Oracle) TO_LOB function (Oracle) TO_MULTI_BYTE function (Oracle) TO_NCHAR function (Oracle) TO_NCLOB function (Oracle) TO_NUMBER function Oracle PostgreSQL TO_SINGLE_BYTE function (Oracle) TO_TIMESTAMP function Oracle PostgreSQL TO_TIMESTAMP_TZ function (Oracle) TO_UNICHAR function TO_YMINTERVAL function (Oracle) Transact-SQL Transaction object ADO.NET JDBC TRANSACTION_NONE isolation level (Transaction object (JDBC)) TRANSACTION_READ_COMMITTED isolation level (Transaction object (JDBC)) TRANSACTION_READ_UNCOMMITTED isolation level (Transaction object (JDBC)) TRANSACTION_REPEATABLE_READ isolation level (Transaction object (JDBC)) TRANSACTION_SERIALIZABLE isolation level (Transaction object (JDBC)) transactions beginning 2nd ADO.NET JDBC closing and making permanent committing ending explicit implicit managing rolling back TRANSLATE function DB2 Oracle 2nd PostgreSQL TREAT function (Oracle) TRIM function TRUNC function DB2 Oracle 2nd PostgreSQL TRUNCATE function DB2 MySQL TRUNCATE TABLE statement DB2 defacto standard syntax keywords MySQL Oracle platform support for PostgreSQL programming tips rules SQL Server TSEQUEL function Twelve Principles of Relational Databases TYPE_ID function (DB2) TYPE_NAME function (DB2) TYPE_SCHEMA function (DB2) TYPEPROPERTY function SQL Server TZ_OFFSET function (Oracle) [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] UCASE function (MySQL) UHIGHSURR function UID function (Oracle) UInt parameter object type (ADO.NET) ULOWSURR function unary operators 2nd UNCOMPRESS function (MySQL) UNHEX function (MySQL) UNICHAR datatype (Sybase adaptive server) UNICODE function SQL Server UNION set operator DB2 keywords MySQL Oracle platform support for PostgreSQL programming tips rules SQL Server SQL2003 syntax UNIQUE constraints UNIQUEIDENTIFIER datatype platform comparison SQL Server 2nd UNISTR function (Oracle) UNIVARCHAR datatype (Sybase adaptive server) UNIX_TIMESTAMP function (MySQL) Unspecified isolation level (Transaction object (ADO.NET)) UPDATE statement DB2 keywords MySQL operators and 2nd Oracle platform support for PostgreSQL programming tips rules SQL Server SQL2003 syntax UPDATEXML function (Oracle) UPPER function URLs MySQL open source Oracle PostgreSQL SQL Server UROWID datatype Oracle platform comparison USCALAR function user USER( ) function (MySQL) USER_ID function (SQL Server) USER_NAME function (SQL Server) USERENV function (Oracle) [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] VALID_NAME function VALID_USER function VALUE function DB2 Oracle VAR function DB2 SQL Server VAR_POP function 2nd Oracle VAR_SAMP function 2nd Oracle VARBINARY datatype platform comparison SQL Server VARBINARY( ) datatype SQL Server Sybase adaptive server VARBIT datatype platform comparison VARCHAR datatype DB2 MySQL Oracle platform comparison PostgreSQL SQL Server VARCHAR FOR BIT DATA datatype DB2 platform comparison VARCHAR function DB2 PostgreSQL VARCHAR( ) datatype SQL Server Sybase adaptive server VARCHAR_FORMAT function (DB2) VARCHAR2 datatype Oracle platform comparison VARGRAPHIC datatype DB2 platform comparison VARGRAPHIC function (DB2) VARIANCE function DB2 Oracle VARP function SQL Server Vector subquery in WHERE/HAVING clause VERSION( ) function (MySQL) views 2nd operators and 2nd VSIZE function (Oracle) [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] WEEK function DB2 MySQL WEEK_ISO function (DB2) WEEKDAY function (MySQL) WHERE clause 2nd 3rd DB2 keywords MySQL Oracle platform support for PostgreSQL programming tips rules SQL Server SQL2003 syntax WHERE clause (SELECT statement) comparison operators and logical operators and WIDTH function (PostgreSQL) WIDTH_BUCKET function window functions 2nd DB2 syntax group clause Oracle syntax ordering clause partitioning clause SQL2003 syntax windowing functions WITH[OUT] SESSION SHUTDOWN [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] Xbase XML datatype Oracle platform comparison XML schema tables based on XMLAGG function (Oracle) XMLCOLATTVAL function (Oracle) XMLCONCAT function (Oracle) XMLELEMENT function (Oracle) XMLFOREST function (Oracle) XMLSEQUENCE function (Oracle) XMLTRANSFORM function (Oracle) [SYMBOL] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] YEAR function DB2 MySQL SQL Server YEARWEEK function (MySQL) ... SEQUEL ultimately became SQL, or Structured Query Language IBM, along with other relational database vendors, wanted a standardized method for accessing and manipulating data in a relational database Although IBM was first to develop relational... information," not as empty strings, blanks, or zeros Metadata (data about the database) must be stored in the database just as regular data is A single language must be able to define data, views, integrity constraints, authorization, transactions, and data... manipulate relational data However, few were as easy to learn or as universally accepted as SQL Programmers and administrators now have the benefit of learning a single language that, with minor adjustments, is applicable to a wide