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

SQL AIO desk reference for dummies jul 2007

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 (13.61 MB, 723 trang )


SQL
ALL-IN-ONE DESK REFERENCE

FOR

DUMmIES



by Allen G. Taylor



SQL
ALL-IN-ONE DESK REFERENCE

FOR

DUMmIES





SQL
ALL-IN-ONE DESK REFERENCE

FOR

DUMmIES





by Allen G. Taylor


SQL All-in-One Desk Reference For Dummies®
Published by
Wiley Publishing, Inc.
111 River Street
Hoboken, NJ 07030-5774
www.wiley.com
Copyright © 2007 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 permitted 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 per-copy fee to the
Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600.
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-4355, or online at
/>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 John Wiley & Sons, Inc. and/or its affiliates in the United
States and other countries, and may not be used without written permission. All 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: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS
OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND
SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A

PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS.
THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS
SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING,
OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE
FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS
WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE
AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR
RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN
THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT
IS READ.

For general information on our other products and services, 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.
For technical support, please visit www.wiley.com/techsupport.
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: 2007926383
ISBN: 978-0-470-11928-0
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1


About the Author
Allen G. Taylor is a 30-year veteran of the computer industry and the author
of 25 books, including SQL For Dummies, Crystal Reports 10 For Dummies,
Database Development For Dummies, Access 2003 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 microprocessor design at Portland State University. For the latest news on Allen’s
activities, check out www.DatabaseCentral.Info. You can contact Allen at





Dedication
This book is dedicated to Joyce Carolyn Taylor, who still puts up with me
after all these years.



Author’s 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, Nicole
Sholly, and my acquisitions editor, Kyle Looper, for their key contributions to
the production of this book. Thanks also to my agent, Carole McClendon of
Waterside Productions, for her 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
Media Development

Composition Services

Project Editor: Nicole Sholly

Acquisitions Editor: Kyle Looper
Copy Editor: Linda Morris
Technical Editor: Robert Schneider
Editorial Manager: Kevin Kirschner
Media Development and Quality Assurance:
Angela Denny, Kate Jenkins,
Steven Kudirka, Kit Malone

Project Coordinator: Kristie Rees
Layout and Graphics: Carl Byers,
Carrie A. Foster, Denny Hager,
Stephanie D. Jumper, Heather Ryan
Proofreaders: Laura Albert, Aptara,
John Greenough
Indexer: Aptara
Anniversary Logo Design: Richard Pacifico

Media Development Coordinator:
Jenny Swisher
Media Project Supervisor:
Laura Moss-Hollister
Editorial Assistant: Amanda Foxworth
Sr. Editorial Assistant: Cherie Case
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 Bednarek, Executive Acquisitions Director

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
Book I: SQL Concepts....................................................7
Chapter 1: Relational Database Basics ............................................................................9
Chapter 2: Modeling a System ........................................................................................27
Chapter 3: SQL Overview ................................................................................................49
Chapter 4: SQL and the Relational Model .....................................................................59
Chapter 5: The Major Components of SQL ...................................................................67
Chapter 6: SQL Characteristics ......................................................................................89

Book II: Relational Database Development .................117
Chapter 1: System Development Overview ................................................................119
Chapter 2: Building a Database Model.........................................................................135
Chapter 3: Balancing Performance and Correctness.................................................159
Chapter 4: Creating a Database with SQL....................................................................189

Book III: SQL Queries................................................201
Chapter 1: Values, Variables, Functions, and Expressions........................................203
Chapter 2: SELECT Statements and Modifying Clauses ............................................227
Chapter 3: Querying Multiple Tables with Subqueries..............................................269
Chapter 4: Querying Multiple Tables with Relational Operators.............................297

Chapter 5: Cursors .........................................................................................................315

