SQL in a Nutshell
by Kevin Kline , Regina O. Obe , and Leo S. Hsu
Copyright 2022 Kevin Kline, Regina O. Obe, and Leo S. Hsu. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com.
- Acquisitions Editor: Andy Kwan
- Development Editor: Rita Fernando
- Production Editor: Beth Kelly
- Copyeditor: Rachel Head
- Proofreader: Tom Sullivan
- Indexer: WordCo Indexing Services, Inc.
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- June 2022: Fourth Edition
Revision History for the Fourth Edition
- 2022-06-14: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492088868 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. SQL in a Nutshell, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the authors and do not represent the publishers views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or theintellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
978-1-492-08886-8
[LSI]
Preface
Since its first incarnation in the 1970s, the Structured Query Language (SQL) has evolved hand-in-hand with the information boom. As a result, it is the most widely used language to administer and query relational databases. Many software companies and developers, including those in the open source and NoSQL movements, have developed their own SQL dialects in response to specific needs. All the while, standards bodies have codified a growing list of features.
SQL in a Nutshell, 4th edition, describes the latest SQL standardSQL:2016version of many SQL commands, then details how different platforms implement that particular command. (Although the standards bodies have released SQL:2019, the new specifications mostly lie in the area of multidimensional arrays, which have not been implemented by the major vendors. You can read more about how SQL can be used in conjunction with these arrays on the ISO website). In this book, you will find a brief overview of the relational database model, which undergirds all relational database management systems (RDBMSs), followed by a more thorough treatment of frequently used SQL syntax and commands. New in the fourth edition are expanded explanations of programming concepts used in each vendors offerings, such as stored procedures and user-defined functions. And, of course, we have added popular commands that have arrived since the publication of the third edition.
SQL in a Nutshell, 4th edition, provides a concise guide to the two most commonly installed commercial database packages on the market, Oracle and Microsoft SQL Server, and to the three most commonly installed open source databases: MySQL, PostgreSQL, and MariaDB (a fork of MySQL).
The language and database versions covered in this book include:
Why This Book?
The primary and definitive source of information for any given relational database package is the official documentation, along with help files, tutorials, advisories, and other tidbits provided by the vendors themselves. But while each vendors documentation should be the resource that developers and database administrators turn to first, official documentation has a number of limitations:
It describes the vendors implementation of SQL without providing an indication of how well that implementation meets the SQL standard.
It covers only the vendors specific product, without discussing potential translation, migration, or integration issues across different vendors.
It covers individual commands in often unrelenting detail, thereby obscuring the most common use cases.
It typically describes programming methods in an overwhelming number of disconnected articles or help files.
A vendors official documentation can be expected to provide an exhaustive explanation of every aspect of their offering. It will describe every command, including each obscure variant, along with some implementation guidance. Subjective commentaries are off-limits. However, if you move between data platforms and need to be productive quickly, you will rarely need this level of detail on all the obscure command variations. Instead, youre looking for the most applicable usage found 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 products to support complex enterprise applications. It offers you the benefit of their decades of experience in a compact and easily usable format. Whether youre new to SQL or you have been using SQL since its earliest days, there are always new tips and techniques to learn. And when youre moving between different data platforms, its always important to uncover compatibility issues before they bite you.
Who Should Read This Book?
SQL in a Nutshell, 4th edition, targets several groups of users. It will be useful for developers who require a concise and handy SQL reference tool; for developers who need to migrate from one SQL dialect to another; and for database administrators (DBAs) who need to both execute a myriad of SQL statements to keep their enterprise databases up and running, and to 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 wont explain the concept of an elementary nested loop algorithm. Instead, we will explain the workings of the cursor as implemented in the standards, then special capabilities of cursors on each database platform. We include advice, based on experience, on how to avoid and circumvent pitfalls when using cursors.
While we dont intend this book to serve as a tutorial on SQL or a handbook for the database architect, we do provide some coverage of introductory design topics. Chapters provide a concise introduction to the language itself, covering essential structures, basic usage, and some history. If youre new to SQL, these two chapters will help you get going.
How This Book Is Organized
The book is divided into 10 chapters and an appendix:
Discusses the relational database model, describes the current and previous SQL standards, and introduces the SQL implementations covered in this book.Describes the fundamental concepts necessary for understanding relational databases and SQL commands, including different syntax elements, data types, and constraints.Provides the command reference for SQL statements that