Copyright 2010 Elsevier Inc.. All rights reserved.
Copyright
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--dc222010009181
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 145 4 3 2 1
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 (Structured Query Language) is a computer language designed to manipulate relational databases. You can use it to define a database's structure, to modify data, and to retrieve data.
This book has been written to give you an in-depth introduction 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 capabilities) 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 IIV; 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 relational core features and the non-relational features. These features have been integrated throughout this third edition.
Organization of This Book
The 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 (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 database? 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.
Part III: Part III discusses creating and managing database structure. It also covers non-data elements in the database environment, such as managing users/ user accounts and transaction control.
Part IV: When SQL-based database environments are being developed, programmers and database administrators do a lot of work using a command-line interface. There are, however, at least two reasons why SQL programming is very common:
The typical end-user should not (or cannot) work directly from the SQL command line. We therefore create application programs to isolate them from direct interaction with the SQL command processor by writing application programs for them to use.
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 programming: embedded SQL (using a high-level host language), 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 then looks at SQL's object-relational features.
Database Software
Much of today's commercial database software is very expensive 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.