Book IV: Data Security..............................................325
Chapter 1: Protecting Against Hardware Failure and External Threats ..................327
Chapter 2: Protecting Against User Errors and Conflicts..........................................343
Chapter 3: Assigning Access Privileges .......................................................................371
Chapter 4: Error Handling .............................................................................................383

Book V: SQL and Programming ..................................397
Chapter 1: Database Development Environments .....................................................399
Chapter 2: Interfacing SQL to a Procedural Language...............................................403
Chapter 3: Using SQL in an Application Program.......................................................409
Chapter 4: Designing a Sample Application ................................................................423
Chapter 5: Building a Sample Application...................................................................443
Chapter 6: SQL’s Procedural Capabilities....................................................................459
Chapter 7: Connecting to a Remote Database ............................................................475


Book VI: SQL and XML..............................................489
Chapter 1: XML/SQL Basics...........................................................................................491
Chapter 2: Storing XML Data in SQL Tables................................................................515
Chapter 3: Retrieving Data from XML Documents .....................................................535

Book VII: Database Tuning Overview..........................551
Chapter 1: Tuning the Database ...................................................................................553
Chapter 2: Tuning the Environment.............................................................................565
Chapter 3: Finding and Eliminating Bottlenecks ........................................................587

Book VIII: Appendixes...............................................619
Appendix A: SQL:2003 Reserved Words ......................................................................621

Appendix B: Glossary ....................................................................................................629

Index .......................................................................639


Table of Contents
Introduction ..................................................................1
About This Book...............................................................................................1
Foolish Assumptions .......................................................................................2
Conventions Used in This Book .....................................................................2
What You Don’t Have to Read ........................................................................3
How This Book Is Organized...........................................................................3
Book I: SQL Concepts.............................................................................3
Book II: Relational Database Development .........................................3
Book III: SQL Queries .............................................................................4
Book IV: Data Security ...........................................................................4
Book V: SQL and Programming.............................................................4
Book VI: SQL and XML ...........................................................................4
Book VII: Database Tuning Overview...................................................5
Book VIII: Appendixes............................................................................5
Icons Used in This Book..................................................................................5
Where to Go from Here....................................................................................6

Book I: SQL Concepts ....................................................7
Chapter 1: Relational Database Basics . . . . . . . . . . . . . . . . . . . . . . . . . . .9
Data Files and Databases ................................................................................9
Irreducible complexity.........................................................................10
The complex program/simple data organization ...................10
The simple program/complex data organization ...................12
Which type of organization is better?................................................12

Databases, Queries, and Database Applications .......................................13
Making data useful ...............................................................................14
Retrieving the data you want — and only the data you want ........14
Competing Database Models ........................................................................14
The hierarchical database model.......................................................15
The network database model .............................................................17
The relational database model ...........................................................20
What makes a database relational? ..........................................20
Codd’s Rules................................................................................20
Inherent flexibility ......................................................................23
The object-oriented database.............................................................23
The object-relational database ...........................................................23
Why Did the Relational Model Win? ............................................................24


xiv

SQL All-in-One Desk Reference For Dummies

Chapter 2: Modeling a System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27
Capturing the Users’ Data Model .................................................................27
Identifying and interviewing stakeholders........................................27
Reconciling conflicting requirements ................................................28
Obtaining stakeholder buy-in .............................................................29
Translating the Users’ Data Model to a Relational Model ........................29
Entity-Relationship modeling techniques .........................................29
Entities .........................................................................................30
Attributes.....................................................................................30
Identifiers.....................................................................................31
Relationships...............................................................................31

Drawing Entity-Relationship diagrams ..............................................34
Maximum cardinality .................................................................34
Minimum cardinality ..................................................................35
Advanced E-R model concepts ...........................................................37
Strong entities and weak entities..............................................37
ID-dependent entities .................................................................39
Supertype and subtype entities................................................39
Incorporating business rules ....................................................40
A simple example of an E-R model .....................................................41
A slightly more complex example ......................................................42
Problems with complex relationships ...............................................46
Simplifying relationships using normalization .................................47
Translating an E-R model into a relational model ............................47
The Relational Database Hierarchy .............................................................47

