Acknowledgments |
|
xvii | |
Introduction |
|
xix | |
Part I Relational Databases and SQL |
|
|
1 Introduction to Relational Databases and SQL |
|
|
3 | (26) |
|
Understand Relational Databases |
|
|
4 | (10) |
|
|
5 | (9) |
|
Try This 1-1: Normalizing Data and Identifying Relationships |
|
|
14 | (1) |
|
|
15 | (9) |
|
|
16 | (3) |
|
|
19 | (1) |
|
|
20 | (1) |
|
SQL Standard versus Product Implementations |
|
|
21 | (3) |
|
Try This 1-2: Connecting to a Database |
|
|
24 | (3) |
|
|
27 | (2) |
|
2 Working with the SQL Environment |
|
|
29 | (20) |
|
Understand the SQL Environment |
|
|
30 | (1) |
|
|
31 | (9) |
|
|
34 | (1) |
|
|
35 | (2) |
|
|
37 | (3) |
|
Name Objects in an SQL Environment |
|
|
40 | (2) |
|
|
41 | (1) |
|
|
42 | (2) |
|
|
44 | (1) |
|
Try This 2-1: Creating a Database and a Schema |
|
|
45 | (1) |
|
|
46 | (3) |
|
3 Creating and Altering Tables |
|
|
49 | (28) |
|
|
50 | (5) |
|
Specify Column Data Types |
|
|
55 | (9) |
|
|
56 | (1) |
|
|
56 | (2) |
|
|
58 | (2) |
|
|
60 | (2) |
|
|
62 | (1) |
|
|
62 | (2) |
|
Create User-Defined Types |
|
|
64 | (1) |
|
Specify Column Default Values |
|
|
65 | (1) |
|
Specify Identity Column and Generation Options |
|
|
66 | (3) |
|
Maintaining Independent Sequence Generators |
|
|
68 | (1) |
|
Try This 3-1: Creating SQL Tables |
|
|
69 | (1) |
|
|
70 | (2) |
|
|
72 | (1) |
|
|
72 | (2) |
|
Try This 3-2: Altering and Deleting SQL Tables |
|
|
74 | (1) |
|
|
75 | (2) |
|
4 Enforcing Data Integrity |
|
|
77 | (30) |
|
Understand Integrity Constraints |
|
|
78 | (2) |
|
|
80 | (1) |
|
|
81 | (3) |
|
Add PRIMARY KEY Constraints |
|
|
84 | (2) |
|
Add FOREIGN KEY Constraints |
|
|
86 | (10) |
|
|
92 | (2) |
|
The <referential triggered action> Clause |
|
|
94 | (2) |
|
Try This 4-1: Adding NOT NULL, Unique, and Referential Constraints |
|
|
96 | (3) |
|
|
99 | (5) |
|
|
102 | (1) |
|
Creating Domains and Domain Constraints |
|
|
103 | (1) |
|
Try This 4-2: Adding a CHECK Constraint |
|
|
104 | (1) |
|
|
105 | (2) |
|
|
107 | (20) |
|
Add Views to the Database |
|
|
108 | (11) |
|
|
114 | (5) |
|
|
119 | (3) |
|
Using the WITH CHECK OPTION Clause |
|
|
121 | (1) |
|
Drop Views from the Database |
|
|
122 | (2) |
|
Try This 5-1: Adding Views to Your Database |
|
|
124 | (1) |
|
|
125 | (2) |
|
6 Managing Database Security |
|
|
127 | (22) |
|
Understand the SQL Security Model |
|
|
128 | (7) |
|
|
130 | (2) |
|
Accessing Database Objects |
|
|
132 | (3) |
|
|
135 | (1) |
|
Grant and Revoke Privileges |
|
|
136 | (6) |
|
|
139 | (3) |
|
|
142 | (1) |
|
|
143 | (1) |
|
Try This 6-1: Managing Roles and Privileges |
|
|
143 | (2) |
|
|
145 | (4) |
Part II Data Access and Modification |
|
|
|
149 | (34) |
|
Use a SELECT Statement to Retrieve Data |
|
|
150 | (6) |
|
The SELECT Clause and FROM Clause |
|
|
151 | (5) |
|
Use the WHERE Clause to Define Search Conditions |
|
|
156 | (15) |
|
Defining the WHERE Clause |
|
|
160 | (3) |
|
Use the GROUP BY Clause to Group Query Results |
|
|
163 | (8) |
|
Use the HAVING Clause to Specify Group Search Conditions |
|
|
171 | (2) |
|
Use the ORDER BY Clause to Sort Query Results |
|
|
173 | (3) |
|
Try This 7-1: Querying the Inventory Database |
|
|
176 | (3) |
|
|
179 | (4) |
|
|
183 | (24) |
|
|
184 | (7) |
|
Inserting Values from a SELECT Statement |
|
|
189 | (2) |
|
|
191 | (5) |
|
Updating Values from a SELECT Statement |
|
|
194 | (2) |
|
|
196 | (1) |
|
|
197 | (4) |
|
Try This 8-1: Modifying SQL Data |
|
|
201 | (3) |
|
|
204 | (3) |
|
|
207 | (32) |
|
|
208 | (7) |
|
Using the BETWEEN Predicate |
|
|
213 | (2) |
|
|
215 | (3) |
|
|
218 | (4) |
|
Try This 9-1: Using Predicates in SQL Statements |
|
|
222 | (2) |
|
Reference Additional Sources of Data |
|
|
224 | (7) |
|
|
225 | (3) |
|
Using the EXISTS Predicate |
|
|
228 | (3) |
|
Quantify Comparison Predicates |
|
|
231 | (4) |
|
Using the SOME and ANY Predicates |
|
|
231 | (3) |
|
|
234 | (1) |
|
Try This 9-2: Using Subqueries in Predicates |
|
|
235 | (2) |
|
|
237 | (2) |
|
10 Working with Functions and Value Expressions |
|
|
239 | (30) |
|
|
240 | (7) |
|
|
241 | (2) |
|
Using the MAX and MIN Functions |
|
|
243 | (2) |
|
|
245 | (1) |
|
|
246 | (1) |
|
|
247 | (6) |
|
Working with String Value Functions |
|
|
248 | (3) |
|
Working with Datetime Value Functions |
|
|
251 | (2) |
|
|
253 | (8) |
|
Working with Numeric Value Expressions |
|
|
254 | (3) |
|
Using the CASE Value Expression |
|
|
257 | (3) |
|
Using the CAST Value Expression |
|
|
260 | (1) |
|
|
261 | (2) |
|
Try This 10-1: Using Functions and Value Expressions |
|
|
263 | (3) |
|
|
266 | (3) |
|
11 Accessing Multiple Tables |
|
|
269 | (24) |
|
Perform Basic Join Operations |
|
|
270 | (8) |
|
|
274 | (1) |
|
Creating Joins with More than Two Tables |
|
|
275 | (1) |
|
|
275 | (1) |
|
|
276 | (2) |
|
Join Tables with Shared Column Names |
|
|
278 | (2) |
|
Creating the Natural Join |
|
|
278 | (1) |
|
Creating the Named Column Join |
|
|
279 | (1) |
|
|
280 | (5) |
|
|
280 | (2) |
|
|
282 | (3) |
|
|
285 | (3) |
|
Try This 11-1: Querying Multiple Tables |
|
|
288 | (2) |
|
|
290 | (3) |
|
12 Using Subqueries to Access and Modify Data |
|
|
293 | (22) |
|
Create Subqueries That Return Multiple Rows |
|
|
294 | (6) |
|
|
295 | (2) |
|
Using the EXISTS Predicate |
|
|
297 | (1) |
|
Using Quantified Comparison Predicates |
|
|
298 | (2) |
|
Create Subqueries That Return One Value |
|
|
300 | (1) |
|
Work with Correlated Subqueries |
|
|
301 | (1) |
|
|
302 | (2) |
|
Use Subqueries to Modify Data |
|
|
304 | (3) |
|
Using Subqueries to Insert Data |
|
|
305 | (1) |
|
Using Subqueries to Update Data |
|
|
306 | (1) |
|
Try This 12-1: Working with Subqueries |
|
|
307 | (5) |
|
Using Subqueries to Delete Data |
|
|
307 | (5) |
|
|
312 | (3) |
|
13 Working with Temporal Data |
|
|
315 | (36) |
|
Create and Use System-Versioned Tables |
|
|
317 | (8) |
|
Changing Data in a System-Versioned Table |
|
|
320 | (2) |
|
Querying a System-Versioned Table |
|
|
322 | (3) |
|
Create and Use Application-Time Period Tables |
|
|
325 | (9) |
|
Inserting Data into an Application-Time Period Table |
|
|
328 | (1) |
|
Updating Data in an Application-Time Period Table |
|
|
329 | (1) |
|
Deleting Data from an Application-Time Period Table |
|
|
330 | (4) |
|
Try This 13-1: Working with Application-Time Period Tables |
|
|
334 | (3) |
|
Create and Use System-Versioned Application-Time Period Tables |
|
|
337 | (10) |
|
Inserting Data into a System-Versioned Application-Time Period Table |
|
|
339 | (1) |
|
Updating Data in a System-Versioned Application-Time Period Table |
|
|
340 | (3) |
|
Deleting Data from a System-Versioned Application-Time Period Table |
|
|
343 | (4) |
|
|
347 | (4) |
Part III Advanced Data Access |
|
|
14 Creating SQL-Invoked Routines |
|
|
351 | (32) |
|
Understand SQL-Invoked Routines |
|
|
352 | (4) |
|
SQL-Invoked Procedures and Functions |
|
|
353 | (1) |
|
Working with the Basic Syntax |
|
|
354 | (2) |
|
Create SQL-Invoked Procedures |
|
|
356 | (3) |
|
Invoking SQL-Invoked Procedures |
|
|
358 | (1) |
|
Add Input Parameters to Your Procedures |
|
|
359 | (4) |
|
Using Procedures to Modify Data |
|
|
362 | (1) |
|
Add Local Variables to Your Procedures |
|
|
363 | (3) |
|
Work with Control Statements |
|
|
366 | (6) |
|
Create Compound Statements |
|
|
366 | (1) |
|
Create Conditional Statements |
|
|
367 | (2) |
|
Create Looping Statements |
|
|
369 | (3) |
|
Try This 14-1: Creating SQL-Invoked Procedures |
|
|
372 | (2) |
|
Add Output Parameters to Your Procedures |
|
|
374 | (1) |
|
Create SQL-Invoked Functions |
|
|
375 | (2) |
|
Try This 14-2: Creating SQL-Invoked Functions |
|
|
377 | (2) |
|
|
379 | (4) |
|
|
383 | (26) |
|
|
384 | (3) |
|
Trigger Execution Context |
|
|
386 | (1) |
|
|
387 | (3) |
|
Referencing Old and New Values |
|
|
389 | (1) |
|
|
390 | (1) |
|
|
390 | (3) |
|
|
393 | (5) |
|
|
398 | (1) |
|
Create Instead Of Triggers |
|
|
399 | (4) |
|
Try This 15-1: Creating SQL Triggers |
|
|
403 | (3) |
|
|
406 | (3) |
|
|
409 | (28) |
|
|
410 | (4) |
|
Declaring and Opening SQL Cursors |
|
|
411 | (3) |
|
|
414 | (7) |
|
Working with Optional Syntax Elements |
|
|
415 | (3) |
|
Creating a Cursor Declaration |
|
|
418 | (3) |
|
|
421 | (1) |
|
Retrieve Data from a Cursor |
|
|
422 | (5) |
|
Use Positioned UPDATE and DELETE Statements |
|
|
427 | (3) |
|
Using the Positioned UPDATE Statement |
|
|
427 | (2) |
|
Using the Positioned DELETE Statement |
|
|
429 | (1) |
|
Try This 16-1: Working with SQL Cursors |
|
|
430 | (3) |
|
|
433 | (4) |
|
17 Managing SQL Transactions |
|
|
437 | (28) |
|
Understand SQL Transactions |
|
|
438 | (3) |
|
Set Transaction Properties |
|
|
441 | (8) |
|
Specifying an Isolation Level |
|
|
443 | (4) |
|
Specifying a Diagnostics Size |
|
|
447 | (1) |
|
Creating a SET TRANSACTION Statement |
|
|
448 | (1) |
|
|
449 | (2) |
|
Set Constraint Deferability |
|
|
451 | (2) |
|
Create Savepoints in a Transaction |
|
|
453 | (2) |
|
|
455 | (1) |
|
|
455 | (4) |
|
|
456 | (1) |
|
Rolling Back a Transaction |
|
|
457 | (2) |
|
Try This 17-1: Working with Transactions |
|
|
459 | (2) |
|
|
461 | (4) |
|
18 Accessing SQL Data from Your Host Program |
|
|
465 | (30) |
|
|
466 | (2) |
|
Embed SQL Statements in Your Program |
|
|
468 | (9) |
|
Creating an Embedded SQL Statement |
|
|
469 | (2) |
|
Using Host Variables in Your SQL Statements |
|
|
471 | (2) |
|
|
473 | (2) |
|
|
475 | (2) |
|
Try This 18-1: Embedding SQL Statements |
|
|
477 | (3) |
|
Create SQL Client Modules |
|
|
480 | (2) |
|
Defining SQL Client Modules |
|
|
481 | (1) |
|
Use an SQL Call-Level Interface |
|
|
482 | (8) |
|
|
484 | (2) |
|
|
486 | (1) |
|
Working with Host Variables |
|
|
487 | (2) |
|
|
489 | (1) |
|
Try This 18-2: Using the SQL Call-Level Interface |
|
|
490 | (2) |
|
|
492 | (3) |
|
|
495 | (16) |
|
|
496 | (3) |
|
|
499 | (7) |
|
|
499 | (2) |
|
|
501 | (1) |
|
|
502 | (4) |
|
Try This 19-1: Using SQL/XML Functions |
|
|
506 | (1) |
|
|
507 | (4) |
Part IV Appendices |
|
|
|
511 | (8) |
|
|
512 | (3) |
|
|
515 | |
|
B Answers to Self Tests Online |
|
|
|
C SQL Code Used in Try This Exercises Online |
|
|
Index |
|
519 | |