17
SQL Clearly Explained

SQL Clearly Explained

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

SQL Clearly Explained

SQL Clearly Explained

Third Edition

Jan L. Harrington

Morgan Kaufmann Publishers is an imprint of Elsevier. 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA This book is printed on acid-free paper. © 2010 ELSEVIER INC. All rights reserved. No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without permission in writing from the publisher. Details on how to seek permission, further information about the Publisher’s permissions policies and our arrangements with organizations such as the Copyright Clearance Center and the Copyright Licensing Agency, can be found at our website: www.elsevier.com/permissions. This book and the individual contributions contained in it are protected under copyright by the Publisher (other than as may be noted herein). Notices Knowledge and best practice in this field are constantly changing. As new research and experience broaden our understanding, changes in research methods, professional practices, or medical treatment may become necessary. Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using any information, methods, compounds, or experiments described herein. In using such information or methods they should be mindful of their own safety and the safety of others, including parties for whom they have a professional responsibility. To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors, assume any liability for any injury and/or damage to persons or property as a matter of products liability, negligence or otherwise, or from any use or operation of any methods, products, instructions, or ideas contained in the material herein. Library of Congress Cataloging-in-Publication Data Harrington, Jan L. SQL clearly explained / Jan L. Harrington. -- 3rd ed. p. cm. Includes indexes. ISBN 978-0-12-375697-8 1. SQL (Computer program language) I. Title. QA76.73.S67H37 2010 005.13'3--dc22 2010009181 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. For information on all Morgan Kaufmann publications, visit our Web site at www.mkp.com or www.elsevierdirect.com Printed in the United States of America 10 11 12 13 14 5 4 3 2 1

v

Contents

Part I: Introduction

Chapter 1: The Relational Data Model 3Schemas and Entities 3 Relations and Tables 5

Columns and Rows 5Domains 7 Primary Keys 7Nulls 9Base Versus Virtual Tables 10

Representing Relationships 10 Types of Relationships 10Foreign Keys and Referential Integrity 16

Views 18 The Design of the Sample Database 19

Chapter 2: Relational Algebra 29Making Vertical Subsets: Project 31 Making Horizontal Subsets: Restrict 33 Choosing Columns and Rows: Restrict and Then Project 35 Union 37 Join 39

A Non-Database Example 39The Equi-Join 40What’s Really Going On: Product and Restrict 42Equi-Joins Over Concatenated Keys 45

vi Contents

Q-Joins 49Outer Joins 50Valid versus Invalid Joins 54

Difference 58 Intersect 59 Divide 61

Chapter 3: Introduction to SQL 65A Bit of SQL History 65 Conformance Levels 67 SQL Environments 69

Interactive SQL Command Processors 70GUI Environments 71The Embedded SQL Dilemma 73

Elements of a SQL Statement 73

Part II: Interactive SQL

Chapter 4: Simple SQL Retrieval 77Choosing Columns 78

Retrieving All Columns 78Retrieving Specific Columns 80Removing Duplicates 80

Ordering the Result Table 82Choosing Rows 84

Predicates 86Performing Row Selection Queries 96

Nulls and Retrieval: Three-Valued Logic 101

Chapter 5: Retrieving Data From More Than One Table 107SQL Syntax for Inner Joins 107

Traditional SQL Joins 107SQL-92 Join Syntax 109Joining Using Concatenated Keys 111Joining More Than Two Tables 114

Finding Multiple Rows in One Table: Joining a Table to Itself 116Correlation Names 117Performing the Same-Table Join 120

Outer Joins 121

Contents vii

Table Constructors in Queries 124Avoiding Joins with Uncorrelated Subqueries 125

Using the IN Operator 126Using the ANY Operator 127Nesting Subqueries 127Replacing a Same-Table Join with Subqueries 129

Chapter 6: Advanced Retrieval Operations 131Union 131

Performing Union Using the Same Source Tables 132Performing Union Using Different Source Tables 134Alternative SQL-92 Union Syntax 135

Negative Queries 136Traditional SQL Negative Queries 136Negative Queries Using the EXCEPT Operator 1430

The EXISTS Operator 140The INTERSECT Operator 142Performing Arithmetic 143

Arithmetic Operators 145Operator Precedence 146

String Manipulation 147Concatenation 147UPPER and LOWER 148TRIM 149SUBSTRING 149

Date and Time Manipulation 151Date and Time System Values 151Date and Time Interval Operations 152OVERLAPS 155EXTRACT 156

CASE Expressions 156

Chapter 7: Working with Groups of Rows 161Set Functions 161

COUNT 163SUM 164AVG 166MIN and MAX 166Set Functions in Predicates 167

Changing Data Types: CAST 167

viii Contents

Grouping Queries 169Forming Groups 170Restricting Groups 175

Windowing and Window Functions 178Ordering the Partitioning 179Specific Functions 184