Chapter 3: SQL Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
Where SQL Came From..................................................................................49
What SQL Does ...............................................................................................50
The ANSI/ISO SQL Standard..........................................................................51
What SQL Does Not Do..................................................................................51
Available Implementations ...........................................................................52
Microsoft Access ..................................................................................52
Microsoft SQL Server ...........................................................................56
IBM DB2 .................................................................................................56
Oracle.....................................................................................................56
InterBase................................................................................................57
MySQL....................................................................................................57
PostgreSQL ............................................................................................57

Chapter 4: SQL and the Relational Model . . . . . . . . . . . . . . . . . . . . . . . .59

Sets, Relations, Multisets, and Tables .........................................................59
Functional Dependencies..............................................................................60
Keys .................................................................................................................61
Views................................................................................................................62


Table of Contents

xv

Users ................................................................................................................63
Privileges.........................................................................................................63
Schemas ..........................................................................................................64
Catalogs ...........................................................................................................64
Connections, Sessions, and Transactions...................................................64
Routines ..........................................................................................................65
Paths ................................................................................................................66

Chapter 5: The Major Components of SQL . . . . . . . . . . . . . . . . . . . . . . .67
The Data Definition Language (DDL) ...........................................................67
The containment hierarchy ................................................................67
Creating tables ......................................................................................68
Specifying columns ..............................................................................68
Specifying constraints ........................................................................69
Column constraints ....................................................................70
Table constraints ........................................................................71
Assertions....................................................................................71
Creating other objects .........................................................................72
Views ............................................................................................72
Schemas .......................................................................................77

Domains .......................................................................................77
Modifying tables and other objects ...................................................78
Removing tables and other objects ...................................................78
The Data Manipulation Language (DML) ....................................................78
Retrieving data from a database.........................................................79
Adding data to a table..........................................................................80
Adding data the dull and boring way (typing it in) ................80
Adding incomplete data.............................................................81
Adding data in the fastest and most efficient way:
Bypassing typing altogether..................................................81
Updating data in a table ......................................................................81
Deleting data from a table ...................................................................84
Updating views .....................................................................................85
The Data Control Language (DCL) ...............................................................86
Granting access privileges ..................................................................86
Revoking access privileges .................................................................87
Preserving database integrity with transactions ............................87

Chapter 6: SQL Characteristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
Executing SQL Statements ............................................................................89
Interactive SQL .....................................................................................90
Challenges to combining SQL with a host language ........................90
Embedded SQL .....................................................................................91
Module language ..................................................................................93
Using Reserved Words Correctly .................................................................94


xvi

SQL All-in-One Desk Reference For Dummies


SQL’s Data Types ............................................................................................94
Exact numerics .....................................................................................95
INTEGER.......................................................................................95
SMALLINT ....................................................................................96
BIGINT ..........................................................................................96
NUMERIC......................................................................................96
DECIMAL ......................................................................................96
BINARY LARGE OBJECT (BLOB)...............................................97
Approximate numerics ........................................................................97
REAL .............................................................................................97
DOUBLE PRECISION ...................................................................98
FLOAT...........................................................................................98
Character strings..................................................................................99
CHARACTER ................................................................................99
CHARACTER VARYING ...............................................................99
CHARACTER LARGE OBJECT (CLOB) ......................................99
NATIONAL CHARACTER, NATIONAL CHARACTER
VARYING, and NATIONAL CHARACTER
LARGE OBJECT......................................................................100
Booleans ..............................................................................................101
Datetimes.............................................................................................101
DATE ...........................................................................................101
TIME WITHOUT TIME ZONE....................................................101
TIME WITH TIME ZONE............................................................102
TIMESTAMP WITHOUT TIME ZONE .......................................102
TIMESTAMP WITH TIME ZONE ...............................................102
Intervals...............................................................................................103
XML type .............................................................................................103
Row types ............................................................................................104

