|
|
|
xix | |
| Foreword to the Fourth Edition |
|
xxi | |
| Preface |
|
xxiii | |
| Why New Editions? |
|
xxiii | |
| Changes for Recent Editions |
|
xxiv | |
| Include More Real-World Examples |
|
xxiv | |
| Emphasize SQL-92 |
|
xxiv | |
| Provide Software for Hands-On Practice |
|
xxv | |
| The Fourth Edition |
|
xxv | |
| Test on More Systems |
|
xxv | |
| Show Examples of Vendor-Specific Differences |
|
xxvi | |
| Acknowledgments |
|
xxvi | |
| Introduction |
|
xxvii | |
| The Beginnings of SQL |
|
xxvii | |
| The Commercialization of SQL |
|
xxviii | |
| Who Should Use This Book |
|
xxix | |
| The Focus of This Book |
|
xxix | |
| How to Learn SQL with This Book |
|
xxxi | |
| The Structure of This Book |
|
xxxii | |
| How to Use This Book |
|
xxxiii | |
| Save your Practice SQL Statements in Operating System Files |
|
xxxiii | |
| Save Your Successes |
|
xxxiii | |
| Structure an Application's Queries into Separate Modules or Subroutines |
|
xxxiii | |
| Make Yourself a Crib Sheet |
|
xxxiii | |
| Improve on Our Solutions |
|
xxxiii | |
| An Overview of the Book |
|
xxxiv | |
| SQL and Relational Database Management |
|
xxxiv | |
| Designing Databases |
|
xxxiv | |
| Creating and Filling a Database |
|
xxxiv | |
| Selecting Data from the Database |
|
xxxiv | |
| Sorting Data and Other Selection Techniques |
|
xxxiv | |
| Grouping Data and Reporting from It |
|
xxxv | |
| Joining Tables for Comprehensive Data Analysis |
|
xxxv | |
| Structuring Queries with Subqueries |
|
xxxv | |
| Creating and Using Views |
|
xxxv | |
| Security, Transactions, Performance, and Integrity |
|
xxxv | |
| Solving Business Problems |
|
xxxv | |
| Syntax Summary for the SQL Used in This Book |
|
xxxvi | |
| Industry SQL Equivalents |
|
xxxvi | |
| Glossary |
|
xxxvi | |
| The Bookbiz Sample Database |
|
xxxvi | |
| Resources |
|
xxxvi | |
|
SQL and Relational Database Management |
|
|
1 | (20) |
|
|
|
1 | (1) |
|
Relational Database Management |
|
|
1 | (2) |
|
The Relational Model: It's All Tables |
|
|
3 | (1) |
|
|
|
4 | (1) |
|
|
|
5 | (3) |
|
|
|
6 | (1) |
|
|
|
7 | (1) |
|
|
|
8 | (1) |
|
|
|
8 | (5) |
|
|
|
9 | (1) |
|
|
|
10 | (1) |
|
|
|
10 | (3) |
|
Alternatives for Viewing Data |
|
|
13 | (2) |
|
|
|
15 | (1) |
|
|
|
15 | (1) |
|
|
|
16 | (1) |
|
|
|
17 | (1) |
|
|
|
18 | (3) |
|
|
|
21 | (30) |
|
|
|
21 | (1) |
|
|
|
21 | (8) |
|
|
|
22 | (1) |
|
Data Definition and Customization |
|
|
23 | (1) |
|
How to Approach Database Design |
|
|
23 | (1) |
|
|
|
24 | (2) |
|
The Characteristics of a Good Design |
|
|
26 | (1) |
|
Introducing the Sample Database |
|
|
27 | (2) |
|
Data Entities and Relationship |
|
|
29 | (10) |
|
Entities: Things with an Independent Existence |
|
|
29 | (1) |
|
Diagramming Entities and Attributes |
|
|
30 | (1) |
|
|
|
31 | (2) |
|
|
|
33 | (1) |
|
Representing the Relationship |
|
|
33 | (2) |
|
|
|
35 | (1) |
|
Many-to-Many Relationships |
|
|
36 | (1) |
|
|
|
37 | (1) |
|
The Entity-Relationship Approach Summarized |
|
|
38 | (1) |
|
The Normalization Guidelines |
|
|
39 | (7) |
|
|
|
40 | (1) |
|
|
|
41 | (2) |
|
|
|
43 | (2) |
|
|
|
45 | (1) |
|
Reviewing the Database Design |
|
|
46 | (4) |
|
Summarizing the bookbiz Database |
|
|
48 | (1) |
|
Testing Your Database Design |
|
|
49 | (1) |
|
Other Database Definition Considerations |
|
|
50 | (1) |
|
|
|
50 | (1) |
|
Creating and Filling a Database |
|
|
51 | (44) |
|
|
|
51 | (1) |
|
From Theory to Practice: Installing bookbiz |
|
|
51 | (5) |
|
Distinguishings SQL Examples and Syntax |
|
|
52 | (3) |
|
|
|
55 | (1) |
|
|
|
56 | (2) |
|
|
|
56 | (1) |
|
|
|
57 | (1) |
|
Reserving Space for Database Objects |
|
|
57 | (1) |
|
|
|
58 | (1) |
|
|
|
58 | (10) |
|
|
|
61 | (2) |
|
Figuring Datatype Lengths |
|
|
63 | (1) |
|
Specifying Precision and Scale |
|
|
64 | (1) |
|
Assigning NULL and NOT NULL |
|
|
64 | (2) |
|
|
|
66 | (1) |
|
Defining the Tables in Bookbiz |
|
|
66 | (2) |
|
|
|
68 | (4) |
|
The Create Index Statement |
|
|
69 | (1) |
|
|
|
69 | (1) |
|
|
|
70 | (1) |
|
How, What, and Why to Index |
|
|
71 | (1) |
|
Creating Tables with SQL-92 Constraints |
|
|
72 | (7) |
|
|
|
73 | (2) |
|
Implementing Constraints on Individual Columns |
|
|
75 | (2) |
|
Implementing Multicolumn Constraints |
|
|
77 | (2) |
|
Changing and Deleting Databases and Objects |
|
|
79 | (4) |
|
|
|
79 | (1) |
|
Changing Table Definitions |
|
|
79 | (1) |
|
|
|
80 | (1) |
|
Creating Views and Tables |
|
|
81 | (1) |
|
|
|
81 | (1) |
|
|
|
82 | (1) |
|
|
|
82 | (1) |
|
Adding, Changing, and Removing Data |
|
|
83 | (11) |
|
|
|
84 | (1) |
|
Inserting Data into All Columns with Values |
|
|
84 | (1) |
|
Inserting Data into Some Columns with Values |
|
|
85 | (1) |
|
Inserting Data into All Columns with Select |
|
|
86 | (1) |
|
|
|
87 | (1) |
|
Inserting Data into Some Columns with Select |
|
|
88 | (1) |
|
Changing Existing Data: Update |
|
|
89 | (1) |
|
Specifying the Table: Update Clause |
|
|
89 | (1) |
|
Specifying Columns: Set Clause |
|
|
89 | (2) |
|
Specifying Rows: Where Clause |
|
|
91 | (2) |
|
|
|
93 | (1) |
|
|
|
94 | (1) |
|
Selecting Data from the Database |
|
|
95 | (48) |
|
|
|
95 | (1) |
|
Select Overview and Syntax |
|
|
95 | (5) |
|
|
|
96 | (1) |
|
Select list and Search condition Expressions |
|
|
96 | (1) |
|
Combining Select, From and Where |
|
|
97 | (2) |
|
|
|
99 | (1) |
|
Select Statement Clause Order |
|
|
99 | (1) |
|
|
|
99 | (1) |
|
Choosing Columns: The Select Clause |
|
|
100 | (14) |
|
Choosing All Columns: Select |
|
|
101 | (1) |
|
Choosing Specific Columns |
|
|
102 | (1) |
|
Rearranging Result Columns |
|
|
102 | (1) |
|
|
|
103 | (1) |
|
Display Label Conventions |
|
|
103 | (2) |
|
Display Label Limitations |
|
|
105 | (1) |
|
Character Strings in Query Results |
|
|
106 | (1) |
|
Combining Columns, Display Headings, and Text |
|
|
107 | (1) |
|
Computations with Constants |
|
|
107 | (2) |
|
Computed Column Display Headings |
|
|
109 | (1) |
|
Computations with Column Names |
|
|
110 | (1) |
|
Arithmetic Operator Precedence |
|
|
111 | (3) |
|
Specifying Tables: The From Clause Table List |
|
|
114 | (2) |
|
|
|
114 | (1) |
|
|
|
115 | (1) |
|
Selecting Rows: The Where Clause |
|
|
116 | (26) |
|
|
|
117 | (1) |
|
|
|
118 | (1) |
|
Comparing Character Values |
|
|
119 | (1) |
|
Comparing Imaginary Values |
|
|
119 | (1) |
|
Finding Values Not Equal to Some Value |
|
|
120 | (1) |
|
Connecting Conditions with Logical Operators |
|
|
120 | (1) |
|
|
|
121 | (1) |
|
|
|
121 | (1) |
|
Semantic Issues with OR and AND |
|
|
122 | (2) |
|
|
|
124 | (1) |
|
Logical Operator Precedence |
|
|
124 | (3) |
|
Ranges (Between and Not Between) |
|
|
127 | (1) |
|
|
|
128 | (2) |
|
|
|
130 | (1) |
|
|
|
131 | (2) |
|
|
|
133 | (1) |
|
IS NULL and Other Comparison Operators |
|
|
134 | (1) |
|
Matching Character Strings: LIKE |
|
|
134 | (1) |
|
|
|
135 | (2) |
|
|
|
137 | (1) |
|
|
|
138 | (1) |
|
|
|
139 | (1) |
|
|
|
139 | (1) |
|
|
|
139 | (1) |
|
|
|
140 | (1) |
|
|
|
141 | (1) |
|
|
|
142 | (1) |
|
Sorting Data and Other Selection Techniques |
|
|
143 | (32) |
|
|
|
143 | (1) |
|
A New Batch of Select Statement Clauses |
|
|
143 | (1) |
|
Sorting Query Results: Order By |
|
|
144 | (11) |
|
|
|
144 | (2) |
|
Character Sets and Sort Orders |
|
|
146 | (1) |
|
|
|
146 | (2) |
|
|
|
148 | (1) |
|
|
|
149 | (1) |
|
What About More Complexs Expressions? |
|
|
150 | (1) |
|
|
|
151 | (1) |
|
|
|
152 | (1) |
|
Sorting by Complex Expression |
|
|
153 | (1) |
|
|
|
154 | (1) |
|
Eliminating Duplicate Rows: Distinct and All |
|
|
155 | (7) |
|
|
|
157 | (1) |
|
Distinct with Multiple Select List Items |
|
|
158 | (2) |
|
|
|
160 | (1) |
|
|
|
160 | (1) |
|
Distinct and Non-Select-List Order By |
|
|
160 | (2) |
|
|
|
162 | (11) |
|
|
|
164 | (1) |
|
|
|
165 | (1) |
|
|
|
165 | (1) |
|
|
|
166 | (1) |
|
|
|
167 | (2) |
|
|
|
169 | (1) |
|
Null Values and the Aggregate Functions |
|
|
170 | (3) |
|
|
|
173 | (2) |
|
Grouping Data and Reporting from It |
|
|
175 | (30) |
|
|
|
175 | (1) |
|
|
|
175 | (1) |
|
|
|
175 | (17) |
|
|
|
176 | (1) |
|
|
|
177 | (1) |
|
Cautions and Restrictions |
|
|
178 | (1) |
|
Using Complex Expressions |
|
|
179 | (1) |
|
Multiple Summary Values for Multiple Levels of Grouping |
|
|
179 | (3) |
|
|
|
182 | (1) |
|
Count(*) and Count () |
|
|
183 | (1) |
|
Multiple Nulls in a Group |
|
|
184 | (2) |
|
Group By: Aggregate Interactions |
|
|
186 | (1) |
|
Group By Without Aggregates |
|
|
186 | (1) |
|
|
|
187 | (2) |
|
|
|
189 | (2) |
|
|
|
191 | (1) |
|
|
|
192 | (5) |
|
|
|
193 | (1) |
|
|
|
193 | (3) |
|
Multiple Having Conditions |
|
|
196 | (1) |
|
Where, Group By, Having, Order By |
|
|
197 | (1) |
|
|
|
197 | (6) |
|
Nulls and Database Design |
|
|
198 | (1) |
|
Comparisons Involving Nulls |
|
|
198 | (1) |
|
|
|
199 | (2) |
|
Defaults as Alternatives to Nulls |
|
|
201 | (1) |
|
Functions That Work with Nulls |
|
|
201 | (2) |
|
Null Functions and ``What-If'' Calculations |
|
|
203 | (1) |
|
|
|
203 | (2) |
|
Joining Tables for Comprehensive Data Analysis |
|
|
205 | (38) |
|
|
|
205 | (1) |
|
|
|
205 | (1) |
|
|
|
206 | (2) |
|
Associating Data from Separate Tables |
|
|
206 | (1) |
|
|
|
207 | (1) |
|
|
|
208 | (5) |
|
|
|
209 | (1) |
|
|
|
210 | (2) |
|
|
|
212 | (1) |
|
Improving the Readability of Join Results |
|
|
213 | (5) |
|
|
|
213 | (1) |
|
Limiting the Width of the Display |
|
|
214 | (2) |
|
Using Aliases in the From Clause Table/View List |
|
|
216 | (2) |
|
Specifying Join Conditions |
|
|
218 | (4) |
|
|
|
218 | (1) |
|
|
|
219 | (1) |
|
|
|
219 | (1) |
|
Joins Not Based on Equality |
|
|
220 | (1) |
|
Joining More Than Two Tables |
|
|
221 | (1) |
|
|
|
222 | (8) |
|
Joining a Table with Itself: The Self-Join |
|
|
222 | (1) |
|
|
|
223 | (1) |
|
Using Not-Equal Comparisons |
|
|
224 | (1) |
|
Showing the Background: Outer Joins |
|
|
225 | (1) |
|
|
|
225 | (2) |
|
|
|
227 | (1) |
|
|
|
228 | (1) |
|
Outer Joins and Other Conditions |
|
|
229 | (1) |
|
Avoiding a Common Source of Errors |
|
|
230 | (3) |
|
Understanding the Cartesian Product |
|
|
230 | (2) |
|
Using the Cartesian Product |
|
|
232 | (1) |
|
Constraining the Cartesian Product |
|
|
233 | (1) |
|
Going Beyond Joins: Union, Intersect, Minus |
|
|
233 | (8) |
|
|
|
234 | (1) |
|
|
|
235 | (3) |
|
|
|
238 | (1) |
|
|
|
239 | (2) |
|
|
|
241 | (2) |
|
Structuring Queries with Subqueries |
|
|
243 | (40) |
|
|
|
243 | (1) |
|
|
|
243 | (1) |
|
Simplified Subquery Syntax |
|
|
244 | (1) |
|
|
|
244 | (5) |
|
Simple Subquery Processing |
|
|
245 | (1) |
|
|
|
246 | (1) |
|
Correlated Subquery Processing |
|
|
247 | (1) |
|
|
|
248 | (1) |
|
Simple-Correlated Performance Issues |
|
|
249 | (1) |
|
|
|
249 | (3) |
|
|
|
249 | (2) |
|
|
|
251 | (1) |
|
Subqueries vs. Self-Joins? |
|
|
251 | (1) |
|
|
|
252 | (1) |
|
|
|
252 | (1) |
|
Subqueries Returning Zero or More Values |
|
|
253 | (13) |
|
Subqueries Introduced with In |
|
|
254 | (2) |
|
Subqueries Introduced with Not In |
|
|
256 | (1) |
|
Correlated Subqueries Introduced with In |
|
|
257 | (1) |
|
Correlated In Subqueries on a Single Table |
|
|
258 | (1) |
|
Correlated In Subqueries in a Having Clause |
|
|
259 | (1) |
|
Subqueries Introduced with Comparison Operators and Any or All |
|
|
259 | (1) |
|
Understanding All and Any |
|
|
260 | (1) |
|
|
|
260 | (2) |
|
|
|
262 | (1) |
|
Comparing In, Any, and All |
|
|
263 | (3) |
|
Subqueries Returning a Single Value |
|
|
266 | (4) |
|
Aggregate Functions Guarantee a Single Value |
|
|
267 | (1) |
|
Group By and Having Must Return a Single Value |
|
|
268 | (1) |
|
Correlated Subqueries with Comparison Operators |
|
|
269 | (1) |
|
Subqueries Testing Existence |
|
|
270 | (6) |
|
Not Exists Seeks the Empty Set |
|
|
273 | (1) |
|
Using Exists to Find Intersection and Difference |
|
|
274 | (1) |
|
|
|
275 | (1) |
|
Subqueries in Multiple Levels of Nesting |
|
|
276 | (1) |
|
Subqueries in Update, Delete, and Insert Statements |
|
|
277 | (1) |
|
Subqueries in From and Select Clauses |
|
|
278 | (4) |
|
Subqueries in the From Clause |
|
|
278 | (2) |
|
Subqueries in the Select Clause |
|
|
280 | (2) |
|
|
|
282 | (1) |
|
|
|
283 | (32) |
|
|
|
283 | (1) |
|
With a View Toward Flexibility |
|
|
283 | (1) |
|
|
|
284 | (3) |
|
|
|
284 | (1) |
|
Displaying Data Through Views |
|
|
285 | (2) |
|
|
|
287 | (1) |
|
|
|
287 | (7) |
|
Focus, Simplification, and Customization |
|
|
292 | (1) |
|
|
|
292 | (1) |
|
|
|
292 | (2) |
|
|
|
294 | (12) |
|
|
|
296 | (1) |
|
|
|
296 | (1) |
|
|
|
297 | (1) |
|
Creating Views with Multiple Underlying Objects |
|
|
298 | (1) |
|
Using Subqueries and Joins |
|
|
298 | (1) |
|
Deriving Views from Views |
|
|
299 | (1) |
|
|
|
300 | (1) |
|
|
|
300 | (1) |
|
|
|
301 | (2) |
|
|
|
303 | (3) |
|
Data Modification Through Views |
|
|
306 | (4) |
|
The Rules According to ANSI |
|
|
306 | (1) |
|
|
|
307 | (1) |
|
|
|
308 | (1) |
|
Multiple Underlying Objects |
|
|
309 | (1) |
|
|
|
310 | (2) |
|
|
|
312 | (3) |
|
Security, Transactions, Performance, and Integrity |
|
|
315 | (30) |
|
|
|
315 | (1) |
|
Database Management in the Real World |
|
|
315 | (1) |
|
|
|
316 | (10) |
|
User Identification and Special Users |
|
|
317 | (1) |
|
Creating Users and Groups |
|
|
318 | (1) |
|
|
|
319 | (1) |
|
|
|
319 | (1) |
|
|
|
320 | (1) |
|
The Grant and Revoke Commands |
|
|
320 | (1) |
|
|
|
320 | (2) |
|
Grant and Revoke Strategies |
|
|
322 | (2) |
|
Views as Security Mechanisms |
|
|
324 | (2) |
|
|
|
326 | (4) |
|
Transactions and Concurrency |
|
|
326 | (1) |
|
Transactions and Recovery |
|
|
327 | (1) |
|
User-Defined Transactions |
|
|
327 | (1) |
|
User-defined Transaction Syntax |
|
|
328 | (1) |
|
Preventing Data Modification Errors |
|
|
329 | (1) |
|
|
|
330 | (8) |
|
|
|
331 | (1) |
|
|
|
332 | (1) |
|
|
|
333 | (1) |
|
|
|
333 | (1) |
|
|
|
334 | (2) |
|
Other Tools for Monitoring and Boosting Performance |
|
|
336 | (1) |
|
|
|
336 | (1) |
|
Monitoring Execution Plans |
|
|
336 | (2) |
|
|
|
338 | (1) |
|
|
|
338 | (5) |
|
|
|
339 | (2) |
|
|
|
341 | (1) |
|
|
|
341 | (1) |
|
Stored Procedures and Triggers |
|
|
342 | (1) |
|
|
|
343 | (2) |
|
Solving Business Problems |
|
|
345 | (34) |
|
|
|
345 | (1) |
|
|
|
345 | (1) |
|
|
|
346 | (7) |
|
|
|
346 | (5) |
|
|
|
351 | (2) |
|
Formatting and Displaying Data |
|
|
353 | (8) |
|
Displaying One Columns as Two |
|
|
353 | (3) |
|
Displaying Two Columns as One |
|
|
356 | (2) |
|
Converting from One Datatype to Another |
|
|
358 | (3) |
|
|
|
361 | (9) |
|
Matching Uppercase and Lowercase Letters |
|
|
361 | (2) |
|
Finding Data Within a Range When You Don't Know the Values |
|
|
363 | (2) |
|
|
|
365 | (4) |
|
Displaying Data by Time Units |
|
|
369 | (1) |
|
|
|
370 | (8) |
|
|
|
371 | (1) |
|
Distinct with Columns and Expressions |
|
|
372 | (1) |
|
|
|
373 | (2) |
|
|
|
375 | (1) |
|
|
|
375 | (2) |
|
Finding the ``First'' Entry |
|
|
377 | (1) |
|
|
|
378 | (1) |
| Appendix A: Syntax Summary for the SQL Used in This Book |
|
379 | (2) |
|
|
|
379 | (1) |
|
|
|
379 | (1) |
|
|
|
380 | (1) |
|
|
|
380 | (1) |
| Appendix B: Industry SQL Equivalents |
|
381 | (6) |
|
|
|
381 | (1) |
|
|
|
381 | (1) |
|
Naming Convention Comparison |
|
|
381 | (1) |
|
|
|
382 | (2) |
|
|
|
384 | (3) |
|
|
|
384 | (1) |
|
|
|
385 | (1) |
|
|
|
385 | (2) |
| Appendix C: Glossary |
|
387 | (16) |
| Appendix D: The bookbiz Sample Database |
|
403 | (48) |
|
|
|
403 | (1) |
|
|
|
403 | (1) |
|
|
|
403 | (11) |
|
|
|
403 | (1) |
|
|
|
404 | (1) |
|
|
|
405 | (3) |
|
|
|
408 | (1) |
|
|
|
409 | (1) |
|
|
|
410 | (1) |
|
|
|
411 | (1) |
|
|
|
412 | (1) |
|
|
|
413 | (1) |
|
Create Statements for the bookbiz Database |
|
|
414 | (13) |
|
Adaptive Server Anywhere Creates |
|
|
415 | (3) |
|
|
|
418 | (4) |
|
|
|
422 | (2) |
|
|
|
424 | (3) |
|
|
|
427 | (19) |
|
|
|
446 | (5) |
| Appendix E: Resources |
|
451 | (4) |
|
|
|
451 | (1) |
|
|
|
451 | (3) |
|
|
|
451 | (1) |
|
|
|
452 | (1) |
|
|
|
452 | (1) |
|
|
|
452 | (1) |
|
|
|
453 | (1) |
|
|
|
453 | (1) |
|
|
|
453 | (1) |
|
|
|
454 | (1) |
|
|
|
454 | (1) |
|
|
|
454 | (1) |
|
|
|
454 | (1) |
| Index |
|
455 | |