Chapter 8: Data Modification 197Inserting Rows 197

Inserting One Row 197Copying Existing Rows 189

Updating Data 201Deleting Rows 202

Deletes and Referential Integrity 203MERGE 204

Part III: Managing Database Structure

Chapter 9: Schemas and Tables 211Database Object Hierarchy 211

Naming and Identifying Objects 213Schemas 214

Creating a Schema 215Identifying the Schema You Want to Use 215

Domains 216Tables 218

Column Data Types 219Default Values 223NOT NULL Constraints 223Primary Keys 223Foreign Keys 223Additional Column Constraints 233

Assertions 233Determining When Constraints are Checked 234

Changing the Constraint Mode 235

Contents ix

Chapter 10: Views, Temporary Tables, CTEs, and Indexes 237Views 237

Why Use Views 237Creating Views 238Querying Views 239View Updatability Issues 240

Temporary Tables 241Creating Temporary Tables 245Loading Temporary Tables with Data 245Disposition of Temporary Table Rows 246

Common Table Expressions (CTEs) 244Indexes 248

Deciding Which Indexes to Create 250Creating Indexes 252

Chapter 11: Keeping the Design Up to Date 255Modifying Tables 255

Adding New Columns 256Adding Table Constraints 256Modifying Columns 257Deleting Table Elements 258Renaming Table Elements 259

Modifying Domains 259Deleting Database Elements 260

Chapter 12: Users and Access Rights 263Managing User Accounts 263Granting and Revoking Access Rights 265

Types of Access Rights 266Storing Access Rights 267Granting Rights 268Revoking Rights 269

Roles 270

Chapter 13: Users, Sessions, and Transaction Control 273The Concurrent Use Data Environment 273

Muddying the Waters: Isolation Levels 277Database Sessions and Connections 278

SQL for Connecting and Disconnecting 278Session Length Considerations 279

x Contents

Transaction Control 280Transaction Read/Write Permissions 280Transaction Termination 281Starting Transactions 281Ending Transactions 282Transaction Length Considerations 282

Part IV: SQL Programming

Chapter 14: Writing and Executing SQL Routines and Modules—Triggers and Stored Procedures 287

SQL Programming Elements 288Variables and Assignment 290Selection 292Iteration 295Example #1: Interactive Retrievals 297Example #2: Nested Modules 298

Executing Modules as Triggers 298Executing Modules as Stored Procedures 300

Chapter 15: Embedded SQL 301The Embedded SQL Environment 301Using Host Language Variables 304DBMS Return Codes 305Retrieving a Single Row 306

Indicator Variables 308Retrieving Multiple Rows: Cursors 309

Declaring a Cursor 311Opening a Cursor 314Fetching Rows 314Closing a Cursor 317

Embedded SQL Data Modification 317Direct Modification 317Indicator Variables and Data Modification 318Integrity Validation with the Match Predicate 320Modification Using Cursors 321Deletion Using Cursors 322

Contents xi

Chapter 16: Dynamic SQL 323Immediate Execution 323Dynamic SQL with Dynamic Parameters 326

Dynamic Parameters with Cursors 327Dynamic Parameters and without a Cursor 334

Part V: Non-Relational SQL Extensions

Chapter 17: XML Support 339XML Basics 340

XML Structure 340XML Document Structure 343XML Schemas 346

SQL/XML 348XML Publishing Functions 348

The XML Data Type 358XMLSERIALIZE 361

Chapter 18: The Object-Relational Data Model 363Getting Started: Object-Orientation Without Computing 364Basic OO Concepts 366

Objects 367Classes 371Class Relationships 376

Benefits of Object-Orientation 384Where Objects Work Better Than Relations 384

Pure Object-Oriented Databases 390Representing Data Relationships 390Navigating the Relationships 391

The Object-Relational Data Model 392ER Diagrams for Object-Relational Designs 392Features of the OR Data Model 398

Chapter 19: Object-Relational Support 401An Additional Sample Database 402SQL Data Types for Object-Relational Support 402

Row Type 402Array Type 404Multiset Type 406

xii Contents

User-Defined Data Types and Typed Tables 409UDTs as Domains 409UDTs as Classes 410 Creating Typed Tables Using UDTs 410 Inheritance 411 Reference (REF) Type 412

Methods 414Defining Methods 415Executing Methods 415

Part VI: Appendices

Appendix A: Common Acronyms and Abbreviations 419

Appendix B: SQLSTATE Return Codes 421

Appendix C: SQL Syntax Summary 433

Glossay 445

Index 455

xiii

Preface to the Third Edition

If you have had any contact with a relational database, then it is very likely that you have seen the letters “SQL.” SQL (Struc-tured Query Language) is a computer language designed to manipulate relational databases. You can use it to define a da-tabase’s structure, to modify data, and to retrieve data.