Collection types..................................................................................105
ARRAY ........................................................................................105
Multiset ......................................................................................105
REF types.............................................................................................106
User-defined types .............................................................................106
Distinct types ............................................................................106
Structured types .......................................................................107
Data type summary ............................................................................109
Handling Null Values ....................................................................................110
Applying Constraints ...................................................................................111
Column constraints............................................................................111
NOT NULL..................................................................................111
UNIQUE ......................................................................................112
CHECK ........................................................................................112
Table constraints................................................................................113
Foreign key constraints .....................................................................114
Assertions............................................................................................115


Table of Contents

xvii

Book II: Relational Database Development ..................117
Chapter 1: System Development Overview . . . . . . . . . . . . . . . . . . . . . .119
The Components of a Database System....................................................119
The database.......................................................................................120
The database engine ..........................................................................120
The DBMS front end ...........................................................................120
The database application..................................................................120

The user...............................................................................................121
The System Development Life Cycle .........................................................121
Definition phase..................................................................................122
Requirements phase ..........................................................................123
Establishing requirements: An example ................................123
The users’ data model..............................................................124
Statement of Requirements .....................................................124
Evaluation phase ................................................................................125
Determining project scope ......................................................126
Reassessing feasibility .............................................................127
Documenting the evaluation phase........................................127
Design phase .......................................................................................127
Designing the database............................................................128
The database application ........................................................129
Documenting the design phase...............................................129
Implementation phase .......................................................................130
Final documentation and testing phase ..........................................130
Testing the system with sample data.....................................131
Finalizing the documentation..................................................132
Delivering the results (and celebrating)................................132
Maintenance phase ............................................................................132

Chapter 2: Building a Database Model . . . . . . . . . . . . . . . . . . . . . . . . .135
Finding and Listening to Interested Parties..............................................135
Your immediate supervisor...............................................................136
The users.............................................................................................136
The standards organization ..............................................................137
Upper management............................................................................137
Building Consensus......................................................................................138
What do people want, and how badly do they want it? ................138

Arriving at a consensus .....................................................................139
The three-option proposal ................................................................139
The Entity-Relationship Modeling Method ...............................................140
Entities .................................................................................................140
Attributes ............................................................................................141
Identifiers ............................................................................................141
Relationships ......................................................................................142


xviii

SQL All-in-One Desk Reference For Dummies

Maximum cardinality .........................................................................145
Minimum cardinality ..........................................................................145
Business rules.....................................................................................147
Building a Relational Model ........................................................................147
Different worlds, different terminology ...........................................147
The relational tradition......................................................................147
What exactly is a relation? ................................................................149
Functional dependencies ..................................................................149
Keys......................................................................................................150
Being Aware of the Danger of Anomalies ..................................................150
Eliminating anomalies........................................................................152
The higher normal forms ..................................................................155
The Database Integrity versus Performance Tradeoff ............................156

Chapter 3: Balancing Performance and Correctness . . . . . . . . . . . . .159
Designing a Sample Database .....................................................................160
The E-R model for Honest Abe’s ......................................................160

Converting an E-R model into a relational model ..........................161
Normalizing a relational model.........................................................162
Handling binary relationships ..........................................................164
A sample conversion..........................................................................168
Maintaining Integrity ...................................................................................170
Entity integrity ....................................................................................171
Domain integrity.................................................................................172
Referential integrity ...........................................................................172
Avoiding Data Corruption ...........................................................................174
Speeding Data Retrievals ............................................................................175
Hierarchical storage...........................................................................176
Full table scans ...................................................................................177
How indexes work ..............................................................................177
Creating the right indexes .................................................................177
Indexes and the ANSI/ISO Standard...........................................................178
Index costs ..........................................................................................178
Query type dictates the best index..................................................178
Point query................................................................................178
Multipoint query.......................................................................179
Range query ..............................................................................179
Prefix match query ...................................................................179
Extremal query..........................................................................179
Ordering query..........................................................................180
Grouping query.........................................................................180
Equi-join query..........................................................................180
Data structures used for indexes .....................................................180
B+ trees ......................................................................................181
Hash structures.........................................................................181
Indexes, sparse and dense ................................................................181
Index clustering ..................................................................................181

