Tải bản đầy đủ (.pdf) (10 trang)

SQL VISUAL QUICKSTART GUIDE- P1 pps

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (235.25 KB, 10 trang )

Peachpit Press
VISUAL QUICKSTART GUIDE
SQL
Third Edition
Chris Fehily
Visual QuickStart Guide
SQL, Third Edition
Chris Fehily
Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
510/524-2221 (fax)
Find us on the Web at www.peachpit.com
To report errors, please send a note to
Peachpit Press is a division of Pearson Education
Copyright © 2008 by Chris Fehily
Editor: Cliff Colby
Copy Editor: Kim Wimpsett
Production Coordinator: Tracey Croom
Compositor: David Van Ness
Indexer: Rebecca Plunkett
Cover Design: Peachpit Press
Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any
means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written
permission of the publisher. For information on getting permission for reprints and excerpts,
contact
Notice of Liability
The information in this book is distributed on an “As Is” basis, without warranty. While every


precaution has been taken in the preparation of the book, neither the author nor Peachpit Press
shall have any liability to any person or entity with respect to any loss or damage caused or
alleged to be caused directly or indirectly by the instructions contained in this book or by the
computer software and hardware products described in it.
Trademarks
Visual QuickStart Guide is a registered trademark of Peachpit Press, a division of Pearson
Education.
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 Peachpit was aware of a
trademark claim, the designations appear as requested by the owner of the trademark. All other
product names and services identified throughout this book are used in editorial fashion only and
for the benefit of such companies with no intention of infringement of the trademark. No such use,
or the use of any trade name, is intended to convey endorsement or other affiliation with this book.
ISBN 13: 978-0-321-55357-7
ISBN 10: 0-321-55357-8
987654321
Printed and bound in the United States of America
For my father, Jack Fehily
iv
Special thanks to…
Cliff Colby for two in a row
Kim Wimpsett for the hyphenectomy
Tracey Croom and David Van Ness for
kerning above and beyond
Rebecca Plunkett for the subsubentries
Troels Arvin (technical editor for the second
edition), Bryan Steinweg (technical editor
for the first edition), Russ Abbott, Darren
Pennington for their suggestions and
technical help

The data in the sample database are
fictional. I lifted two book titles from
Iain M. Banks’ Culture novels.
v
Contents at a Glance
Introduction xi
Chapter 1: DBMS Specifics 1
Chapter 2: The Relational Model 33
Chapter 3: SQL Basics 61
Chapter 4: Retrieving Data from a Table 87
Chapter 5: Operators and Functions 127
Chapter 6: Summarizing and Grouping Data 169
Chapter 7: Joins 193
Chapter 8: Subqueries 253
Chapter 9: Set Operations 303
Chapter 10: Inserting, Updating, and
Deleting Rows 315
Chapter 11: Creating, Altering, and
Dropping Tables 337
Chapter 12: Indexes 377
Chapter 13: Views 385
Chapter 14: Transactions 399
Chapter 15: SQL Tricks 405
Index 465
CONTENTS AT A GLANCE
vi
Table of Contents
Introduction xi
About SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi

What You’ll Need . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Chapter 1: DBMS Specifics 1
Running SQL Programs . . . . . . . . . . . . . . . . . . . . . . . 2
Microsoft Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Microsoft SQL Server . . . . . . . . . . . . . . . . . . . . . . . . 10
Oracle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
IBM DB2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
PostgreSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Chapter 2: The Relational Model 33
Tables, Columns, and Rows . . . . . . . . . . . . . . . . . . . 34
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
The Sample Database . . . . . . . . . . . . . . . . . . . . . . . . 51
Creating the Sample Database . . . . . . . . . . . . . . . . 57
Chapter 3: SQL Basics 61
SQL Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
SQL Standards and Conformance . . . . . . . . . . . . . 65
Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Character String Types . . . . . . . . . . . . . . . . . . . . . . . 70
Binary Large Object Type . . . . . . . . . . . . . . . . . . . . 72
Exact Numeric Types . . . . . . . . . . . . . . . . . . . . . . . . 73
Approximate Numeric Types . . . . . . . . . . . . . . . . . 75
Boolean Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Datetime Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Interval Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Unique Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Other Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Nulls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
TABLE OF CONTENTS
Chapter 4: Retrieving Data from a Table 87
Retrieving Columns with
SELECT
and
FROM
. . . . . 88
Creating Column Aliases with
AS
. . . . . . . . . . . . . . 91
Eliminating Duplicate Rows with
DISTINCT
. . . . 93
Sorting Rows with
ORDER BY
. . . . . . . . . . . . . . . . . . . 95
Filtering Rows with
WHERE
. . . . . . . . . . . . . . . . . . . 101
Combining and Negating Conditions with
AND
,
OR
, and
NOT
. . . . . . . . . . . . . . . . . . . . . . . . 105
Matching Patterns with
LIKE

