SQL ® FOR DUMmIES ‰ 5 TH EDITION by Allen G.Taylor SQL ® FOR DUMmIES ‰ 5 TH EDITION by Allen G.Taylor SQL For Dummies®,5th Edition Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030 www.wiley.com Copyright © 2003 by Wiley Publishing, Inc., Indianapolis, Indiana Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permit- ted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate licensing fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8700. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, e-mail: permcoord [email protected]. Trademarks:Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com and related trade dress are trademarks or registered trademarks of Wiley Publishing, Inc., in the United States and other countries, and may not be used without written permissionAll other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK, THEY MAKE NO REPRESENTATIONS OR WAR- RANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTA- TIVES OR WRITTEN SALES MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR YOUR SITUATION. YOU SHOULD CONSULT WITH A PROFESSIONAL WHERE APPRO- PRIATE. NEITHER THE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CON- SEQUENTIAL, OR OTHER DAMAGES. For general information on our other products and services or to obtain technical support, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2003105668 ISBN: 07645-4075-0 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 is a trademark of Wiley Publishing, Inc. About the Author Allen G. Taylor is a 30-year veteran of the computer industry and the author of 22 books, including Crystal Reports 9 For Dummies, Database Development For Dummies, Access Power Programming with VBA, and SQL Weekend Crash Course. He lectures internationally on databases, networks, innovation, and entrepreneurship. He also teaches database development through a leading online education provider and teaches digital electronics at Portland State University. He teaches computer hardware via distance learning at the International Institute for Information, Science & Technology in Shanghai, China. For the latest news on Allen’s activities, check out allengtaylor.com. You can contact Allen at [email protected]. Dedication This book is dedicated to all the seekers who are trying to find their way in the world. May you find what you seek. Acknowledgments First and foremost, I would like to acknowledge the help of Jim Melton, editor of the ISO/ANSI specification for SQL. Without his untiring efforts, this book, and indeed SQL itself as an international standard, would be of much less value. Andrew Eisenberg has also contributed to my knowledge of SQL through his writing. I would also like to thank my project editor, Kala Schrager, and my acquisitions editor, Terri Varveris, for their key contribu- tions to the production of this book. As always, thanks to my agent, Matt Wagner of Waterside Productions, for his support of my career. Publisher’s Acknowledgments We’re proud of this book; please send us your comments through our online registration form located at www.dummies.com/register/. Some of the people who helped bring this book to market include the following: Acquisitions, Editorial, and Production MediaDevelopment Project Coordinator: Ryan Steffen Project Editor: Kala Schrager Layout and Graphics: Jennifer Click, Acquisitions Editor: Terri Varveris Seth Conley, StephanieD. Jumper, Kristin McMullan,Tiffany Muth, Copy Editor: Kim Darosett Jacque Schneider, Mary Gillot Virgin Technical Editor: Tom Farrington Proofreaders: Brian H.Walls, Kathy Simpson, Editorial Manager: Kevin Kirschner TECHBOOKS Production Services Media Development Manager: Indexer: TECHBOOKS Production Services Laura VanWinkle Media Development Supervisor: Richard Graves Editorial Assistant: Cartoons:Rich Tennant (www.the5thwave.com) Publishing and Editorial for Technology Dummies Richard Swadley,Vice President and Executive Group Publisher Andy Cummings,Vice President and Publisher Mary C. Corder,Editorial Director Publishing for Consumer Dummies Diane Graves Steele,Vice President and Publisher Joyce Pepple,Acquisitions Director Composition Services Gerry Fahey,Vice President of Production Services Debbie Stailey,Director of Composition Services Contents at a Glance Introduction ................................................................1 Part I: Basic Concepts ..................................................5 Chapter 1:Relational Database Fundamentals ..............................................................7 Chapter 2: SQL Fundamentals .......................................................................................21 Chapter 3: The Components of SQL ..............................................................................45 Part II: Using SQL to Build Databases .........................71 Chapter 4:Building and Maintaining a Simple Database Structure ..........................73 Chapter 5: Building a Multitable Relational Database ................................................91 Part III: Retrieving Data ..........................................119 Chapter 6: Manipulating Database Data .....................................................................121 Chapter 7: Specifying Values ........................................................................................139 Chapter 8: Advanced SQL Value Expressions ............................................................161 Chapter 9: Zeroing In on the Data You Want ..............................................................173 Chapter 10: Relational Operators ................................................................................199 Chapter 11: Delving Deep with Nested Queries .........................................................223 Chapter 12: Recursive Queries ....................................................................................241 Chapter 13: Providing Database Security ...................................................................251 Part IV: Controlling Operations .................................265 Chapter 14: Protecting Data .........................................................................................267 Chapter 15: Using SQL Within Applications ...............................................................285 Part V: SQL in the Real World ..................................295 Chapter 16:ODBC and JDBC ........................................................................................297 Chapter 17: SQL:2003 and XML ....................................................................................307 Part VI: Advanced Topics .........................................321 Chapter 18: Cursors ......................................................................................................323 Chapter 19: Persistent Stored Modules ......................................................................333 Chapter 20: Error-Handling ..........................................................................................351