Composite indexes.............................................................................182


Table of Contents

xix

Index effect on join performance .....................................................183
Table size as an indexing consideration..........................................183
Indexes versus full-table scans.........................................................183
Reading SQL Server Execution Plans.........................................................183
Robust execution plans .....................................................................183
A sample database .............................................................................184
A typical query..........................................................................185
The execution plan ...................................................................185
Running the Database Engine Tuning Advisor......................185
Hot Tables and Load Balancing..................................................................187

Chapter 4: Creating a Database with SQL . . . . . . . . . . . . . . . . . . . . . . .189
First Things First: Planning Your Database...............................................189
Building Tables .............................................................................................190
Locating table rows with keys ..........................................................191
Using the CREATE TABLE statement................................................192
Setting Constraints.......................................................................................193
Column constraints............................................................................193
Table constraints................................................................................194
Keys and Indexes .........................................................................................194
Ensuring Data Validity with Domains ........................................................194
Establishing Relationships between Tables .............................................195
Altering Table Structure ..............................................................................198

Deleting Tables .............................................................................................199

Book III: SQL Queries ................................................201
Chapter 1: Values, Variables, Functions, and Expressions . . . . . . . .203
Entering Data Values....................................................................................203
Row values have multiple parts .......................................................203
Identifying values in a column ..........................................................204
Literal values don’t change ...............................................................204
Variables Vary...............................................................................................205
Special Variables Hold Specific Values ......................................................206
Working with Functions...............................................................................207
Summarizing data with set functions ..............................................207
COUNT .......................................................................................207
AVG .............................................................................................208
MAX ............................................................................................208
MIN .............................................................................................209
SUM.............................................................................................209
Dissecting data with value functions ...............................................209
String value functions ..............................................................209
Numeric value functions..........................................................212
Datetime value functions .........................................................216


xx

SQL All-in-One Desk Reference For Dummies

Using Expressions ........................................................................................217
Numeric value expressions ...............................................................217
String value expressions....................................................................218

Datetime value expressions ..............................................................218
Interval value expressions ................................................................219
Boolean value expressions................................................................219
Array value expressions ....................................................................220
Conditional value expressions..........................................................220
Handling different cases ..........................................................221
The NULLIF special CASE ........................................................222
Bypassing null values with COALESCE ..................................223
Converting data types with a CAST expression .............................223
Casting one SQL data type to another ...................................224
Using CAST to overcome data type incompatibilities
between SQL and its host language....................................224
Row value expressions ......................................................................225

Chapter 2: SELECT Statements and Modifying Clauses . . . . . . . . . . .227
Finding Needles in Haystacks with the SELECT Statement ....................227
Modifying Clauses ........................................................................................228
FROM clauses......................................................................................228
WHERE clauses ...................................................................................229
Comparison predicates............................................................230
BETWEEN...................................................................................231
IN and NOT IN ...........................................................................232
LIKE and NOT LIKE ...................................................................234
SIMILAR......................................................................................235
NULL...........................................................................................235
ALL, SOME, ANY .......................................................................236
EXISTS ........................................................................................239
UNIQUE ......................................................................................240
DISTINCT....................................................................................240
OVERLAPS .................................................................................241

