ptg ptg 800 East 96th Street, Indianapolis, Indiana, 46240 USA Ryan Stephens Ron Plew Arie D. Jones Sams Teach Yourself 24 in Hours SQL FIFTH EDITION Down from [www.wowebook.com] ptg Sams Teach Yourself SQL in 24 Hours, Fifth Edition Copyright © 2011 by Pearson Education, Inc. All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. ISBN-13: 978-0-672-33541-9 ISBN-10: 0-672-33541-7 The Library of Congress cataloging-in-publication data is on file. Printed in the United States of America First Printing May 2011 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the programs accompanying it. Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk pur- chases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearson.com Associate Publisher Mark Taub Acquisitions Editor Trina MacDonald Development Editor Michael Thurston Managing Editor Kristy Hart Project Editor Jovana San Nicolas- Shirley Copy Editor The Wordsmithery LLC Indexer Lisa Stumpf Proofreader Gill Editorial Services Technical Editor Benjamin Schupak Publishing Coordinator Olivia Basegio Book Designer Gary Adair Composition Gloria Schurick Down from [www.wowebook.com] ptg Contents at a Glance Part I: An SQL Concepts Overview HOUR 1 Welcome to the World of SQL . 1 Part II: Building Your Database HOUR 2 Defining Data Structures . 21 3 Managing Database Objects . 37 4 The Normalization Process . 61 5 Manipulating Data . 73 6 Managing Database Transactions. 87 Part III: Getting Effective Results from Queries HOUR 7 Introduction to the Database Query 99 8 Using Operators to Categorize Data. 115 9 Summarizing Data Results from a Query . 141 10 Sorting and Grouping Data. 153 11 Restructuring the Appearance of Data . 169 12 Understanding Dates and Times . 191 Part IV: Building Sophisticated Database Queries HOUR 13 Joining Tables in Queries . 207 14 Using Subqueries to Define Unknown Data. 225 15 Combining Multiple Queries into One . 239 Part V: SQL Performance Tuning HOUR 16 Using Indexes to Improve Performance . 255 17 Improving Database Performance 267 Down from [www.wowebook.com] ptg Part VI: Using SQL to Manage Users and Security HOUR 18 Managing Database Users 285 19 Managing Database Security. 299 Part VII: Summarized Data Structures HOUR 20 Creating and Using Views and Synonyms 313 21 Working with the System Catalog 329 Part VIII: Applying SQL Fundamentals in Today’s World HOUR 22 Advanced SQL Topics . 339 23 Extending SQL to the Enterprise, the Internet, and the Intranet. 355 24 Extensions to Standard SQL. 367 Part IX: Appendixes A Common SQL Commands . 377 B Using the Databases for Exercises. 383 C Answers to Quizzes and Exercises . 391 D CREATE TABLE Statements for Book Examples. 439 E INSERT Statements for Data in Book Examples. 443 F Glossary. 451 G Bonus Exercises . 455 Index . 461 iv Teach Yourself SQL in 24 Hours Down from [www.wowebook.com] ptg Table of Contents Part I: An SQL Concepts Overview HOUR 1: Welcome to the World of SQL 1 SQL Definition and History . 1 SQL Sessions . 8 Types of SQL Commands . 9 The Database Used in This Book . 12 Summary 17 Q&A. 17 Workshop 18 Part II: Building Your Database HOUR 2: Defining Data Structures 21 What Is Data?. 21 Basic Data Types . 22 Summary . 31 Q&A. 31 Workshop . 32 HOUR 3: Managing Database Objects 37 What Are Database Objects? . 37 What Is a Schema? . 37 Tables: The Primar y Storage for Data . 39 Integrity Constraints . 49 Summary . 54 Q&A. 55 Workshop . 55 Down from [www.wowebook.com] ptg HOUR 4: The Normalization Process 61 Normalizing a Database . 61 Denormalizing a Database . 69 Summary . 70 Q&A. 70 Workshop . 71 HOUR 5: Manipulating Data 73 Overview of Data Manipulation. 73 Populating Tables with New Data. 74 Updating Existing Data . 80 Deleting Data from Tables. 82 Summary . 83 Q&A. 83 Workshop . 84 HOUR 6: Managing Database Transactions 87 What Is a Transaction?. 87 Controlling Transactions. 88 Transactional Control and Database Performance . 95 Summary . 96 Q&A. 96 Workshop . 97 Part III: Getting Effective Results from Queries HOUR 7: Introduction to the Database Query 99 What Is a Query? . 99 Introduction to the SELECT Statement . 99 Examples of Simple Queries . 108 Summary . 112 Q&A 112 Workshop . 113 vi Teach Yourself SQL in 24 Hours Down from [www.wowebook.com] ptg HOUR 8: Using Operators to Categorize Data 115 What Is an Operator in SQL? . 115 Comparison Operators . 116 Logical Operators. 119 Conjunctive Operators . 126 Negative Operators . 129 Arithmetic Operators . 133 Summary . 136 Q&A . 137 Workshop . 137 HOUR 9: Summarizing Data Results from a Query 141 What Are Aggregate Functions? . 141 Summary . 150 Q&A . 150 Workshop . 150 HOUR 10: Sorting and Grouping Data 153 Why Group Data? . 153 The GROUP BY Clause . 154 GROUP BY Versus ORDER BY . 159 CUBE and ROLLUP Expressions . 161 The HAVING Clause . 164 Summary . 165 Q&A . 166 Workshop . 166 HOUR 11: Restructuring the Appearance of Data 169 ANSI Character Functions . 169 Common Character Functions . 170 Miscellaneous Character Functions . 179 Mathematical Functions . 183 Conversion Functions . 183 Contents vii Down from [www.wowebook.com] ptg Combining Character Functions . 186 Summary . 187 Q&A . 188 Workshop . 188 HOUR 12: Understanding Dates and Times 191 How Is a Date Stored? . 191 Date Functions . 193 Date Conversions . 198 Summary . 204 Q&A . 204 Workshop . 205 Part IV: Building Sophisticated Database Queries HOUR 13: Joining Tables in Queries 207 Selecting Data from Multiple Tables . 207 Understanding Joins . 208 Join Considerations . 217 Summary . 221 Q&A . 222 Workshop . 222 HOUR 14: Using Subqueries to Define Unknown Data 225 What Is a Subquery? . 225 Embedded Subqueries . 231 Correlated Subqueries . 233 Subquery Performance . 234 Summary . 235 Q&A . 235 Workshop . 236 viii Teach Yourself SQL in 24 Hours Down from [www.wowebook.com] ptg HOUR 15: Combining Multiple Queries into One 239 Single Queries Versus Compound Queries. 239 Compound Query Operators . 240 Using ORDER BY with a Compound Query. 246 Using GROUP BY with a Compound Query . 248 Retrieving Accurate Data. 250 Summary . 250 Q&A . 250 Workshop . 251 Part V: SQL Performance Tuning HOUR 16: Using Indexes to Improve Performance 255 What Is an Index? . 255 How Do Indexes Work? . 256 The CREATE INDEX Command. 257 Types of Indexes . 258 When Should Indexes Be Considered? . 260 When Should Indexes Be Avoided? . 261 Altering an Index. 263 Dropping an Index. 263 Summary . 264 Q&A . 264 Workshop . 265 HOUR 17: Improving Database Performance 267 What Is SQL Statement Tuning? . 267 Database Tuning Versus SQL Statement Tuning . 268 Formatting Your SQL Statement . 268 Full Table Scans . 274 Other Performance Considerations . 275 Cost-Based Optimization . 279 Performance Tools . 280 Summary . 280 Contents ix Down from [www.wowebook.com] [...]... Part 1: SQL/ Framework—Specifies the general requirements for conformance and defines the fundamental concepts of SQL Part 2: SQL/ Foundation—Defines the syntax and operations of SQL Part 3: SQL/ Call-Level Interface—Defines the interface for appli- cation programming to SQL Part 4: SQL/ Persistent Stored Modules—Defines the control struc- tures that then define SQL routines Part 4 also defines the... modules that contain SQL routines Part 9: Management of External Data (SQL/ MED)—Defines extensions to SQL to support the management of external data through the use of data-wrappers and datalink types Part 10: Object Language Bindings—Defines extensions to the SQL language to support the embedding of SQL statements into programs written in Java Part 11: Information and Definition Schemas—Defines specifica-... 351 Working with XML 352 Summary 353 Q&A 353 Workshop 354 HOUR 23: Extending SQL to the Enterprise, the Internet, and the Intranet SQL and the Enterprise Accessing a Remote Database 355 355 357 Down from [www.wowebook.com] xii Teach Yourself SQL in 24 Hours SQL and the Internet 360 SQL and... tions for the Information Schema and Definition Schema, which provide structural and security information related to SQL data Part 13: Routines and Types Using the Java Programming Language—Defines the capability to call Java static routines and classes as SQL- invoked routines Part 14: XML-Related Specifications—Defines ways in which SQL can be used with XML The new ANSI standard (SQL- 2008) has two... 560 RR 1 IN 48990 3174346758 INDIANAPOLIS IN 4 6244 3172719991 WHITELAND 590 HEATHERS FEATHERS AND THINGS 43278 3175456768 ANDYS CANDIES LEBANON INDIANAPOLIS IN 46234 IN 49980 3178879023 4090 N SHADELAND AVE INDIANAPOLIS IN PLAINFIELD BOX 34 IN 46818 3178393441 NASHVILLE IN 48756 8123239871 221 RYANS STUFF 3175634402 2337 S SHELBY ST INDIANAPOLIS IN 47834 175 CAMERON’S PIES 178 N TIBBS AVON IN 46234... Standards Institute (ANSI) is an organization that approves certain standards in many different industries SQL has been deemed the standard language in relational database communication, originally approved in 1986 based on IBM’s implementation In 1987, the ANSI SQL standard was accepted as the international standard by the International Standards Organization (ISO) The standard was revised again in 1992 (SQL- 92)... server model Client Machine Client Machine Network Client Machine Server Client Machine Server Server Client Machine Client Machine Web-Based Database Systems Business information systems are moving toward web integration Databases are now accessible through the Internet, meaning that customers’ access to an organization’s information is enabled through an Internet browser such as Internet Explorer or... you are using to access the database is normally closed DISCONNECT Types of SQL Commands The following sections discuss the basic categories of commands used in SQL to perform various functions These functions include building database objects, manipulating objects, populating database tables with data, updating existing data in tables, deleting data, performing database queries, controlling database... discussed during the following hours include CREATE TABLE ALTER TABLE DROP TABLE CREATE INDEX ALTER INDEX DROP INDEX Down from [www.wowebook.com] 10 HOUR 1: Welcome to the World of SQL CREATE VIEW DROP VIEW These commands are discussed in detail during Hour 3, “Managing Database Objects,” Hour 17, “Improving Database Performance,” and Hour 20, “Creating and Using Views and Synonyms.” Manipulating Data... Table-Naming Standards By the Way Naming Standards You should not only adhere to the object-naming syntax of any SQL implementation, but also follow local business rules and create names that are descriptive and related to the data groupings for the business Table-naming standards, as well as any standard within a business, are critical to maintaining control After studying the tables and data in the . Street, Indianapolis, Indiana, 4 6240 USA Ryan Stephens Ron Plew Arie D. Jones Sams Teach Yourself 24 in Hours SQL FIFTH EDITION Down from [www.wowebook.com] ptg Sams Teach Yourself SQL in 24 Hours, . 236 viii Teach Yourself SQL in 24 Hours Down from [www.wowebook.com] ptg HOUR 15: Combining Multiple Queries into One 239 Single Queries Versus Compound Queries. 239 Compound Query Operators . 240 Using. Databases for Exercises 383 Windows Installation Instructions for MySQL . 383 Windows Installation Instructions for Oracle . 386 Windows Installation Instructions for Microsoft SQL Server . 388 APPENDIX