SQL For Dummies®
To view this book's Cheat Sheet, simply go to
www.dummies.com
and search for “SQL For Dummies Cheat Sheet” in the Search box.
Table of Contents
Cover
Introduction
About This Book
Foolish Assumptions
Icons Used in This Book
Beyond the Book
Where to Go from Here
Part 1: Getting Started with SQL
Chapter 1: Relational Database Fundamentals
Keeping Track of Things
What Is a Database?
Database Size and Complexity
What Is a Database Management System?
Flat Files
Database Models
Database Design Considerations
Chapter 2: SQL Fundamentals
What SQL Is and Isn’t
A (Very) Little History
SQL Statements
Reserved Words
Data Types
Null Values
Constraints
Using SQL in a Client/Server System
Using SQL on the Internet or an Intranet
Chapter 3: The Components of SQL
Data Definition Language
Data Manipulation Language
Data Control Language
Part 2: Using SQL to Build Databases
Chapter 4: Building and Maintaining a Simple Database Structure
Using a RAD Tool to Build a Simple Database
Building POWER with SQL’s DDL
Portability Considerations
Chapter 5: Building a Multi-table Relational Database
Designing a Database
Working with Indexes
Maintaining Data Integrity
Normalizing the Database
Part 3: Storing and Retrieving Data
Chapter 6: Manipulating Database Data
Retrieving Data
Creating Views
Updating Views
Adding New Data
Updating Existing Data
Transferring Data
Deleting Obsolete Data
Chapter 7: Handling Temporal Data
Understanding Times and Periods
Working with Application-Time Period Tables
Working with System-Versioned Tables
Tracking Even More Time Data with Bitemporal Tables
Formatting and Parsing Dates and Times
Chapter 8: Specifying Values
Values
Value Expressions
Functions
Chapter 9: Using Advanced SQL Value Expressions
CASE Conditional Expressions
CAST Data-Type Conversions
Row Value Expressions
Chapter 10: Zeroing In on the Data You Want
Modifying Clauses
FROM Clauses
WHERE Clauses
Logical Connectives
GROUP BY Clauses
HAVING Clauses
ORDER BY Clauses
Limited FETCH
Peering through a Window to Create a Result Set
Chapter 11: Using Relational Operators
UNION
INTERSECT
EXCEPT
Join Operators
ON versus WHERE
Chapter 12: Delving Deep with Nested Queries
What Subqueries Do
Chapter 13: Recursive Queries
What Is Recursion?
What Is a Recursive Query?
Where Might You Use a Recursive Query?
Where Else Might You Use a Recursive Query?
Part 4: Controlling Operations
Chapter 14: Providing Database Security
The SQL Data Control Language
User Access Levels
Granting Privileges to Users
Granting Privileges across Levels
Granting the Power to Grant Privileges
Taking Privileges Away
Using GRANT and REVOKE Together to Save Time and Effort
Chapter 15: Protecting Data
Threats to Data Integrity
Reducing Vulnerability to Data Corruption
Constraints Within Transactions
Avoiding SQL Injection Attacks
Chapter 16: Using SQL within Applications
SQL in an Application
Hooking SQL into Procedural Languages
Part 5: Taking SQL to the Real World
Chapter 17: Accessing Data with ODBC and JDBC
ODBC
ODBC in a Client/Server Environment
ODBC and the Internet
ODBC and an Intranet
JDBC
Chapter 18: Operating on XML Data with SQL
How XML Relates to SQL
The XML Data Type
Mapping SQL to XML and XML to SQL
SQL Functions That Operate on XML Data
Predicates
Transforming XML Data into SQL Tables
Mapping Non-Predefined Data Types to XML
The Marriage of SQL and XML
Chapter 19: SQL and JSON
Using JSON with SQL
The SQL/JSON Data Model
SQL/JSON Functions
SQL/JSON Path Language
There's More
Part 6: Advanced Topics
Chapter 20: Stepping through a Dataset with Cursors
Declaring a Cursor
Opening a Cursor
Fetching Data from a Single Row
Closing a Cursor
Chapter 21: Adding Procedural Capabilities with Persistent Stored Modules
Compound Statements
Flow of Control Statements
Stored Procedures
Stored Functions
Privileges
Stored Modules
Chapter 22: Handling Errors
SQLSTATE
WHENEVER Clause
Diagnostics Areas
Handling Exceptions
Chapter 23: Triggers
Examining Some Applications of Triggers
Creating a Trigger
Firing a Succession of Triggers
Referencing Old Values and New Values
Firing Multiple Triggers on a Single Table
Part 7: The Parts of Tens
Chapter 24: Ten Common Mistakes
Assuming That Your Clients Know What They Need
Ignoring Project Scope
Considering Only Technical Factors
Not Asking for Client Feedback
Always Using Your Favorite Development Environment
Using Your Favorite System Architecture Exclusively
Designing Database Tables in Isolation
Neglecting Design Reviews
Skipping Beta Testing
Not Documenting Your Process
Chapter 25: Ten Retrieval Tips
Verify the Database Structure
Try Queries on a Test Database
Double-Check Queries That Include Joins
Triple-Check Queries with Subselects
Summarize Data with GROUP BY
Watch GROUP BY Clause Restrictions
Use Parentheses with AND, OR, and NOT
Control Retrieval Privileges
Back Up Your Databases Regularly
Handle Error Conditions Gracefully
Appendix: ISO/IEC SQL: 2016 Reserved Words
Index
About the Author
Connect with Dummies
End User License Agreement
Guide
Cover
Table of Contents
Begin Reading
Pages
i
ii
1
2
3
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
365
366
367
368
369
370
371
372
373
374
375
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496