1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu SQL A Beginner’s Guide Third Edition pptx

553 392 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 553
Dung lượng 4,2 MB

Nội dung

www.it-ebooks.info SQL A Beginner’s Guide Third Edition Andy Oppel Robert Sheldon New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto www.it-ebooks.info Copyright © 2009 by The McGraw-Hill Companies All rights reserved Manufactured in the United States of America Except as permitted under the United States 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 0-07-154865-3 The material in this eBook also appears in the print version of this title: 0-07-154864-5 All trademarks are trademarks of their respective owners Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark Where such designations appear in this book, they have been printed with initial caps McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs For more information, please contact George Hoare, Special Sales, at george_hoare@mcgraw-hill.com or (212) 904-4069 TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc (“McGraw-Hill”) and its licensors reserve all rights in and to the work Use of this work is subject to these terms Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited Your right to use the work may be terminated if you fail to comply with these terms THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE McGraw-Hill and its licensors not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom McGraw-Hill has no responsibility for the content of any information accessed through the work Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise DOI: 10.1036/0071548645 www.it-ebooks.info About the Authors Andrew (Andy) J Oppel is a proud graduate of the Boys’ Latin School of Maryland and of Transylvania University (Lexington, Kentucky) where he earned a BA in computer science in 1974 Since then he has been continuously employed in a wide variety of information technology positions, including programmer, programmer/analyst, systems architect, project manager, senior database administrator, database group manager, consultant, database designer, data modeler, and data architect In addition, he has been a part-time instructor with the University of California (Berkeley) Extension for over 20 years, and received the Honored Instructor Award for the year 2000 His teaching work included developing three courses for UC Extension, “Concepts of Database Management Systems,” “Introduction to Relational Database Management Systems,” and “Data Modeling and Database Design.” He also earned his Oracle 9i Database Associate certification in 2003 He is currently employed as a senior data modeler for Blue Shield of California Aside from computer systems, Andy enjoys music (guitar and vocals), amateur radio (Pacific Division vice director, American Radio Relay League) and soccer (referee instructor, U.S Soccer) Andy has designed and implemented hundreds of databases for a wide range of applications, including medical research, banking, insurance, apparel manufacturing, telecommunications, wireless communications, and human resources He is the author of Databases Demystified (McGraw-Hill/Osborne, 2004) and SQL Demystified (McGraw-Hill/Osborne, 2005) His database product experience includes IMS, DB2, Sybase, Microsoft SQL Server, Microsoft Access, MySQL, and Oracle (versions 7, 8, 8i, 9i, and 10g) 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 clientserver applications and the databases that supported those applications 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 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 About the Technical Editor James Seymour is a graduate of the University of North Carolina at Chapel Hill with a BA in history and political science and the University of Kentucky with a MA in history He became first involved with computer technology in 1965 with the mainframe environment at North Carolina While in the United States Army during the Vietnam War, he was on the small team that worked with the mainframe setup at the Pentagon for various military strategic scenarios Since 1972, he has been involved in varied computer environments with the second point-of-sale Copyright © 2009 by The McGraw-Hill Companies Click here for terms of use www.it-ebooks.info and inventory control project in the retail industry, analytical programs and database initiatives in the insurance and benefits industries, loss control startups, and other inventory control and sales tracking projects throughout many different industries From 1987 through 1995, James was an instructor of database management in the community college system of the state of Kentucky In this capacity, he created the first database management and C programming courses in the state of Kentucky and helped both public and private entities with urgent training needs, including the programming of guidance systems on cruise missiles for Desert Storm Before 1985, he was a system administrator, network administrator, programmer, and database administrator Since 1985, James has been a senior database administrator working primarily with DB2 and Oracle DBMSs on multiple platforms including SQL Server beginning with version 7.0 He is currently the senior database administrator and data architect for a Fortune 100 company overseeing major projects in the United States, Canada, and the United Kingdom www.it-ebooks.info For more information about this title, click here Contents ACKNOWLEDGMENTS INTRODUCTION xi xi PART I Relational Databases and SQL Introduction to Relational Databases and SQL Understand Relational Databases The Relational Model Learn About SQL The SQL Evolution Types of SQL Statements Types of Execution SQL Standard versus Product Implementations Working with the SQL Environment Understand the SQL Environment Understand SQL Catalogs Schemas Schema Objects Then What Is a Database? Name Objects in an SQL Environment Qualified Names v www.it-ebooks.info 15 15 18 19 21 29 30 32 34 35 37 40 41 vi SQL: A Beginner’s Guide Create a Schema Create a Database 42 44 Creating and Altering Tables Create SQL Tables Specify Column Data Types String Data Types Numeric Data Types Datetime Data Types Interval Data Type Boolean Data Type Using SQL Data Types Create User-Defined Types Specify Column Default Values Delete SQL Tables 49 Enforcing Data Integrity Understand Integrity Constraints Use NOT NULL Constraints Add UNIQUE Constraints Add PRIMARY KEY Constraints Add FOREIGN KEY Constraints The MATCH Clause The Clause Define CHECK Constraints Defining Assertions Creating Domains and Domain Constraints 73 Creating SQL Views Add Views to the Database Defining SQL Views Create Updateable Views Using the WITH CHECK OPTION Clause Drop Views from the Database 103 Managing Database Security Understand the SQL Security Model SQL Sessions Accessing Database Objects Create and Delete Roles Grant and Revoke Privileges Revoking Privileges Grant and Revoke Roles Revoking Roles 123 www.it-ebooks.info 50 54 55 57 58 60 61 62 63 64 69 74 76 77 79 83 88 89 95 97 98 104 108 114 116 117 124 126 128 130 131 135 137 138 Contents PART II Data Access and Modification Querying SQL Data Use a SELECT Statement to Retrieve Data The SELECT Clause and FROM Clause Use the WHERE Clause to Define Search Conditions Defining the WHERE Clause Use the GROUP BY Clause to Group Query Results Use the HAVING Clause to Specify Group Search Conditions Use the ORDER BY Clause to Sort Query Results 145 Modifying SQL Data Insert SQL Data Inserting Values from a SELECT Statement Update SQL Data Updating Values from a SELECT Statement Delete SQL Data 175 Using Predicates Compare SQL Data Using the BETWEEN Predicate Return Null Values Return Similar Values Reference Additional Sources of Data Using the IN Predicate Using the EXISTS Predicate Quantify Comparison Predicates Using the SOME and ANY Predicates Using the ALL Predicate 193 10 Working with Functions and Value Expressions Use Set Functions Using the COUNT Function Using the MAX and MIN Functions Using the SUM Function Using the AVG Function Use Value Functions Working with String Value Functions Working with Datetime Value Functions Use Value Expressions Working with Numeric Value Expressions Using the CASE Value Expression Using the CAST Value Expression Use Special Values 225 www.it-ebooks.info 146 147 152 156 159 164 166 176 180 182 185 186 194 199 200 203 209 209 213 216 216 218 226 227 229 231 232 232 233 236 238 238 241 244 245 vii viii SQL: A Beginner’s Guide 11 Accessing Multiple Tables Perform Basic Join Operations Using Correlation Names Creating Joins with More than Two Tables Creating the Cross Join Creating the Self-Join Join Tables with Shared Column Names Creating the Natural Join Creating the Named Column Join Use the Condition Join Creating the Inner Join Creating the Outer Join Perform Union Operations 253 12 Using Subqueries to Access and Modify Data Create Subqueries That Return Multiple Rows Using the IN Predicate Using the EXISTS Predicate Using Quantified Comparison Predicates Create Subqueries That Return One Value Work with Correlated Subqueries Use Nested Subqueries Use Subqueries to Modify Data Using Subqueries to Insert Data Using Subqueries to Update Data Using Subqueries to Delete Data 277 PART III 254 257 258 259 260 261 262 263 263 264 266 269 278 279 281 282 283 284 286 288 288 290 291 Advanced Data Access 13 Creating SQL-Invoked Routines Understand SQL-Invoked Routines SQL-Invoked Procedures and Functions Working with the Basic Syntax Create SQL-Invoked Procedures Invoking SQL-Invoked Procedures Add Input Parameters to Your Procedures Using Procedures to Modify Data Add Local Variables to Your Procedures Work with Control Statements Create Compound Statements Create Conditional Statements Create Looping Statements Add Output Parameters to Your Procedures Create SQL-Invoked Functions www.it-ebooks.info 299 300 301 301 303 305 306 309 311 313 313 314 316 320 321 Contents 14 Creating SQL Triggers Understand SQL Triggers Trigger Execution Context Create SQL Triggers Referencing Old and New Values Dropping SQL Triggers Create Insert Triggers Create Update Triggers Create Delete Triggers 329 15 Using SQL Cursors Understand SQL Cursors Declaring and Opening SQL Cursors Declare a Cursor Working with Optional Syntax Elements Creating a Cursor Declaration Open and Close a Cursor Retrieve Data from a Cursor Use Positioned UPDATE and DELETE Statements Using the Positioned UPDATE Statement Using the Positioned DELETE Statement 351 16 Managing SQL Transactions Understand SQL Transactions Set Transaction Properties Specifying an Isolation Level Specifying a Diagnostics Size Creating a SET TRANSACTION Statement Start a Transaction Set Constraint Deferability Create Savepoints in a Transaction Releasing a Savepoint Terminate a Transaction Committing a Transaction Rolling Back a Transaction 377 17 Accessing SQL Data from Your Host Program Invoke SQL Directly Embed SQL Statements in Your Program Creating an Embedded SQL Statement Using Host Variables in Your SQL Statements Retrieving SQL Data Error Handling 403 www.it-ebooks.info 330 331 333 334 335 336 338 343 352 353 355 356 360 363 363 368 368 370 378 381 382 387 388 389 390 392 394 395 395 396 404 406 407 408 411 413 ix 520 SQL: A Beginner’s Guide aggregate functions See set functions aliases with triggers, 334–335 using, 257–258 ALL keyword with SELECT clause, 147–148, 152 after UNION operator, 271 ALL predicate, 218–219, 221 ALL PRIVILEGES option with GRANT clause, 132 using, 136 allocation handles in CLI, 421–423, 427 AllocHandle() function, 422–423 ALTER [COLUMN] clause, 68 ALTER FUNCTION statement, 311 ALTER PROCEDURE statement, 310 ALTER TABLE statement with CHECK constraints, 99 using, 50, 67–68 ALTER VIEW statement, 118 ampersand (&) with allocation handles, 422 AND CHAIN clause with COMMIT, 396 with ROLLBACK, 397 AND keyword with joins, 257 with BETWEEN predicate, 199 with predicates, 153–155, 157, 197–198, 208, 219 AND NO CHAIN clause with COMMIT, 396 with ROLLBACK, 397 AND NOT keywords, 157–158 angle brackets () using, 42 in XML, 436 ANSI standard for SQL-86, 16 ANY predicate, 216–218, 221, 282 App_User authorization identifier, 127–128 ARTIST_CDS table, creating, 94 ARTIST_LOG table with triggers, 345–348 ARTISTS table altering, 68 constraints in, 94 creating, 53, 62–63, 65 creating insert trigger on, 345 querying, 170 transactions in, 398–400 ARTISTS_CDS table, set functions on, 226–227 AS keyword with CAST, 244–245 with correlation names, 258 with views, 109–111 AS subclause with SELECT statement, 151 ASC keyword with ORDER BY, 166 ASENSITIVE option with cursors, 357 assertions defined, 74 defining, 97–98 limitation of, 75 asterisk (*) in COUNT function, 227–228 with cursors, 361 with query results, 148 ATOMIC keyword with insert triggers, 337 with triggers, 334 atomic quality, applying to transactions, 379 attributes defined, functional dependence of, terminology, unique identifiers, 8–9 in XML, 436 AUTHORIZATION clause, 417 option, 133 value, 43 authorization identifiers See also identifiers associating privileges with, 131–132 with comma (,), 137–138 description of, 32 role names, 125 user identifiers, 124 AUTHORIZATION keyword, 42 authorization stack, creating, 127 authorization users, determining, 128 averages, creating views of, 115 AVG function in queries, 148–149 using, 232 in views, 115 AVG_PRICE column, returning, 165 B base tables See also tables created local temporary, 51 creating, 51 declared local temporary, 51 description of, 35, 50 global temporary, 51 naming, 52 persistent, 50 specifying parts of, 52 types of, 104 www.it-ebooks.info BEFORE keyword with triggers, 333 BEGIN block with compound statements, 313–314 with loops, 318 BEGIN END statement with conditional statements, 314 with delete triggers, 344 with insert triggers, 337 with triggers, 334, 340 using, 313–314 BETWEEN operator with constraints, 97 BETWEEN predicate, 199–200, 207 BIGINT data types, mapping to XML schema types, 442 binary strings, 55, 57 BindCol() function in CLI, 426 binding styles See execution methods BindParameter() function in CLI, 425 bit strings, 55–56 BOOLEAN data types mapping to XML schema types, 442 using, 61 brackets ([]) with MATCH clause, 88 bytes size of, 55 storing, 57 C C program AllocHandle() function in, 422 BindParameter() function in, 425 calling modules in, 418 declaring host variables in, 409–410 embedding statements in, 409 SQL statements in, 408 CALL statement with conditional statements, 315 with loops, 317 with procedures, 305–306, 308–310, 312, 320 call-level interface (CLI) allocation handles in, 421–423 ExecDirect() function in, 423–424, 428 Execute() function in, 424–425 executing SQL statements in, 423–424 host variables in, 424–425 overview of, 419–420 Prepare() function in, 424 retrieving data in, 426 using, 427–429, 513 Index candidate key defined, with PRIMARY KEY constraint, 80 Cartesian product table, producing, 255 CASCADE CONSTRAINTS option, 93 CASCADE option with procedures, 311 with REVOKE, 136 with schemas, 43–44 with tables, 68–69 CASCADE referential action, 90 CASE value expression, 241–244, 249 CAST value expression, 244–245, 249, 410 catalogs See also schemas components of, 33 description of, 32 hierarchical structure of, 33–34 object relationships in, 34 versus schemas, 34 schemas in, 34–35 CATEGORY column, values in, 160, 163 CD_ARTIST function defining, 322 returning values from, 323 CD_ARTISTS table, querying, 281 CD_DISCOUNTS view columns in, 113 displaying, 108–109 CD_IN_STOCK view, querying, 171 CD_INVENTORY schema, creating, 45 CD_INVENTORY table declaring cursors on, 360–363 granting access to, 134 inserting values into, 177–182 querying, 181–182 CD_LABELS table adding company to, 188 creating, 67 using constraints with, 93 CD_PUBLISHERS view, creating, 119 CD_STOCK table creating delete trigger on, 343 querying, 279–280 CD_TITLES table, creating, 85 CD_TRACKING table querying, 241 value expressions in, 238 CDs in stock, determining, 247–248 CDS_IN_STOCK view, creating, 119 CHARACTER data types descriptions of, 56 mapping to XML schema types, 442 clause, 43 character sets, 36 character strings See also string value functions comparing, 230 types of, 55 CHECK constraints adding, 99 assertions as, 97–98 defining, 95–97, 500 domains as, 98 child versus parent nodes, 5–6 class keyword with separator, 133 clauses optional use of, 88 processing in order, 165, 228 CLI (call-level interface) allocation handles in, 421–423 ExecDirect() function in, 423–424, 428 Execute() function in, 424–425 executing SQL statements in, 423–424 host variables in, 424–425 overview of, 419–420 Prepare() function in, 424 retrieving data in, 426 using, 427–429, 513 CLI method, 20 CLI model, implementations of, 419 client modules See SQL client modules CLOSE statement with cursors, 354, 363 COBOL, SQL statements in, 408 Codd, E.F., 5, 7, 15 code, separating into lines, 96–97 code samples altering and deleting tables, 499 CHECK constraints, 500 CLI (call-level interface), 513 creating SQL tables, 498 cursors, 510–511 embedded SQL statements, 512 functions, 504–505 modifying SQL data, 502 NOT NULL constraints, 499–500 predicates in SQL statements, 503 querying INVENTORY database, 501–502 querying multiple tables, 505 referential constraints, 499–500 roles and privileges, 501 SQL-invoked functions, 508 www.it-ebooks.info SQL-invoked procedures, 507–508 SQL/XML functions, 513–514 subqueries, 505–507 subqueries in predicates, 503–504 transactions, 511 triggers, 508–509 UNIQUE constraints, 499–500 value expressions, 504–505 views, 500 collations, 36 colon (:) with host variables, 408–409 with statement label, 317 column CHECK constraint, creating, 95 column constraints adding, 85 creating, 78 defined, 74–75 FOREIGN KEY constraints as, 84–85 column data types, specifying, 54–55 column default values, specifying, 64–65 placeholder, 68 column definitions adding PRIMARY KEY constraints to, 81 without defaults, 69 interval data types in, 61 with NOT NULL, 76 with strings, 56 column names adding after privileges, 134 with comparison operators, 195 order in ORDER BY clause, 168 providing for derived columns, 149 providing for views, 110 sharing, 261–263 specifying in INSERT INTO, 177 column values adding together, 231–232 combining, 240–241 counting, 227–228 deleting, 187 columns See also placeholder; view columns adding, 239 averaging values in, 148–149 calculating, 240 combining into constraints, 79 creating and deleting, 70–71 521 522 SQL: A Beginner’s Guide columns (Continued) defining, 52–53 in foreign keys, 86–87 in HAVING clause, 165 for INSERT INTO, 178–179 in INSERT INTO, 180 listing values for, 96 naming for views, 110–111 in NULL predicate, 202 in query results, 148 querying, 149–151, 153 in result sets, 411 returning from tables, 361 specifying for columns, 95–97 specifying with GROUP BY, 162 unique values in, 77–79 using comma (,) with, 53 using RESTRICT option with, 68 values for, 95–97 viewing, 105 in views, 116 comma (,) with authorization identifiers, 137–138 with CALL statement, 308 with columns, 53 with INSERT INTO clause, 177 with table references, 149 comma-separated join versus cross join, 259 guidelines for, 257 implementing, 254 performing, 272 using, 258–259 comments, adding in XML, 436 COMMIT statement defined, 380 terminating transactions with, 395 with transactions, 395–396 using, 52, 381 COMPACT_DISC_STOCK table with HAVING clause, 165 using, 160–161 COMPACT_DISC_TYPES table constraints in, 93–94 creating, 70 querying, 294 COMPACT_DISCS table adding CD to, 188 with CHECK constraint, 99 with constraints, 93 creating, 66 granting privileges on, 130–140 inserting rows into, 188 querying, 171 comparison operators with ANY and SOME, 217–218 with subqueries, 284 symbols and examples of, 195 comparison predicates (>) combining, 197, 207 with constraints, 95 defining subqueries with, 292 description of, 194 compound statements, creating, 312–313 condition join description of, 263 as inner and outer join, 264 using, 273 conditional control statements conditions in, 316 using, 314–315 conditions and diagnostics size, 387 inverse of, 200, 212, 214 connection environment, establishing in CLI, 427 connection handle in CLI, 421–423 connectivity, verifying, 26 consistent quality, applying to transactions, 379 CONSTRAINT keyword, 86 constraints See also integrity constraints CHECK, 95–97 combining columns into, 79 data model for, 91–94 data types as, deferring in transactions, 390–393 defined, 68 defining, 52 description of, 36 domain, 98 FOREIGN KEY, 83–87 NOT NULL, 76–77, 93 PRIMARY KEY, 79–82 and relationships, 91 UNIQUE, 77–79, 93 constructed data types, 54 CONTENT type modifier in SQL/ XML, 438 CONTINUE action with WHENEVER, 413 control statements See also SQL statements compound, 312–313 conditional, 314–315 www.it-ebooks.info defined, 312 looping, 316–318 correlated subqueries See also queries defined, 213 versus joins, 286 using, 284–285 correlation names with triggers, 334–335 using, 257–258 COUNT function, 227–228, 247 CREATE DATABASE statement, 44 CREATE FUNCTION statement, 303, 322 CREATE INDEX statement, variations of, 70 CREATE PROCEDURE statement defining parameters in, 308 with output parameters, 321 using, 302–303, 305 CREATE ROLE statement syntax, 130 using, 131 CREATE SCHEMA statement syntax, 42–43 using, 34 CREATE TABLE statement for base tables, 51 executing, 53 syntax, 51–52 using, 43, 50, 65 CREATE TRIGGER statement using, 334, 336, 338–339 varying semantics of, 337 CREATE TYPE statement, 64 CREATE VIEW statement for averages, 115 WITH CHECK OPTION clause, 116–117 using, 110–111, 113 created local temporary tables, 51 cross join, creating, 259, 273 CUBE operator with GROUP BY, 163–164 curly brackets ({}),51 CURRENT OF option with UPDATE, 369, 374 CURRENT_* value functions, 236–237 CURRENT_ROLE value, 246 CURRENT_USER value description of, 246 with sessions, 128 cursor declaration, creating, 360–363 cursor holdability, overview of, 358 cursor ordering, overview of, 359 Index cursor returnability, overview of, 358–359 cursor scrollability defining, 361 overview of, 357 cursor sensitivity, options for, 357 cursor updatability defining, 362, 373 overview of, 359–360 cursors accessing tables with, 353–354 declaring, 354–356, 371–372 declaring and accessing, 373 declaring in modules, 417 defining, 369 features of, 364 opening and closing, 354, 363, 369 overview of, 353–355 as pointers, 353, 411 with positioned DELETE, 370–371 with positioned UPDATE, 368–370 read-only, 372–373 retrieving data from, 363–367 with SELECT statements, 368 statements used with, 354 using, 510–511 D data comparing with predicates, 194–198 deleting from referenced columns, 89–91 deleting with subqueries, 291 inserting, 288–289 joining with views, 108 modifying, 188–190, 502 modifying with procedures, 309–310 modifying with views, 108 in nodes, 5–6 protecting between tables, 83 in record types, retrieving, 411–412 retrieving in CLI, 426 storage in tuples, summarizing, 164 updating, 182–186, 290 updating from referenced columns, 89–91 using DELETE statement with, 69 data access methods, choosing, 420 Data Control Language (DCL), 19 Data Definition Language (DDL), 18 data integrity defined, ensuring, 13 Data Manipulation Language (DML), 19 data models with constraints, 91–94 defined, 13 for INVENTORY database, 91–92 data phenomena dirty reads, 383–384 nonrepeatable reads, 384 phantom reads, 384–385 data types assigning, 62 Boolean, 61 changing values of, 244–245 constructed, 54 datetime, 58–59 versus domains, inheritance by view columns, 105 interval, 60–61 numeric, 57–58 predefined, 54, 62 specifying for columns, 54–55 in SQL/XML, 437–439 for strings, 55–57 user-defined, 55, 63–64 using, 62–63 database models hierarchical, 4–5 network, relational, database objects See also objects; schema objects accessing, 128–130 granting privileges on, 130 organization of, 44 databases adding views to, 119–120 concept of, 37–39 connecting to, 25–26 creating, 44–45 defined, dropping views from, 117 date, retrieving, 237 DATE data types description of, 59 mapping to XML schema types, 442 datetime data types descriptions of, 58–59 versus interval, 60 www.it-ebooks.info datetime value functions, availability of, 236 datetime values, conversion of, 245 day-time interval, 60 DCL (Data Control Language), 19 DDL (Data Definition Language), 18 DDL statements SQL keywords for, 18 using in transactions, 390 DECIMAL data type, 58 DECIMAL data types, mapping to XML schema types, 442 DECLARE CURSOR statement cursor holdability part of, 358 cursor ordering part of, 359 cursor returnability part of, 358–359 cursor scrollability part of, 357 cursor sensitivity part of, 357 cursor updatability part of, 359–360 using, 354–356, 361 DECLARE statement with variables, 311–312 declared local temporary tables, 51 DEFAULT CHARACTER SET keywords, 43 default collation, 36 DEFAULT keyword with columns, 64–65 DEFERRED keyword with constraints, 392 DELETE FROM clause, 186 DELETE privilege with schema objects, 129 DELETE statement adding WHERE clause to, 198 for cursors, 359, 370–371 for data, 69, 291 versus DROP TABLE, 187 for procedures, 310 for rows, 190 for triggers, 343–344 using, 186–187 delete triggers See also triggers creating, 343–344, 346 dropping, 347 deleting columns, 71 data from referenced columns, 89–91 data from tables, 69 roles, 130–131, 140 schema objects, 43 schemas, 34, 43–44 tables, 69–71, 499 delimited identifiers, conventions for, 40–41 523 524 SQL: A Beginner’s Guide denormalizing data, 11 placeholder, 148–149 See also columns derived tables See also tables description of, 50 versus persistent base tables, 104 DESC keyword with ON_HAND column, 167 with ORDER BY, 166 determinant, defined, See also unique identifier diagnostics size, specifying for transactions, 387–388 direct invocation method advantage of, 405 with cursors, 372 defined, 352 support for, 22, 25 using, 19–20, 404 dirty reads, occurrence of, 383–384, 387 DISC_ARTISTS table, querying, 286–288 DISC_INVENTORY table, querying, 286–288 DISC_TYPES table, querying, 286–288 DISTINCT keyword with COUNT function, 228 with joins, 260 performance considerations, 161 with SELECT clause, 147–148, 151–152 distinct versus structured types, 63 division (/) operator, example of, 239 DML (Data Manipulation Language), 19 DML statements in transactions, 390 DOCUMENT type modifier in SQL/XML, 438 domain constraints creating, 98 defined, 74 limitation of, 75 domain data types, mapping to XML schema types, 442 domains creating, 98 defined, description of, 35, 37 DOUBLE PRECISION data types description of, 58 mapping to XML schema types, 442 DRG Records adding to CD_LABELS table, 188 deleting from CD_LABELS table, 190 DROP [COLUMN] clause, 68 DROP FUNCTION statement, 311, 324 DROP PROCEDURE statement, 311, 319 DROP ROLE statement, 131, 140 DROP SCHEMA statement, 34, 43–44 DROP TABLE statement versus DELETE, 69, 187 for triggers, 347 using, 50, 119 DROP TRIGGER statement, 335, 341, 347 DROP VIEW statement, 117 duplicate values, preventing, 79 durable quality, applying to transactions, 379 E ELSE clause with CASE, 242 ELSE IF clause in conditional statements, 316 ELSE keyword in conditional statements, 314–316 ELSEIF clause in conditional statements, 316 embedded SELECT statement, 415–416 See also SELECT statements embedded SQL creating statements in, 407–408 description of, 20, 352 statements, 512 support for, 406–407 using, 414–416 EMP_COMM view columns in, 115 removing, 117 EMPLOYEE_COMMISSIONS table creating, 114–115 extracting data from, 116 EMPLOYEES table, self-joining, 260–261 END IF keyword with loops, 317 END keyword with CASE, 242 with compound statements, 313–314 END LOOP keyword, 316–317 endpoints in ranges, 97 entity, defined, environment handle in CLI, 421–422 establishing in CLI, 427 www.it-ebooks.info equal to (=) operator with equi-joins, 256 example of, 195 with WHERE clause, 196 equi-join creating, 256 defining in condition join, 263, 272 error handling, providing, 413–415 EXCEPT operator, effect of, 272 EXEC SQL example of, 409–410 meaning of, 355, 408 ExecDirect() function, 423–424 Execute() function in CLI, 424–425, 428 EXECUTE privilege with schema objects, 130 execution context, applying to triggers, 331–332 execution methods CLI (call-level interface), 20 direct invocation, 19 embedded SQL, 20 module binding, 20 EXISTS predicate description of, 213 with subqueries, 215, 220, 281 using, 284–285, 292 EXIT keyword with loops, 316 Extensible Markup Language (XML) adding comments in, 436 overview of, 434–437 F false, evaluating rows to, 157 placeholder, 364 FETCH statement and cursor scrollability, 357 with cursors, 354–355, 361, 364–367, 373 first normal form, 8–9 FIRST option with FETCH statement, 365 [FK], meaning of, 91 flags, null values as, 76 FLOAT data types description of, 58 mapping to XML schema types, 442 floating-point number, defined, 57 FOR EACH clause with delete triggers, 344 with insert triggers, 337 with triggers, 333, 335, 340 Index FOR keyword with cursors, 356 FOR parameter with SUBSTRING, 234–235 FOR READ ONLY clause with cursors, 361 FOR UPDATE clause with cursors, 362–363 FOREIGN KEY constraints adding as column constraints, 85 guidelines for, 84 MATCH clause of, 88–89 versus primary keys, 87–88 clause of, 89–91 foreign keys in COMPACT_DISC_TYPES table, 94 in COMPACT_DISCS table, 93 creating as table constraints, 86 for joining tables, 108 multiple columns in, 86–87 representing as [FK], 91 forests in XML, 437 Fortran SQL statements in, 408 FROM clause evaluation of, 147 in REVOKE statement, 136 with SELECT clause, 149, 152–153 with views, 110–111 FROM parameter with SUBSTRING, 234–235 FULL option with MATCH clause, 89 full outer join defining, 268–269 description of, 266 versus UNION operator, 271 fully qualified names, 41–42 functional dependence, defined, functions See also set functions; userdefined functions; value functions calling in CLI (call-level interface), 419 capabilities of, 36 with CLI routines, 419 concept of, 301 creating, 303, 322 defined, 63, 226 dropping, 324 invoking, 322–323 versus procedures, 301–302, 321 and SQL-invoked routines, 300 syntax, 301–303 using, 247–249, 504–505 using in SQL/XML, 439–440 G GET_CD_ARTISTS procedure calling, 320 dropping, 319 getdate function, 237 global temporary tables overview, 51 GOTO action with WHENEVER, 413–414 GRANT clause, options for, 132 GRANT OPERATION FOR clause, 135 GRANT OPTION FOR clause for revoking privileges, 135, 137 for revoking roles, 138 GRANT statement versus FROM clause, 136 example of, 134 with objects, 130 with roles, 137 using, 132 GRANTED BY clause with REVOKE statement, 135 using, 132, 138 greater than (>) operator, 195, 197 greater than or equal to (>=) operator, 195, 198–199 GROUP BY clause with HAVING clause, 166 performance considerations, 161 with query results, 159–164 with set functions, 230–231, 247 with WHERE clause, 162 group search conditions, specifying, 164–166 grouping elements, 51 placeholder, 159–160 GUI interface, 22 H HAVING clause adding to SELECT statement, 171–172 with GROUP BY, 166 including columns in, 165 subqueries in, 279 using, 165–166 versus WHERE, 164–165 hierarchical data model, 4–5 host language, embedded SQL in, 407–408 www.it-ebooks.info host variables See also variables binding query results to, 428 in CLI (call-level interface), 424–425 creating, 414–415 declaration of, 422 defined, 364 indicators, 412 as parameter, 368 in SQL statements, 408–410 hyphens ( )with INSERT statements, 170 I identifiers See also authorization identifiers case of, 41 conventions for, 40 qualification of, 41–42 IF statement with loops, 317 impedance mismatch defined, 352–353 elimination of, 405 occurrence of, 364 using cursors for, 411 IN keyword avoiding use of, 322 using, 212 IN predicate defining lists with, 210–211 description of, 209 with subqueries, 284 subquery in, 211–212 updating data with, 290 using, 220, 279–280 IN_STOCK values averaging, 185 basing rows on, 156–157 returning, 158–159 indexing, support for, 70 indicator host variables, declaring, 412, 415 INFORMATION_SCHEMA, contents of, 34 inner joins creating, 264–266, 273 versus outer join, 264 input parameters See also output parameters; parameters adding to procedures, 307–310 using, 307 INSENSITIVE option with cursors, 357, 362, 373 INSERT INTO clause specifying columns in, 180 using, 176–177 525 526 SQL: A Beginner’s Guide INSERT privileges granting on COMPACT_ DISCS table, 130–140 granting to roles, 134 revoking, 137 with schema objects, 129 INSERT statements with datetime functions, 237–238 with delete triggers, 344 executing, 180 with insert triggers, 337–338 with procedures, 310 with SELECT statement, 181–182 syntax, 176 using, 169–170, 178, 288–289 VALUES clause in, 177 INSERT trigger, defining, 336 insert triggers, creating, 336–338, 345 See also triggers INSERT_LOG trigger, creating, 336–337 INT data type basing domain on, 98 with procedures, 312 INTEGER data types description of, 58 mapping to XML schema types, 442 integers, generation of, 36 integrity constraints See also constraints categories of, 74–75 defined, 74 interactive SQL See direct invocation method INTERSECT operator versus UNION, 272 interval data types mapping to XML schema types, 442 using, 60–61 INTO clause with embedded SELECT, 415–416 with indicator variables, 412 with singleton SELECT, 411 INVENTORY database connecting to, 66, 70 creating, 45 creating roles in, 139–140 data model for, 92 querying, 169–172, 501–502 re-creating, 514–518 tables in, 71 INVENTORY schema, creating, 43 INVENTORY table, columns in, 156–157 IS FALSE operator, 156 IS keyword with cursors, 356 with NULL predicate, 201 IS TRUE operator, 156 isolated quality, applying to transactions, 379 ISOLATION LEVEL keyword, 388 isolation levels data anomalies for, 387 and dirty reads, 383–384 READ COMMITTED, 386, 388 READ UNCOMMITTED, 385–386, 388 REPEATABLE READ, 386 SERIALIZABLE, 386–388, 399 setting for transactions, 382–383 J JOIN keyword, 264 joins avoiding duplicate rows in, 260 comma-separated, 254, 257–259, 272 condition join, 263–264, 273 versus correlated subqueries, 286 creating with multiple tables, 258–259 cross-join, 259 equi-join, 256 full outer join, 266, 268–269, 273 inner join, 264–266, 273 named column join, 263 natural, 262–263 outer join, 266–269 self-join, 260–261 theta-join, 257 types of, 263 with views, 108 K keys in normalization, keywords AUTHORIZATION, 42 with DDL statements, 18 DEFAULT CHARACTER SET, 43 www.it-ebooks.info defined, 40 nonreserved, 494–496 reserved, 492–494 L LAMP (Linux, Apache, MySQL, PHP), 25 LANGUAGE clause, 417 LAST option with FETCH statement, 365 LEAVE keyword with loops, 316 left outer join creating, 267–268 description of, 266 less than (

Ngày đăng: 20/02/2014, 11:20

TỪ KHÓA LIÊN QUAN

w