. . . . . . . . . . . . . . . . 114
Range Filtering with
BETWEEN
. . . . . . . . . . . . . . . . 118
List Filtering with
IN
. . . . . . . . . . . . . . . . . . . . . . . . 121
Testing for Nulls with
IS NULL
. . . . . . . . . . . . . . . . 124
Chapter 5: Operators and Functions 127
Creating Derived Columns . . . . . . . . . . . . . . . . . . 128
Performing Arithmetic Operations . . . . . . . . . . . 130
Determining the Order of Evaluation . . . . . . . . . 133
Concatenating Strings with
||
. . . . . . . . . . . . . . . 134
Extracting a Substring with
SUBSTRING()
. . . . . 137
Changing String Case with
UPPER()
and
LOWER()
. . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Trimming Characters with
TRIM()
. . . . . . . . . . . 142
Finding the Length of a String with
CHARACTER_LENGTH()

. . . . . . . . . . . . . . . . . . . . 147
Finding Substrings with
POSITION()
. . . . . . . . . . 149
Performing Datetime and Interval
Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Getting the Current Date and Time . . . . . . . . . . 154
Getting User Information . . . . . . . . . . . . . . . . . . . 156
Converting Data Types with
CAST()
. . . . . . . . . . 157
Evaluating Conditional Values with
CASE
. . . . . . 161
Checking for Nulls with
COALESCE()
. . . . . . . . . . 165
Comparing Expressions with
NULLIF()
. . . . . . . 166
Chapter 6: Summarizing and Grouping Data 169
Using Aggregate Functions . . . . . . . . . . . . . . . . . . 170
Creating Aggregate Expressions . . . . . . . . . . . . . 171
Finding a Minimum with
MIN()
. . . . . . . . . . . . . . 172
Finding a Maximum with
MAX()
. . . . . . . . . . . . . 173
Calculating a Sum with

SUM()
. . . . . . . . . . . . . . . . 174
Calculating an Average with
AVG()
. . . . . . . . . . . 175
Counting Rows with
COUNT()
. . . . . . . . . . . . . . . . 178
Aggregating Distinct Values with
DISTINCT
. . . 179
Grouping Rows with
GROUP BY
. . . . . . . . . . . . . . . . 183
Filtering Groups with
HAVING
. . . . . . . . . . . . . . . . 190
vii
Table of Contents
Table of Contents
viii
Table of Contents
Table of Contents
Chapter 7: Joins 193
Qualifying Column Names . . . . . . . . . . . . . . . . . . 194
Creating Table Aliases with
AS
. . . . . . . . . . . . . . . 196
Using Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Creating Joins with

JOIN
or
WHERE
. . . . . . . . . . . . 200
Creating a Cross Join with
CROSS JOIN
. . . . . . . . 204
Creating a Natural Join with
NATURAL JOIN
. . . . 206
Creating an Inner Join with
INNER JOIN
. . . . . . . 210
Creating Outer Joins with
OUTER JOIN
. . . . . . . . . 235
Creating a Self-Join . . . . . . . . . . . . . . . . . . . . . . . . . 247
Chapter 8: Subqueries 253
Understanding Subqueries . . . . . . . . . . . . . . . . . . 254
Subquery Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Subqueries vs. Joins . . . . . . . . . . . . . . . . . . . . . . . . . 257
Simple and Correlated Subqueries . . . . . . . . . . . 262
Qualifying Column Names in Subqueries . . . . . 267
Nulls in Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . 268
Using Subqueries as Column Expressions . . . . . 270
Comparing a Subquery Value by Using a
Comparison Operator . . . . . . . . . . . . . . . . . . 275
Testing Set Membership with
IN
. . . . . . . . . . . . . 281

