Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Front Matter Blind Folio FM:i SQL A Beginner’s Guide Second Edition P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Front Matter Blind Folio FM:ii This page intentionally left blank P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Front Matter Blind Folio FM:iii SQL A Beginner’s Guide Second Edition Robert Sheldon McGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Front Matter Blind Folio FM:iv McGraw-Hill/Osborne 2100 Powell Street, 10th Floor Emeryville, California 94608 U.S.A To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact McGraw-Hill/Osborne at the above address For information on translations or book distributors outside the U.S.A., please see the International Contact Information page immediately following the index of this book SQL: A Beginner’s Guide, Second Edition Copyright © 2003 by The McGraw-Hill Companies All rights reserved Printed in the United States of America Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication 1234567890 FGR FGR 019876543 ISBN 0-07-222885-7 Publisher Brandon A Nordin Vice President & Associate Publisher Scott Rogers Acquisitions Editor Lisa McClain Senior Project Editor LeeAnn Pickrell Acquisitions Coordinator Athena Honore Technical Editor Greg Guntle Copy Editor Margaret Berson Proofreader Marion Selig Indexer James Minkin Computer Designers Carie Abrew, Tara A Davis Illustrators Kathleen Fay Edwards, Melinda Moore Lytle, Michael Mueller, Lyssa Wald Series Design Jean Butterfield Series Cover Design Sarah F Hinks This book was composed with Corel VENTURA™ Publisher Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable However, because of the possibility of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Front Matter Blind Folio FM:v About the Author Robert Sheldon has worked as a consultant and technical writer for a number of years As a consultant, he has managed the development and maintenance of web-based and client-server applications and the databases that supported those applications In addition, he has designed and implemented various Access and SQL Server databases and has used SQL to build databases, create and modify database objects, query and modify data, and troubleshoot system- and data-related problems Robert has also written or cowritten eight books on various network and server technologies, one of which received a Certificate of Merit from the Puget Sound Chapter of the Society for Technical Communication In addition, two of the books that Robert has written focus exclusively on SQL Server design and implementation Robert has also written and edited a variety of other documentation related to SQL databases and other computer technologies In addition, his writing includes material outside the computer industry—everything from news articles to ad copy to legal documentation—and he has received two awards from the Colorado Press Association P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Front Matter Blind Folio FM:ii This page intentionally left blank P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Blind Folio FM:vii Color profile: Generic CMYK printer profile Composite Default screen Contents at a Glance PART I SQL Databases Introduction to Relational Databases and SQL Working with the SQL Environment 27 Creating and Altering Tables 47 Enforcing Data Integrity 73 Creating SQL Views 105 Managing Database Security 125 PART II Data Access and Modification Querying SQL Data 149 Modifying SQL Data 181 Using Predicates 201 10 Working with Functions and Value Expressions 233 vii P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 / Front Matter Blind Folio FM:viii Color profile: Generic CMYK printer profile Composite Default screen viii SQL: A Beginner’s Guide 11 Accessing Multiple Tables 261 12 Using Subqueries to Access and Modify Data 285 PART III Advanced Data Access 13 Creating SQL-Invoked Routines 307 14 Creating SQL Triggers 337 15 Using SQL Cursors 361 16 Managing SQL Transactions 389 17 Accessing SQL Data from Your Host Program 417 PART IV Appendixes A Answers to Mastery Checks 451 B SQL:1999 Keywords 489 C SQL Code Used in the Book’s Projects 495 P:\010Comp\Begin8\885-7\fm.vp Tuesday, April 08, 2003 12:33:10 PM Color profile: Generic CMYK printer profile Composite Default screen 518 Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 Blind Folio Index:518 SQL: A Beginner’s Guide project on working with, 383–386, 506 Q & A about, 379 read-only, 370 retrieving data from, 374–379 returnability options, 368–369 scrollability options, 367 sensitivity options, 367 SQL client modules and, 364, 433 syntax for, 366–370 updatability options, 369–370 D data deleting, 69, 194–195, 299 denormalizing, 10 inserting, 182–188, 297–298 integrity of, 12, 74 modifying, 182–199, 296–299 normalizing, 6–10, 12–13 querying, 150–180 updating, 188–193, 298–299 data access methods, 418–448 call-level interface, 435–446 direct invocation, 418–419 embedded SQL, 419–432 mastery check on, 446–448, 486–488 projects on, 429–432, 443–446 SQL client modules, 432–435 data anomalies, 395–397 dirty reads, 395, 396 nonrepeatable reads, 395–396, 397 phantom reads, 396–397, 398 summary table of, 399 Data Control Language (DCL), 17 Data Definition Language (DDL), 16, 403 data host variables See host variables data integrity, 12, 74 See also constraints Data Manipulation Language (DML), 17, 403 data models, 12, 94 data types, 5, 52–62 Boolean, 59, 60 constructed, 52 datetime, 56–57 P:\010Comp\Begin8\885-7\index.vp Tuesday, April 08, 2003 1:34:31 PM example of using, 61 interval, 57–59 numeric, 55–56 predefined, 52, 60 Q & A about, 60 string, 53–55 user-defined, 53, 62–63 database objects privileges assigned to, 130–132 See also schema objects databases, creating, 42, 43 models of, 4–5 project on connecting to, 22–24, 496 SQL environment and, 35–36 See also relational databases Databases node, 35 DATE data type, 56 datetime data types, 56–57 descriptions/examples of, 57 types of, 56 datetime value functions, 244–245 day-time intervals, 57 DCL statements, 17 DDL statements, 16, 403 DECLARE CURSOR statement, 364, 366–373 basic syntax for, 366 examples of using, 370–373 holdability options, 368 optional syntax elements, 366–370 ordering options, 369 returnability options, 368–369 scrollability options, 367 sensitivity options, 367 updatability options, 369–370 DECLARE statement, 321 declared local temporary tables, 49 declaring cursors, 363–364, 366–373 host variables, 423–424 variables, 319–321 DEFAULT keyword, 63 deferrable constraints, 402–405 DEFERRED keyword, 405 DELETE FROM clause, 194 Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 Blind Folio Index:519 Index DELETE privilege, 131 DELETE statement, 69, 194–195 cursor declarations and, 369–370 examples of using, 194–195 mastery check on, 199, 468–469 positioned, 382 procedures containing, 318 project using, 195–197 subqueries used in, 299 syntax for, 194 triggers and, 352–354 delete triggers, 352–354 project on creating, 354–357 syntax for, 352 deleting data, 69, 194–195, 299, 318 functions, 319 procedures, 319 roles, 133–134 schemas, 31, 40–41 tables, 68, 70 triggers, 344 view definitions, 121 delimited identifiers, 37–38 denormalizing data, 10 placeholder, 152–153 derived tables, 34, 48 DESC keyword, 172, 173, 369 diagnostics size, 399–400 direct invocation, 17, 18, 362, 418–419 direct subtype, 132 dirty reads, 395, 396 DISTINCT keyword, 151–152, 155–156 distinct types, 62, 308 division operator (/), 247 DML statements, 17, 403 domain constraints, 74, 99–100 domains, 5, 33 CHECK constraint and, 100–101 SQL vs relational model, 34 syntax for creating, 100 DROP FUNCTION statement, 319 DROP PROCEDURE statement, 319 DROP ROLE statement, 134 DROP SCHEMA statement, 31, 40–41 P:\010Comp\Begin8\885-7\index.vp Tuesday, April 08, 2003 1:34:31 PM DROP TABLE statement, 48, 68, 70 DROP TRIGGER statement, 344, 350 DROP VIEW statement, 121 E ELSE clause, 250, 324 ELSE keyword, 322–323 ELSEIF clause, 324 embedded SQL, 18, 362, 419–432 compiling programs with, 420–421 data retrieval with, 424–427 error handling with, 427–428 EXEC SQL statement and, 364, 422 guidelines for using, 421–422 host variables used with, 423–424 null values retrieved with, 426–427 overview of, 419–420 prefixes and terminators for, 422 programming languages using, 420 project on using, 429–432, 507–508 Q & A about, 425 END DECLARE SECTION statement, 424 END keyword, 320, 321–322 END LOOP keyword, 324 Entry level conformance, 16 environment handle, 437–439 equals sign (=), 202, 203 equi-joins, 263–264, 272–273 error handling, 427–428 exact numeric data types, 55 EXEC SQL statement, 364 ExecDirect() function, 440 Execute() function, 441 EXECUTE privilege, 132 EXECUTE statement, 314 executing SQL statements, 17–18, 440–441 execution context, 339–340 existential quantifiers, 224 EXISTS predicate, 221–223 subqueries used in, 288–289, 293 expressions query, 116 value, 246–253 519 Color profile: Generic CMYK printer profile Composite Default screen 520 Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 Blind Folio Index:520 SQL: A Beginner’s Guide F placeholder, 375 FETCH statement, 364, 374–379 examples of using, 376–379 orientation options, 375–376 overview of, 374–375 first normal form, 7–8 FIRST option, 376 flags, 76 floating-point numbers, 55 FOR EACH clause, 342, 345, 349, 352 FOR keyword, 369 FOR UPDATE clause, 381–382 FOREIGN KEY constraints, 83–91 guidelines for creating, 85 MATCH clause, 88–90 multiple columns and, 87–88 clause, 90–91 syntax for, 85–87 FROM clause, 153–156 FETCH statement and, 375 view definitions and, 113, 115 full outer joins, 275–276 fully qualified names, 38 functions, 33, 62, 234–245, 330–333 AVG, 239–240 CLI routines and, 435 COUNT, 234–236 creating, 310–311, 330–333 datetime, 244–245 deleting, 319 LOWER, 243–244 mastery check on, 258–259, 471–473 MAX, 236–238 MIN, 236–238 modifying, 319 procedures compared to, 309 projects on, 255–258, 332–333, 501–502, 504 set, 234–240 string, 241–244 SUBSTRING, 242–243 SUM, 239 UPPER, 243–244 value, 241–245 See also procedures P:\010Comp\Begin8\885-7\index.vp Tuesday, April 08, 2003 1:34:31 PM G global temporary tables, 48 GOTO action, 428 GRANT clause, 135, 141 GRANT OPTION FOR clause, 139, 140 GRANT statement, 132, 134–138, 141–142 GRANTED BY clause, 139, 141 granting privileges, 132, 134–138 roles, 141–142 greater-than operator (>), 96, 114, 203 greater-than-or-equals operator (>=), 203 GROUP BY clause, 165–169 placeholder, 165 GUI applications, 20 H HAVING clause, 170–171 subqueries in, 286 hierarchical databases, holdability, cursor, 368 host variables, 375, 379, 423–424 CLI and, 438, 441–442 declaring, 423–424 indicator, 426–427 I identifiers, 37–38 IF ELSE statement, 322 IF END IF statement, 322 IF THEN ELSE statement, 322 IF clause, 324 IF keyword, 322–323 IF statement, 322, 325 IMMEDIATE keyword, 405 impedance mismatch, 60, 362, 418, 425 IN keyword, 315 IN operator, 98 IN predicate, 218–221 subqueries used in, 287–288, 292 indexes, 69 indicator host variables, 426–427 Color profile: Generic CMYK printer profile Composite Default screen Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 Blind Folio Index:521 Index Information Schema, 32 inner joins, 273–275 INOUT keyword, 315 input parameters, 315–318 input/output parameters, 315 INSENSITIVE option, 367 INSERT INTO clause, 182–186 INSERT keyword, 346 INSERT privilege, 131, 137, 140 INSERT statement, 182–188 examples of using, 183–186 executing multiple, 192–193 mastery check on, 198, 466–467 procedures containing, 318 project using, 175–176, 195–197 Q & A about, 192–193 SELECT statement and, 186–188 subqueries used in, 297–298 syntax for, 182 triggers and, 344–347 VALUES clause, 182–186 insert triggers, 344–347 project on creating, 354–357 syntax for, 345 inserting SQL data, 182–188 procedures for, 318 project on, 195–197 SELECT statement for, 186–188 subqueries for, 297–298 through a view, 193 INTEGER data type, 425 integrity constraints, 74–75 categories of, 74 types of, 74–75 See also constraints interactive SQL, 362, 411, 418–419 International Organization for Standardization (ISO), 4, 308 interval data type, 57–59 INTERVAL keyword, 58 INTO clause, 426 Inventory database, SQL statements, 509–513 IS FALSE operator, 160 IS TRUE operator, 160 IS UNKNOWN operator, 160 P:\010Comp\Begin8\885-7\index.vp Tuesday, April 08, 2003 1:34:31 PM isolation levels, 394–399 choosing for transactions, 397–399 data anomalies and, 395–397 options specified for, 394 J join operations, 262–284 basic performance of, 262–265 comma-separated joins, 262–265 condition joins, 272–278 correlated subqueries and, 293, 294 correlation names and, 265–266 cross joins, 267 equi-joins, 263–264, 272–273 inner joins, 273–275 mastery check on, 283–284, 473–474 multiple tables and, 266–267 named column joins, 272, 273 natural joins, 271–272, 273 outer joins, 275–278 overview of, 262 project on using, 281–283, 502 Q & A about, 281 SELECT statement and, 262–265, 294 self-joins, 268–269 shared column names and, 270–272 union joins, 281 UNION operator and, 279–281 See also subqueries K keywords, 37, 490–494 nonreserved, 493–494 reserved, 490–492 See also specific keywords L LANGUAGE clause, 432 LAST option, 376 LEAVE keyword, 324 left outer joins, 275–276 521 Color profile: Generic CMYK printer profile Composite Default screen 522 Begin8 / SQL: A Beginner’s Guide / Sheldon / 222885-7 Blind Folio Index:522 SQL: A Beginner’s Guide less-than operator (