Microsoft SQL Server - Pearsoncmg

Transcription

Microsoft SQL Server2012 T-SQL Fundamentals Itzik Ben-Gan

Copyright 2012 by Itzik Ben-GanAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by anymeans without the written permission of the publisher.ISBN: 978-0-735-65814-1Fifth Printing: March 2015Printed and bound in the United States of America.Microsoft Press books are available through booksellers and distributors worldwide. If you need support relatedto this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think ofthis book at soft and the trademarks listed at ctualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property oftheir respective owners.The example companies, organizations, products, domain names, email addresses, logos, people, places, andevents depicted herein are fictitious. No association with any real company, organization, product, domain name,email address, logo, person, place, or event is intended or should be inferred.This book expresses the author’s views and opinions. The information contained in this book is provided withoutany express, statutory, or implied warranties. Neither the author, Microsoft Corporation, nor its resellers, ordistributors will be held liable for any damages caused or alleged to be caused either directly or indirectly bythis book.Acquisitions and Developmental Editor: Russell JonesProduction Editor: Kristen BorgEditorial Production and Illustration: Online Training Solutions, Inc.Technical Reviewer: Gianluca Hotz and Herbert AlbertCopyeditor: Kathy KrauseIndexer: Allegro Technical IndexingCover Design: Twist Creative SeattleCover Composition: Karen Montgomery

To DatoTo live in hearts we leave behind,Is not to die.—Thomas Campbell

This page intentionally left blank

Contents at a GlanceForewordxixIntroductionxxiChapter 1Background to T-SQL Querying and ProgrammingChapter 2Single-Table Queries1Chapter 3JoinsChapter 4Subqueries129Chapter 5Table Expressions157Chapter 6Set Operators191Chapter 7Beyond the Fundamentals of Querying211Chapter 8Data Modification247Chapter 9Transactions and Concurrency297Chapter 10Programmable Objects339Appendix AGetting Started375Index397About the Author4132799

This page intentionally left blank

ContentsForeword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiChapter 1Background to T-SQL Querying and Programming1Theoretical Background. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1SQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Set Theory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Predicate Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The Data Life Cycle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9SQL Server Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12The ABC Flavors of SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12SQL Server Instances. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Schemas and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Creating Tables and Defining Data Integrity. . . . . . . . . . . . . . . . . . . . . . . . . 19Creating Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Defining Data Integrity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Chapter 2Single-Table Queries27Elements of the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27The FROM Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29The WHERE Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31The GROUP BY Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can continually improve ourbooks and learning resources for you. To participate in a brief online survey, please visit:microsoft.com/learning/booksurveyvii

The HAVING Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36The SELECT Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36The ORDER BY Clause. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42The TOP and OFFSET-FETCH Filters. . . . . . . . . . . . . . . . . . . . . . . . . . . 44A Quick Look at Window Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . 48Predicates and Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50CASE Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53NULL Marks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55All-at-Once Operations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Working with Character Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Collation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Operators and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64The LIKE Predicate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71Working with Date and Time Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Date and Time Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73Literals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Working with Date and Time Separately. . . . . . . . . . . . . . . . . . . . . . . 78Filtering Date Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Date and Time Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Querying Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Catalog Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Information Schema Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89System Stored Procedures and Functions. . . . . . . . . . . . . . . . . . . . . . 89Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 911 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 923 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 924 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 936 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 937 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94viiiContents8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 951 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 952 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 953 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 965 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 976 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 977 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 988 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Chapter 3Joins99Cross Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99ANSI SQL-92 Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100ANSI SQL-89 Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Self Cross Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101Producing Tables of Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102Inner Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103ANSI SQL-92 Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103ANSI SQL-89 Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105Inner Join Safety. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105More Join Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Composite Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Non-Equi Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Multi-Join Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109Outer Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Fundamentals of Outer Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Beyond the Fundamentals of Outer Joins. . . . . . . . . . . . . . . . . . . . . 113Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1201-1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1201-2 (Optional, Advanced). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1212 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1223 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1234 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Contentsix

5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1236 (Optional, Advanced). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1247 (Optional, Advanced). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Solutions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1251-1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1251-2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1262 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1263 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1274 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1275 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1276 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1287 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Chapter 4Subqueries129Self-Contained Subqueries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Self-Contained Scalar Subquery Examples. . . . . . . . . . . . . . . . . . . . 130Self-Contained Multivalued Subquery Examples. . . . . . . . . . . . . . . 132Correlated Subqueries. . . . . .

SQL Server teaching, mentoring, and consulting experience to write books on advanced programming subjects, leaving a significant gap not only for the novice and less ex-perienced users but also for the many experts working with SQL Server in roles where T-SQL programming is not a high priority. When it comes to T-SQL, Itzik is one of the most knowledgeable people in the world. In fact, we .