Comparing All Subquery Values with
ALL
. . . . . 288
Comparing Some Subquery Values with
ANY
. . 291
Testing Existence with
EXISTS
. . . . . . . . . . . . . . . 294
Comparing Equivalent Queries . . . . . . . . . . . . . . 301
Chapter 9: Set Operations 303
Combining Rows with
UNION
. . . . . . . . . . . . . . . . . 304
Finding Common Rows with
INTERSECT
. . . . . . 310
Finding Different Rows with
EXCEPT
. . . . . . . . . . 312
Chapter 10: Inserting, Updating, and
Deleting Rows 315
Displaying Table Definitions . . . . . . . . . . . . . . . . . 316
Inserting Rows with
INSERT
. . . . . . . . . . . . . . . . . . 319
Updating Rows with
UPDATE
. . . . . . . . . . . . . . . . . 327
Deleting Rows with

DELETE
. . . . . . . . . . . . . . . . . . 333
Chapter 11: Creating, Altering, and
Dropping Tables 337
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Understanding Constraints . . . . . . . . . . . . . . . . . . 339
Creating a New Table with
CREATE TABLE
. . . . . . 341
Forbidding Nulls with
NOT NULL
. . . . . . . . . . . . . . 343
Specifying a Default Value with
DEFAULT
. . . . . . 346
ix
Table of Contents
Table of Contents
Specifying a Primary Key with
PRIMARY KEY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Specifying a Foreign Key with
FOREIGN KEY
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
Forcing Unique Values with
UNIQUE
. . . . . . . . . . . 359
Adding a Check Constraint with
CHECK
. . . . . . . 363

Creating a Temporary Table with
CREATE TEMPORARY TABLE
. . . . . . . . . . . . . . . . . 366
Creating a New Table from an Existing One
with
CREATE TABLE AS
. . . . . . . . . . . . . . . . . . . . 369
Altering a Table with
ALTER TABLE
. . . . . . . . . . . . 373
Dropping a Table with
DROP TABLE
. . . . . . . . . . . . 376
Chapter 12: Indexes 377
Creating an Index with
CREATE INDEX
. . . . . . . . . 378
Dropping an Index with
DROP INDEX
. . . . . . . . . . 383
Chapter 13: Views 385
Creating a View with
CREATE VIEW
. . . . . . . . . . . . 386
Retrieving Data Through a View . . . . . . . . . . . . . 391
Updating Data Through a View . . . . . . . . . . . . . . 394
Dropping a View with
DROP VIEW
. . . . . . . . . . . . . 398
Chapter 14: Transactions 399

Executing a Transaction . . . . . . . . . . . . . . . . . . . . 400
Chapter 15: SQL Tricks 405
Calculating Running Statistics . . . . . . . . . . . . . . . 406
Generating Sequences . . . . . . . . . . . . . . . . . . . . . . 409
Finding Sequences, Runs, and Regions . . . . . . . 415
Limiting the Number of Rows Returned . . . . . . 421
Assigning Ranks . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Calculating a Trimmed Mean . . . . . . . . . . . . . . . . 432
Picking Random Rows . . . . . . . . . . . . . . . . . . . . . . 433
Handling Duplicates . . . . . . . . . . . . . . . . . . . . . . . . 435
Creating a Telephone List . . . . . . . . . . . . . . . . . . . 438
Retrieving Metadata . . . . . . . . . . . . . . . . . . . . . . . . 439
Working with Dates . . . . . . . . . . . . . . . . . . . . . . . . 445
Calculating a Median . . . . . . . . . . . . . . . . . . . . . . . 451
Finding Extreme Values . . . . . . . . . . . . . . . . . . . . . 453
Changing Running Statistics Midstream . . . . . . 454
Pivoting Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Working with Hierarchies . . . . . . . . . . . . . . . . . . . 458
Index 465

×