SPECIAL OFFERS
Keep up with new releases and promotions.Sign up to hear from us.
Register your productto gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from yourAccountpage after purchase:
EPUBThe open industry format known for its reflowable content and usability on supported mobile devices.
PDFThe popular standard, used most often with the freeAdobe® Reader®software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
This eBook includes the following formats, accessible from yourAccountpage after purchase:
EPUBThe open industry format known for its reflowable content and usability on supported mobile devices.
PDFThe popular standard, used most often with the freeAdobe® Reader®software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Whether you're an application developer, database administrator, web application designer, mobile app developer, or Microsoft Office users, a good working knowledge of SQL is an important part of interacting with databases. And地空导弹自学SQL in 10 Minutesoffers the straightforward, practical answers you need to help you do your job.
Expert trainer and popular author Ben Forta teaches you just the parts of SQL you need to know–starting with simple data retrieval and quickly going on to more complex topics including the use of joins, subqueries, stored procedures, cursors, triggers, and table constraints.
You'll learn methodically, systematically, and simply–in short, quick lessons that will each take only 10 minutes or less to complete.
Introduction 1
Who Is the Teach Yourself SQL Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . 1
DBMSs Covered in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Conventions Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1 Understanding SQL 5
Database Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
What Is SQL? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Try It Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2 Retrieving Data 13
The SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Retrieving Individual Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Retrieving Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Retrieving All Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Retrieving Distinct Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Limiting Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Using Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 Sorting Retrieved Data 25
Sorting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Sorting by Multiple Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Sorting by Column Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Specifying Sort Direction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4 Filtering Data 33
Using the WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
The WHERE Clause Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
5 Advanced Data Filtering 41
Combining WHERE Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Using the IN Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Using the NOT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6 Using Wildcard Filtering 51
Using the LIKE Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Tips for Using Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
7 Creating Calculated Fields 59
理解Calculated Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Concatenating Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Performing Mathematical Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
8 Using Data Manipulation Functions 69
理解Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9 Summarizing Data 79
Using Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Aggregates on Distinct Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Combining Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
10 Grouping Data 89
理解Data Grouping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Creating Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Filtering Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Grouping and Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
SELECT Clause Ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
11 Working with Subqueries 99
理解Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Filtering by Subquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Using Subqueries as Calculated Fields . . . . . . . . . . . . . . . . . . . . . . . . . . 103
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 105
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
12 Joining Tables 107
理解Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
创建一个连接 . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 109
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 115
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
13 Creating Advanced Joins 117
Using Table Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Using Different Join Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Using Joins with Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Using Joins and Join Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 125
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
14 Combining Queries 127
理解Combined Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Creating Combined Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 133
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
15 Inserting Data 135
理解Data Insertion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Copying from One Table to Another . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 142
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
16 Updating and Deleting Data 145
Updating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Guidelines for Updating and Deleting Data . . . . . . . . . . . . . . . . . . . . . . . 149
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 149
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
17 Creating and Manipulating Tables 151
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Updating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Deleting Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Renaming Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 158
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
18 Using Views 161
理解Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 169
Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
19 Working with Stored Procedures 171
理解Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
理解为什么使用存储过程。. . . . . . . . . . . . . . . . . 172
Executing Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Creating Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 178
20 Managing Transaction Processing 179
理解Transaction Processing . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Controlling Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 185
21 Using Cursors 187
理解Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Working with Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 192
22 Understanding Advanced SQL Features 193
理解Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
理解Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
理解Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Database Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
总结 . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . 202
A Sample Table Scripts 203
B SQL Statement Syntax 209
C Using SQL Datatypes 215
D SQL Reserved Words 221
TOC, 9780135182796, 10/9/19
We've made every effort to ensure the accuracy of this book and its companion content. Any errors that have been confirmed since this book was published can be downloaded below.
Download the errata(19 KB .docx)