This book has been written to give you an in-depth introduc-tion to using SQL, providing a gentle but complete approach to learning the language. You will learn not only SQL syntax, but also how SQL works. Understanding the “how” as well as the “what” will help you create SQL statements that execute as quickly as possible.

The elements of the SQL language covered in the first four parts of this book are based on those parts of the SQL standard that are for use with pure relational databases. Part V covers two non-relational extensions (XML and object-relational ca-pabilities) that have been part of SQL since 2003. Virtually all database management systems that support SQL will provide the bulk of what you will find in Parts I–IV; implementations of the features in Part V are less common and tend to vary from the standard.

There have been some substantial enhancements to the SQL standard since the second edition of this book, both in the

xiv Preface

relational core features and the non-relational features. These features have been integrated throughout this third edition.

Organization of This BookThe five parts of this book take you from theory to practice:

◊ Part I: The theoretical material underlying relational databases and SQL has been moved into two chapters at the beginning of the book. In previous editions, the material in Chapter 2 (relational algebra) was scattered throughout the book. This organization should make it easier to find. The third chapter in Part I provides an overview of SQL environments.

◊ Part II: Part II covers interactive SQL retrieval. At first, this might seem backwards. Why discuss retrieving data before creating a database and getting data into that da-tabase? There is actually a very good reason for this.

SQL presents someone trying to learn the language with a bit of a catch-22. You need to know how to retrieve data before you can modify it, because modifying data means finding the data you want to change. On the other hand, you need to be able to create a database and enter some data before you have some data on which you can perform retrievals. Like Yossarian trying to meet with Major Major, it doesn’t seem that you can win!

The best alternative is to have someone who knows how to do it create a sample database and load it with data for you. Then you can learn to query that database and carry those techniques over to modifying data. At that point, you’ll have an understanding of SQL basics and will be ready to learn to create databases.

Preface xv

◊ Part III: Part III discusses creating and managing data-base structure. It also covers non-data elements in the database environment, such as managing users/ user ac-counts and transaction control.

◊ Part IV: When SQL-based database environments are being developed, programmers and database adminis-trators do a lot of work using a command-line interface. There are, however, at least two reasons why SQL pro-gramming is very common:

o The typical end-user should not (or cannot) work directly from the SQL command line. We there-fore create application programs to isolate them from direct interaction with the SQL command processor by writing application programs for them to use.

o In many cases, there are actions the database should perform in specific circumstances. We don’t want to require users to remember to do these actions, so we write blocks of program code that are stored within the database to be executed automatically at the appropriate time.

Part IV introduces several techniques for SQL program-ming: embedded SQL (using a high-level host lan-guage), dynamic SQL, and triggers/stored procedures. These chapters teach you syntax of SQL programming constructs, but do not teach programming.

◊ Part V: Part V discusses the non-relational extensions that have been added to the SQL standard: XML and object-relational capabilities. Just as Chapter 1 pres-ents a brief introduction to the relational data model, Chapter 18 covers object-oriented concepts, including the differences between pure object-oriented databases

xvi Preface

and object-relational databases. Chapter 19 then looks at SQL’s object-relational features.

Database SoftwareMuch of today’s commercial database software is very ex-pensive and requires expensive hardware on which to run. If you are looking for a database management system for your own use, you needn’t purchase anything should you choose not to. There are at least two open-source products that will run on reasonable hardware configurations: mySQL (http://www. mysql.com) and PostgreSQL (http://www.postgresql.org). Both are certainly used in commercial settings, but can also function well as learning environments. Distributions are available for Windows, Linux, and Mac OS X.

The SQL commands to create the sample database used in the first four parts of this book and the SQL commands to insert data into those tables can be downloaded from the Morgan Kaufmann Web site.

Teaching MaterialsIf you are using this book as a college text (perhaps jointly with its companion volume, Relational Database Design and Imple-mentation Clearly Explained), you can find teaching support materials on the Morgan Kaufmann Web site. These include a sample syllabus, assignments (and where appropriate, solu-tions), a project description, and exams.

AcknowledgementsAlthough an author spends a lot of time alone in front of the computer, no book can come into being without the coopera-tion and hard work of many people. It may be my name on

Preface xvii

the cover, but without the people at Morgan Kaufmann, you wouldn’t be holding this book right now.

First I’d like to thank the editorial staff, Rick Adams (Senior Acquisitions Editor) and Heather Scherer (Assistant Editor). You’re a joy to work with (as always). Second, I am forever grateful for the production staff, who have done everything they can to make my life easier and to produce a great volume: Anne McGee (Project Manager), Joanne Blank (Designer), and Carol Lewis (Copyeditor).

I also can’t forget my support staff: my mother, my son, and the four fur kids. (Now, if the kittens could just distinguish between my leg and a scratching post, my world would be at peace.)