Head First SQL by Lynn Beighley Copyright © 2007 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 Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or
[email protected]
Series Creators:
Kathy Sierra, Bert Bates
Series Editor:
Brett D. McLaughlin
Editor:
Catherine Nolan
Design Editor:
Louise Barr
Cover Designers:
Louise Barr, Karen Montgomery
Production Editor:
Sanders Kleinfeld
Indexer:
Julie Hawks
Page Viewer:
Andrew Fader
Printing History: August 2007: First Edition.
He’s incredibly patient. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. The Head First series designations, Head First SQL, 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 the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. No clowns, doughnuts, or Girl Sprouts were harmed in the making of this book. Just my car, but it’s been fixed. TM
This book uses RepKover™, a durable and flexible lay-flat binding.
ISBN-10: 0-596-52684-9 ISBN-13: 978-0-596-52684-9 [M]
table of contents
Table of Contents (Summary)
Intro
xxv
1
Data and Tables: A place for everything
1
2
The SELECT Statement: Gifted data retrieval
53
3
DELETE and UPDATE: A change will do you good
119
4
Smart Table Design: Why be normal?
159
5
ALTER: Rewriting the past
197
6
Advanced SELECT: Seeing your data with new eyes
235
7
Multi-table Database Design: Outgrowing your table
281
8
Joins and Multi-table Operations: Can’t we all just get along?
343
9
Subqueries: Queries Within Queries
379
10
Outer Joins, Self Joins, and Unions: New maneuvers
417
11
Constraints, Views, and Transactions: Too many cooks spoil the database
455
12
Security: Protecting your assets
493
Table of Contents (the real thing) Intro Your brain on SQL. Here you are trying to learn something, while here your brain is doing you a favor by making sure the learning doesn’t stick. Your brain’s thinking, “Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea.” So how do you trick your brain into thinking that your life depends on knowing SQL?
Who is this book for?
xxvi
We know what you’re thinking
xxvii
Metacognition
xxix
Bend your brain into submission
xxxi
Read me
xxxii