MATCH .......................................................................................241
The MATCH predicate and referential integrity....................243
Logical connectives............................................................................245
AND.............................................................................................245
OR ...............................................................................................246
NOT ............................................................................................247
GROUP BY clauses .............................................................................247
HAVING clauses ..................................................................................249
ORDER BY clauses..............................................................................249
Tuning Queries .............................................................................................251
SELECT DISTINCT...............................................................................252
Temporary tables ...............................................................................255
The ORDER BY clause........................................................................259
The HAVING clause.............................................................................262
The OR logical connective ................................................................266


Table of Contents

xxi

Chapter 3: Querying Multiple Tables with Subqueries . . . . . . . . . . .269
What Is a Subquery? ....................................................................................269
What Subqueries Do ....................................................................................269
Subqueries that return multiple values...........................................270
Subqueries that retrieve rows satisfying a condition ..........270
Subqueries that retrieve rows that
don’t satisfy a condition.......................................................271
Subqueries that return a single value..............................................272
Quantified subqueries return a single value...................................275

Correlated subqueries .......................................................................277
Using a subquery as an existence test...................................278
Introducing a correlated subquery with the IN keyword ....279
Introducing a correlated subquery
with a comparison operator................................................279
Correlated subqueries in a HAVING clause ...........................281
Using Subqueries in INSERT, DELETE, and UPDATE Statements ...........282
Tuning Considerations for Statements Containing Nested Queries ......285
Tuning Correlated Subqueries....................................................................290

Chapter 4: Querying Multiple Tables with Relational Operators . . .297
UNION ............................................................................................................297
UNION ALL ..........................................................................................299
UNION CORRESPONDING ..................................................................300
INTERSECT....................................................................................................300
EXCEPT..........................................................................................................302
JOINS..............................................................................................................303
Cartesian product or cross join........................................................303
Equi-join...............................................................................................305
Natural join..........................................................................................307
Condition join .....................................................................................307
Column-name join ..............................................................................308
Inner join .............................................................................................309
Outer join ............................................................................................310
Left outer join............................................................................310
Right outer join .........................................................................312
Full outer join ............................................................................313
ON versus WHERE........................................................................................313
Join Conditions and Clustering Indexes....................................................314


Chapter 5: Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315
Declaring a Cursor .......................................................................................316
The query expression ........................................................................317
Ordering the query result set ...........................................................317
Updating table rows ...........................................................................319
Sensitive versus insensitive cursors................................................319
Scrolling a cursor ...............................................................................320


xxii

SQL All-in-One Desk Reference For Dummies

Opening a Cursor .........................................................................................320
Fetching Data from a Single Row................................................................322
FETCH syntax......................................................................................322
Absolute versus relative fetches ......................................................323
Deleting a row .....................................................................................323
Updating a row....................................................................................324
Closing a Cursor ...........................................................................................324

Book IV: Data Security ..............................................325
Chapter 1: Protecting Against Hardware
Failure and External Threats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
What Could Possibly Go Wrong?................................................................327
Equipment failure ...............................................................................328
Platform instability.............................................................................329
Database design flaws........................................................................330
Data entry error..................................................................................330
Operator error ....................................................................................331

Taking Advantage of RAID...........................................................................331
Striping.................................................................................................332
RAID levels ..........................................................................................332
RAID 0.........................................................................................332
RAID 1.........................................................................................334
RAID 5.........................................................................................334
RAID 10.......................................................................................334
RAID level comparison.............................................................335
Backing Up Your System .............................................................................335
Preparing for the worst .....................................................................335
Full or incremental backup ...............................................................336
Frequency............................................................................................336
Backup maintenance..........................................................................337
Coping with Internet Threats .....................................................................337
Viruses .................................................................................................337
Worms ..................................................................................................338
Denial-of-service attacks ...................................................................339
Phishing scams ...................................................................................339
Zombie spambots...............................................................................339
Installing Layers of Protection ...................................................................340
Network layer firewalls ......................................................................340
Application layer firewalls.................................................................340
Antivirus software ..............................................................................340
Vulnerabilities, exploits, and patches..............................................341
Education ............................................................................................341
Alertness..............................................................................................341


×