www.freebookslides.com Database Processing Fundamentals, Design, and Implementation The editorial team at Pearson has worked closely with educators around the globe to inform students of the ever-changing world in a broad variety of disciplines Pearson Education offers this product to the international market, which may or may not include alterations from the United States version INTERNATIONAL EDITION INTERNATIONAL EDITION THIRTEENTH EDITION Kroenke Auer This is a special edition of an established title widely used by colleges and universities throughout the world Pearson published this exclusive edition for the benefit of students outside the United States and Canada If you purchased this book within the United States or Canada you should be aware that it has been imported without the approval of the Publisher or Author Pearson International Edition INTERNATIONAL EDITION Database Processing Fundamentals, Design, and Implementation THIRTEENTH EDITION David M Kroenke • David J Auer www.freebookslides.com Database Processing Fundamentals, Design, and Implementation EDITION 13 A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com Database Processing E it d Fundamentals, Design, and Implementation ion 13 David M Kroenke David J Auer Western Washington University International Edition contributions by Arup Kumar Bhattacharjee RCC Institute of Information Technology, Kolkata Soumen Mukherjee RCC Institute of Information Technology, Kolkata Boston Columbus Indianapolis New York San Francisco Upper Saddle River Amsterdam Cape Town Dubai London Madrid Milan Munich Paris Montréal Toronto Delhi Mexico City São Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com Editor in Chief: Stephanie Wall Executive Editor: Bob Horan Program Management Lead: Ashley Santora Program Manager: Kelly Loftus Editorial Assistant: Kaylee Rotella Director of Marketing: Maggie Moylan Executive Marketing Manager: Anne Fahlgren Marketing Assistant: Gianna Sandri Project Management Lead: Judy Leale Publishing Operations Director, International Edition: Angshuman Chakraborty Manager, Publishing Operations, International Edition: Shokhi Shah Khandelwal Associate Print & Media Editor, International Edition: Anuprova Dey Chowdhuri Acquisitions Editor, International Edition: Shivangi Ramachandran Publishing Administrator, International Edition: Hema Mehta Project Editor, International Edition: Karthik Subramanian Senior Manufacturing Controller, Production, International Edition: Trudy Kimber Production Project Manager: Jane Bonnell Operations Specialist: Michelle Klein Senior Art Director: Janet Slowik Interior Designer: Karen Quigley Cover Designer: Jodi Notowitz Cover Image: Itana/Shutterstock.com Media Project Manager, Editorial: Denise Vaughn Media Project Manager, Production: Lisa Rinaldi Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsoninternationaleditions.com © Pearson Education Limited 2014 The rights of David M Kroenke and David J Auer to be identified as authors of this work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988 Authorized adaptation from the United States edition, entitled Database Processing: Fundamentals, Design, and Implementation, 13th Edition, ISBN 978-0-13-305835-2, by David M Kroenke and David J Auer, published by Pearson Education © 2014 All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby Street, London EC1N 8TS All trademarks used herein are the property of their respective owners The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in the documents and related graphics published as part of the services for any purpose All such documents and related graphics are provided “as is” without warranty of any kind Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and non-infringement In no event shall Microsoft and/or its respective suppliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of information available from the services The documents and related graphics contained herein could include technical inaccuracies or typographical errors Changes are periodically added to the information herein Microsoft and/or its respective suppliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time Partial screen shots may be viewed in full within the software version specified Microsoft- Windows-, and Microsoft Office- are registered trademarks of the Microsoft Corporation in the U.S.A and other countries This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation ISBN 10: 1-292-00486-X ISBN 13: 978-1-292-00486-0 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library 10 9 8 7 6 5 4 3 2 1 14 13 12 11 10 Typeset in Kepler Std-Light by Integra Printed and bound by Courier Kendallville in The United States of America A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com Brief Contents Part 1 ● Getting Started Chapter Chapter 33 Introduction 34 Introduction to Structured Query Language 65 Part 2 ● Database Design Chapter Chapter Chapter Chapter 149 The Relational Model and Normalization 150 Database Design Using Normalization 192 Data Modeling with the Entity-Relationship Model 211 Transforming Data Models into Database Designs 262 Part 3 ● Database Implementation Chapter Chapter 309 SQL for Database Construction and Application Processing 310 Database Redesign 398 Part 4 ● Multiuser Database Processing Chapter Chapter 10 425 Managing Multiuser Databases 426 Managing Databases with SQL Server 2012, Oracle Database 11g Release 2, and MySQL 5.6 460 Chapter 10A Managing Databases with SQL Server 2012 Online Chapter: See Page 465 for Instructions Chapter 10B Managing Databases with Oracle Database 11g Release 2 Chapter 10C Managing Databases with MySQL 5.6 Online Chapter: See Page 465 for Instructions Online Chapter: See Page 465 for Instructions Part 5 ● Database Access Standards Chapter 11 Chapter 12 The Web Server Environment 468 Big Data, Data Warehouses, and Business Intelligence Systems 566 Appendix A Appendix B Appendix C Appendix D Appendix E Appendix F Appendix G Appendix H Appendix I Appendix J Getting Started with Microsoft Access 2013 Getting Started with Systems Analysis and Design E-R Diagrams and the IDEF1X Standard E-R Diagrams and the UML Standard Getting Started with MySQL Workbench Data Modeling Tools Getting Started with Microsoft Visio 2013 Data Structures for Database Processing The Semantic Object Model Getting Started with Web Servers, PHP, and the Eclipse PDT Business Intelligence Systems 467 Online Appendices: See Page 612 for Instructions A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com Contents Preface 19 Part 1 ● Getting Started 33 Chapter 1: Introduction 34 Chapter Objectives 34 The Characteristics of Databases 35 A Note on Naming Conventions 36 • A Database Has Data and Relationships 36 • Databases Create Information 37 Database Examples 38 Single-User Database Applications 38 • Multiuser Database Applications 38 • E-Commerce Database Applications 39 • Reporting and Data Mining Database Applications 39 The Components of a Database System 40 Database Applications and SQL 41 • The DBMS 43 • The Database 44 Personal Versus Enterprise-Class Database Systems 46 What Is Microsoft Access? 46 • What Is an Enterprise-Class Database System? 47 Database Design 49 Database Design from Existing Data 49 • Database Design for New Systems Development 50 • Database Redesign 51 What You Need to Learn 52 A Brief History of Database Processing 53 The Early Years 53 • The Emergence and Dominance of the Relational Model 55 • Post-Relational Developments 56 Summary 58 • Key Terms 59 • Review Questions 59 • Project Questions 61 Chapter 2: Introduction to Structured Query Language 65 Chapter Objectives 65 Components of a Data Warehouse 66 Cape Codd Outdoor Sports 67 The Extracted Retail Sales Data 67 • RETAIL_ORDER Data 68 • ORDER_ITEM Data 69 • SKU_DATA Table 70 • The Complete Cape Codd Data Extract Schema 70 • Data Extracts Are Common 71 SQL Background 71 The SQL SELECT/FROM/WHERE Framework 72 Reading Specified Columns from a Single Table 72 • Specifying Column Order in SQL Queries from a Single Table 73 • Reading Specified Rows from a Single Table 75 • Reading Specified Columns and Rows from a Single Table 76 Submitting SQL Statements to the DBMS 77 Using SQL in Microsoft Access 2013 77 • Using SQL in Microsoft SQL Server 2012 82 • Using SQL in Oracle Database 11g Release 2 85 • Using SQL in Oracle MySQL 5.6 87 A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com 8 Contents SQL Enhancements for Querying a Single Table 90 Sorting the SQL Query Results 90 • SQL WHERE Clause Options 92 • Combing the SQL WHERE Clause and the SQL ORDER BY Clause 97 Performing Calculations in SQL Queries 97 Using SQL Built-in Functions 97 • SQL Expressions in SQL SELECT Statements 100 Grouping in SQL SELECT Statements 102 Looking for Patterns in NASDAQ Trading 106 Investigating the Characteristics of the Data 106 • Searching for Patterns in Trading by Day of Week 107 Querying Two or More Tables with SQL 109 Querying Multiple Tables with Subqueries 109 • Querying Multiple Tables with Joins 112 • Comparing Subqueries and Joins 117 •The SQL JOIN ON Syntax 117 • Outer Joins 119 Summary 123 • Key Terms 123 • Review Questions 124 • Project Questions 129 • Case Questions 133 • The Queen Anne Curiosity Shop 138 • Morgan Importing 145 Part 2 ● Database Design 149 Chapter 3: The Relational Model and Normalization 150 Chapter Objectives 150 Relational Model Terminology 152 Relations 152 • Characteristics of Relations 153 • Alternative Terminology 155 • Functional Dependencies 156 • Finding Functional Dependencies 158 • Keys 161 Normal Forms 163 Modification Anomalies 163 • A Short History of Normal Forms 164 • Normalization Categories 165 • From First Normal Form to Boyce-Codd Normal Form Step by Step 166 • Eliminating Anomalies from Functional Dependencies with BCNF 169 • Eliminating Anomalies from Multivalued Dependencies 179 • Fifth Normal Form 183 • Domain/Key Normal Form 183 Summary 183 • Key Terms 184 • Review Questions 185 • Project Questions 187 • Case Questions 188 • The Queen Anne Curiosity Shop 189 • Morgan Importing 191 Chapter 4: Database Design Using Normalization 192 Chapter Objectives 192 Assess Table Structure 193 Designing Updatable Databases 194 Advantages and Disadvantages of Normalization 194 • Functional Dependencies 195 • Normalizing with SQL 195 • Choosing Not to Use BCNF 196 • Multivalued Dependencies 197 Designing Read-Only Databases 197 Denormalization 198 • Customized Duplicated Tables 199 Common Design Problems 199 The Multivalue, Multicolumn Problem 200 • Inconsistent Values 202 • Missing Values 203 • The General-Purpose Remarks Column 204 Summary 205 • Key Terms 205 • Review Questions 206 • Project Questions 208 • Case Questions 208 • The Queen Anne Curiosity Shop 209 • Morgan Importing 210 A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com Contents Chapter 5: Data Modeling with the Entity-Relationship Model 211 Chapter Objectives 211 The Purpose of a Data Model 212 The Entity-Relationship Model 212 Entities 213 • Attributes 213 • Identifiers 213 • Relationships 214 • Maximum Cardinality 216 • Minimum Cardinality 217 • EntityRelationship Diagrams and Their Versions 218 • Variations of the E-R Model 218 • E-R Diagrams Using the IE Crow’s Foot Model 219 • Strong Entities and Weak Entities 221 • ID-Dependent Entities 221 • Non-ID-Dependent Weak Entities 222 • The Ambiguity of the Weak Entity 223 • Subtype Entities 223 Patterns in Forms, Reports, and E-R Models 225 Strong Entity Patterns 226 • ID-Dependent Relationships 228 • Mixed Identifying and Nonidentifying Patterns 235 • The For-Use-By Pattern 237 • Recursive Patterns 239 The Data Modeling Process 241 The College Report 242 • The Department Report 243 • The Department/Major Report 245 • The Student Acceptance Letter 246 Summary 248 • Key Terms 249 • Review Questions 249 • Project Questions 251 • Case Questions 257 • The Queen Anne Curiosity Shop 260 • Morgan Importing 261 Chapter 6: Transforming Data Models into Database Designs 262 Chapter Objectives 262 The Purpose of a Database Design 263 Create a Table for Each Entity 263 Selecting the Primary Key 264 • Specifying Candidate (Alternate) Keys 265 • Specify Column Properties 266 • Verify Normalization 268 Create Relationships 268 Relationships Between Strong Entities 269 • Relationships Using IDDependent Entities 272 • Relationships with a Weak Non-ID-Dependent Entity 277 • Relationships in Mixed Entity Designs 277 • Relationships Between Supertype and Subtype Entities 279 • Recursive Relationships 279 • Representing Ternary and Higher-Order Relationships 281 • Relational Representation of the Highline University Data Model 284 Design for Minimum Cardinality 285 Actions When the Parent Is Required 287 • Actions When the Child Is Required 288 • Implementing Actions for M-O Relationships 288 • Implementing Actions for O-M Relationships 289 • Implementing Actions for M-M Relationships 290 • Designing Special Case M-M Relationships 290 • Documenting the Minimum Cardinality Design 291 • An Additional Complication 293 • Summary of Minimum Cardinality Design 293 The View Ridge Gallery Database 293 Summary of Requirements 293 • The View Ridge Gallery Data Model 294 • Database Design with Data Keys 295 • Minimum Cardinality Enforcement for Required Parents 296 • Minimum Cardinality Enforcement for the Required Child 297 • Column Properties for the View Ridge Gallery Database Design Tables 300 Summary 301 • Key Terms 302 • Review Questions 302 • Project Questions 304 • Case Questions 305 • The Queen Anne Curiosity Shop 307 • Morgan Importing 307 A01_KROE4860_13_PIE_FM.indd 05/09/13 8:40 PM www.freebookslides.com Chapter 2 Introduction to Structured Query Language 85 We recommend that you create a folder for each database in the Projects folder We have created a folder named DBC-e13-Cape-Codd-Database to store the script files associated with the Cape Codd database Saving a Microsoft SQL Server Query as an SQL Script in Microsoft SQL Server Management Studio Click the Save button shown in Figure 2-17 The Save File As dialog appears, as shown in Figure 2-17 Browse to the \My Documents\SQL Server Management Studio\Projects\DBP-e13-CapeCodd-Database folder Note that there are already two SQL script names displayed in the dialog box These are the SQL scripts that were used to create and populate the Cape Codd database tables, and they are available on our Web site at www.pearsoninternationaleditions com/kroenke In the File Name text box, type the SQL script file name SQL-Query-CH02-01 Click the Save button To rerun the saved query, you would click the Open File button shown in Figure 2-17 to open the Open File dialog box, open the query, and then click the Execute button At this point, you should work through each of the other nine queries in the preceding discussion of the SQL SELECT/FROM/WHERE framework Save each query as SQL-Query-CH02-##, where ## is a sequential number from 02 to 09 that corresponds to the SQL query label shown in the SQL comment line of each query Using SQL in Oracle Database 11g Release Before you can enter SQL statements into Oracle Database 11g Release you need access to a computer that has Oracle Database 11g Release installed and that has a database with the tables and data shown in Figures 2-3, 2-4, and 2-5 Your instructor may have installed Oracle Database 11g Release on a computer in the lab and entered the data for you If so, follow his or her instructions for accessing that database Otherwise, you will need to obtain a copy of Oracle Database 11g Release and install it on your computer Read the appropriate sections of Chapter 10B about obtaining and installing Oracle Database 11g Release After you have installed Oracle Database 11g Release 2, you will need to read the introductory discussion for Oracle Database 11g Release in Chapter 10B, starting on page 10B-1, and create the Cape Codd database Oracle Database 11g Release scripts for creating and populating the Cape Codd database tables are available on our Web site at www.pearsoninternationaleditions.com/kroenke Although Oracle users have been dedicated to the Oracle SQL*Plus command line tool, professionals are moving to the new Oracle SQL Developer GUI tool This application is installed as part of the Oracle Database 11g Release installation, and updated versions are available for free download at www.oracle.com/technology/software/products/sql/index.html We will use it as our standard GUI tool for managing the databases created by the Oracle Database DBMS Figure 2-18 shows the execution of SQL-Query-CH02-01 (note that the SQL comment is not included in the SQL statement as run—also note that the SQL comment could have been included in the SQL code if we had chosen to include it): /* *** SQL-Query-CH02-01 *** */ SELECT Department, Buyer FROM SKU_DATA; Running an SQL Query in Oracle SQL Developer Click the New Connection button and open the Cape Codd database In the tabbed SQL Worksheet, type the SQL SELECT command (without the SQL comment line shown above): SELECT Department, Buyer FROM SKU_DATA; as shown in Figure 2-18 M02_KROE4860_13_PIE_C02.indd 85 04/09/13 10:00 AM www.freebookslides.com 86 Part 1 Getting Started The SQL Worksheet Connections object browser shows connected databases The New Connection button The Cape Codd database The Cape Codd database tables The Execute button The SQL query in the SQL Worksheet The Results tabbed window Figure 2-18 Running an SQL Query in Oracle SQL Developer Click the Execute button to run the query The results are displayed in a results window, as shown in Figure 2-18 Note that in Figure 2-18, the Cape-Codd-Database object in the Object Browser in the left side Connection object browser of the Oracle SQL Developer has been expanded to show the tables in the Cape Codd database Many of the functions of SQL Developer are associated with the objects in the Connections object browser and are often accessed by right-clicking the object to display a shortcut menu We are using Oracle Database 11g Release running in Microsoft Server 2008 R2 When we give specific sequences of steps to follow in the text or figures in this book, we use the command terminology used by Oracle Database 11g Release and associated utility programs in Microsoft Server 2008 R2 If you are running a workstation operating system such as Microsoft XP, Microsoft Vista, or Linux, the terminology may vary somewhat By The Way Oracle Database 11g Release is an enterprise-class DBMS product and, as is typical of such products, does not store queries within the DBMS (it does store SQL Views, which can be considered a type of query, and we will discuss SQL Views later in this chapter) However, you can save queries as SQL script files An SQL script file is a separately stored plain text file, and it usually has a file name extension of *.sql An SQL script can be opened and run as an SQL command (or set of commands) Often used to create and populate databases, scripts can also be used to store a query or set of queries Figure 2-19 shows the SQL query being saved as an SQL script Note that in Figure 2-19 the SQL scripts are shown in a folder named {UserName}\ Documents\SQL Developer\DBP-e13-Cape-Codd-Database By default, Oracle SQL Developer stores *.sql files in an obscure location within its own application files We recommend that you create a subfolder in your My Documents folder named SQL Developer and then create a M02_KROE4860_13_PIE_C02.indd 86 04/09/13 10:00 AM www.freebookslides.com Chapter 2 Introduction to Structured Query Language 87 The Save button The Save dialog box The DBP-e13-CapeCodd-Database folder Existing SQL scripts— these were used to create and populate the Cape-Codd database The Documents Folder button The DBP-e13-CapeCodd-Database folder button Type the SQL script file name here The dialog box Save button Figure 2-19 Saving an Oracle SQL Query as an SQL Script in Oracle SQL Developer subfolder for each database in the SQL Developer folder We have created a folder named DBPe13-Cape-Codd-Database to store the script files associated with the Cape Codd database Saving an SQL Script in Oracle SQL Developer Click the Save button shown in Figure 2-19 The Save dialog appears, as shown in Figure 2-19 Click the Documents button on the Save dialog box to move to the Documents folder and then browse to the DBP-e13-Cape-Codd-Database folder Note that there are already two SQL script names displayed in the dialog box These are the SQL scripts that were used to create and populate the Cape Codd database tables, and they are available on our Web site at www.pearsoninternationaleditions com/kroenke In the File Name text box, type the SQL script file name SQL-Query-CH02-01.sql Click the Save button To rerun the saved query, you would click the SQL Developer Open File button to open the Open File dialog box, browse to the query file, open the query file, and then click the Execute button At this point, you should work through each of the other nine queries in the preceding discussion of the SQL SELECT/FROM/WHERE framework Save each query as SQLQuery-CH02-##, where ## is a sequential number from 02 to 09 that corresponds to the SQL query label shown in the SQL comment line of each query Using SQL in Oracle MySQL 5.6 Before you can use SQL statements with Oracle MySQL 5.6, you need access to a computer that has MySQL 5.6 installed and that has a database with the tables and data shown in Figure 2-4, 2-5, and 2-6 Your instructor may have installed MySQL 5.6 in your computer lab and entered the data for you If so, follow his or her instructions for accessing that database Otherwise, you will need to obtain a copy of MySQL Community Server 5.6 and install M02_KROE4860_13_PIE_C02.indd 87 04/09/13 10:00 AM www.freebookslides.com 88 Part 1 Getting Started The SQL Editor tabbed window The SQL Editor menu and toolbar The Object Browser The Cape Codd database The Cape Codd database tables The Query tabbed window—enter your SQL statement The Execute Current SQL Statement in Connected Server button The query results in the results tabbed window Figure 2-20 Running an SQL Query in the MySQL Workbench it on your computer Read the a ppropriate sections of Chapter 10C about obtaining and installing MySQL Community Server 5.6 After you have MySQL 5.6 installed, you will need to read the introductory discussion for MySQL 5.6 in Chapter 10C, starting on page 10C-1, and create the Cape Codd database MySQL scripts for creating and populating the Cape Codd database tables are available on our Web site at www.pearsoninternationaleditions.com/kroenke MySQL 5.6 uses the MySQL Workbench as the GUI tool for managing the MySQL 5.6 DBMS and the databases controlled by the DBMS This tool must be installed separately from the MySQL 5.6 DBMS and this is discussed in Chapter 10C SQL statements are created and run in the MySQL Workbench, and Figure 2-20 shows the execution of SQL-Query-CH02-01 (note that the SQL comment is not included in the SQL statement as run—also note that the SQL comment could have been included in the SQL code if we had chosen to include it): /* *** SQL-Query-CH02-01 *** */ SELECT Department, Buyer FROM SKU_DATA; Running an SQL Query in the MySQL Workbench To make the Cape Codd database the default schema (active database), right-click the cape_codd schema (database) object to display the shortcut menu and then click the Set as Default Schema command In the Query tabbed window in the SQL Editor tabbed window, type the SQL SELECT command (without the SQL comment line shown above): SELECT Department, Buyer FROM SKU_DATA; as shown in Figure 2-20 M02_KROE4860_13_PIE_C02.indd 88 04/09/13 10:00 AM www.freebookslides.com Chapter 2 Introduction to Structured Query Language 89 Click the Execute Current SQL Statement in Connected Server button to run the query The results are displayed in a tabbed Query Result window, shown as the Query Result window in Figure 2-20 (you can have more than one Query Result window open, and thus they need to be numbered) Note that in Figure 2-20 the Cape Codd database object in the Object Browser in the left-side window of the MySQL Workbench has been expanded to show the tables in the Cape Codd database Many of the functions of the MySQL Workbench are associated with the objects in the Object Browser and are often accessed by right-clicking the object to display a shortcut menu We are using MySQL Community Server 5.6 running in Microsoft Server 2012 When we give specific sequences of steps to follow in the text or figures in this book, we use the command terminology used for MySQL 5.6 and associated utility programs in Microsoft Server 2012 If you are running a workstation operating system such as Microsoft XP, Microsoft Vista, or Linux, the terminology may vary somewhat By The Way Figure 2-21 Saving a MySQL Query as an SQL Script in the MySQL Workbench MySQL 5.6 is an enterprise-class DBMS product and, as is typical of such products, does not store queries within the DBMS (it does store SQL Views, which can be considered a type of query, and we will discuss SQL Views later in this chapter) However, you can save MySQL queries as SQL script files An SQL script file is a separately stored plain text file, and it usually uses a file name extension of *.sql An SQL script file can be opened and run as an SQL command Figure 2-21 shows the SQL query being saved as an SQL script file Note that in Figure 2-21 the query will be saved in a folder named My Documents\MySQL Workbench\Schemas\DBP-e13-Cape-Codd-Database By default, MySQL Workbench stores files in the user’s My Documents folder We recommend that you create a subfolder in your My Documents folder named MySQL Workbench and then create subfolders labeled EER Models and Schemas Within each of these subfolders, create a sub-subfolder for each MySQL 5.6 data base We have created a folder named DBP-e13-Cape-Codd-Database to store the script files associated with the Cape Codd database Click the Save SQL Script to File button to open the Save SQL Script dialog box The Save SQL Script dialog box The My Documents/ MySQL Workbench/ Schemas/DBP-e13Cape-Codd-Database folder Type the SQL script File name here The Save button M02_KROE4860_13_PIE_C02.indd 89 04/09/13 10:00 AM www.freebookslides.com 90 Part 1 Getting Started Saving a MySQL 5.6 Query Click the Save SQL Script to File button, as shown in Figure 2-21 The Save Query to File dialog appears, as shown in Figure 2-21 Browse to the My Documents\MySQL Workbench\Schemas\DBP-e13-Cape-CoddDatabase folder In the File name text box, type the SQL query file name SQL-Query-CH02-01 Click the Save button To rerun the saved query, you would click the File | Open SQL Script menu command to open the Open SQL Script dialog box, then select and open the SQL query *.sql files, and, finally, click the Execute Current SQL Statement in Connected Server button At this point, you should work through each of the other nine queries in the preceding discussion of the SQL SELECT/FROM/WHERE framework Save each query as SQLQuery-CH02-##, where ## is a sequential number from 02 to 09 that corresponds to the SQL query label shown in the SQL comment line of each query SQL Enhancements for Querying a Single Table We started our discussion of SQL queries with SQL statements for processing a single table, and now we will add an additional SQL feature to those queries As we proceed, you will begin to see how powerful SQL can be for querying databases and for creating information from existing data The SQL results shown in this chapter were generated using Microsoft SQL Server 2012 Query results from other DBMS products will be similar, but may vary a bit By The Way Sorting the SQL Query Results The order of the rows produced by an SQL statement is arbitrary and determined by programs in the bowels of each DBMS If you want the DBMS to display the rows in a particular order, you can use the SQL ORDER BY clause For example, the SQL statement: /* *** SQL-Query-CH02-10 *** */ SELECT * FROM ORDER_ITEM ORDER BY OrderNumber; will generate the following results: M02_KROE4860_13_PIE_C02.indd 90 04/09/13 10:00 AM www.freebookslides.com Chapter 2 Introduction to Structured Query Language 91 We can sort by two columns by adding a second column name For example, to sort first by OrderNumber and then by Price within OrderNumber, we use the following SQL query: /* *** SQL-Query-CH02-11 *** */ SELECT * FROM ORDER_ITEM ORDER BY OrderNumber, Price; The result for this query is: If we want to sort the data by Price and then by OrderNumber, we would simply reverse the order of those columns in the ORDER BY clause as follows: /* *** SQL-Query-CH02-12 *** */ SELECT * FROM ORDER_ITEM ORDER BY Price, OrderNumber; with the results: Note to Microsoft Access users: Unlike the Microsoft SQL Server output shown here, Microsoft Access displays dollar signs in the output of urrency data c By The Way By default, rows are sorted in ascending order To sort in descending order, add the SQL DESC keyword after the column name Thus, to sort first by Price in descending order and then by OrderNumber in ascending order, we use the SQL query: /* *** SQL-Query-CH02-13 *** */ SELECT * FROM ORDER_ITEM ORDER BY Price DESC, OrderNumber ASC; M02_KROE4860_13_PIE_C02.indd 91 04/09/13 10:00 AM www.freebookslides.com 92 Part 1 Getting Started The result is: Because the default order is ascending, it is not necessary to specify ASC in the last SQL statement Thus, the following SQL statement is equivalent to the previous SQL query: /* *** SQL-Query-CH02-14 *** */ SELECT * FROM ORDER_ITEM ORDER BY Price DESC, OrderNumber; and produces the same results: SQL WHERE Clause Options SQL includes a number of SQL WHERE clause options that greatly expand SQL’s power and utility In this section, we consider three options: compound clauses, ranges, and wildcards Compound WHERE Clauses SQL WHERE clauses can include multiple conditions by using the SQL AND, OR, IN, and NOT IN operators For example, to find all of the rows in SKU_DATA that have a Department named Water Sports and a Buyer named Nancy Meyers, we can use the SQL AND operator in our query code: /* *** SQL-Query-CH02-15 *** */ SELECT * FROM SKU_DATA WHERE Department='Water Sports' Buyer='Nancy Meyers'; AND The results of this query are: M02_KROE4860_13_PIE_C02.indd 92 04/09/13 10:00 AM www.freebookslides.com Chapter 2 Introduction to Structured Query Language 93 Similarly, to find all of the rows of SKU_DATA for either the Camping or Climbing departments, we can use the SQL OR operator in the SQL query: /* *** SQL-Query-CH02-16 *** */ SELECT * FROM SKU_DATA WHERE Department='Camping' OR Department='Climbing'; which gives us the following results: Three or more AND and OR conditions can be combined, but in such cases the SQL IN operator and the SQL NOT IN operator are easier to use For example, suppose we want to obtain all of the rows in SKU_DATA for buyers Nancy Meyers, Cindy Lo, and Jerry Martin We could construct a WHERE clause with two ANDs, but an easier way to this is to use the IN operator, as illustrated in the SQL query: /* *** SQL-Query-CH02-17 *** */ SELECT * FROM SKU_DATA WHERE Buyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); In this format, a set of values is enclosed in parentheses A row is selected if Buyer is equal to any one of the values provided The result is: Similarly, if we want to find rows of SKU_DATA for which the buyer is someone other than Nancy Meyers, Cindy Lo, or Jerry Martin, we would use the SQL query: /* *** SQL-Query-CH02-18 *** */ SELECT * FROM SKU_DATA WHERE Buyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); The result is: M02_KROE4860_13_PIE_C02.indd 93 04/09/13 10:00 AM www.freebookslides.com 94 Part 1 Getting Started Observe an important difference between IN and NOT IN A row qualifies for an IN condition if the column is equal to any of the values in the parentheses However, a row qualifies for a NOT IN condition if it is not equal to all of the items in the parentheses Ranges in SQL WHERE Clauses SQL WHERE clauses can specify ranges of data values by using the SQL BETWEEN keyword For example, the following SQL statement: /* *** SQL-Query-CH02-19 *** */ SELECT * FROM ORDER_ITEM WHERE ExtendedPrice BETWEEN 100 AND 200; will produce the following results: Notice that both the ends of the range, 100 and 200, are included in the resulting table The preceding SQL statement is equivalent to the SQL query: /* *** SQL-Query-CH02-20 *** */ SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 ExtendedPrice