Presentation is loading. Please wait.

Presentation is loading. Please wait.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.

Similar presentations


Presentation on theme: "DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2."— Presentation transcript:

1 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2

2 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-2 Agenda Questions? Assignment 1 is posted in WebCt –Due Jan 26 (Thursday) at beginning of Class Finish Introduction To Database Processing Begin Intro to SQL

3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-3 David M. Kroenke’s Chapter One: Introduction Part Two Database Processing: Fundamentals, Design, and Implementation

4 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-4 Three Types of Database Design

5 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-5 Database Design from Existing Data

6 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-6 Data Import: One or Two Tables? This is an important decision, and based on a set of rules known as normalization (which is covered in Chapter Three)

7 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-7 Database Design from New Systems Development Entity-Relationship data modeling is covered in Chapter Five, and data model transformations to database designs are covered in Chapter Six

8 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-8 Database Design from Database Redesign Database redesign is covered in Chapter Eight, after coverage of DDL SQL in Chapter Seven

9 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-9 What You Need to Learn

10 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-10 Knowledge Priorities

11 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-11 A Brief History of Database Processing

12 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-12 The Relational Database Model The dominant database model is the relational database model – all current major DBMS products are based on it Created by IBM engineer E. F. Codd in 1970 It was based on mathematics called relational algebra This text examines and explains the relational database model

13 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-13 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter One – Part Two

14 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-14 David M. Kroenke’s Chapter Two: Introduction to Structured Query Language Part One Database Processing: Fundamentals, Design, and Implementation

15 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-15 Structured Query Language Structured Query Language (SQL) was developed by the IBM Corporation in the late 1970s. SQL was endorsed as a United States national standard by the American National Standards Institute (ANSI) in 1992 [SQL-92]. A newer version [SQL3] exists and incorporates some object-oriented concepts, but is not widely used in commercial DBMS products.

16 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-16 SQL as a Data Sublanguage SQL is not a full featured programming language as are C, C#, and Java. SQL is a data sublanguage for creating and processing database data and metadata. SQL is ubiquitous in enterprise-class DBMS products. SQL programming is a critical skill.

17 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-17 SQL DDL and DML SQL statements can be divided into two categories: –Data definition language (DDL) statements Used for creating tables, relationships, and other structures. Covered in Chapter Seven. –Data manipulation language (DML) statements. Used for queries and data modification Covered in this chapter (Chapter Two)

18 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-18 Cape Codd Outdoor Sports Cape Codd Outdoor Sports is a fictitious company based on an actual outdoor retail equipment vendor. Cape Codd Outdoor Sports: –Has 15 retail stores in the United States and Canada. –Has an on-line Internet store. –Has a (postal) mail order department. All retail sales recorded in an Oracle database.

19 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-19 Cape Codd Retail Sales Structure

20 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-20 Accesing the Cape Cood Database Download an MS access version from WebCT under Database section (chapter_2.mdb) Use the SQL Server LittleBlack –Database CapeCod –You all have read access

21 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-21 Cape Codd Retail Sales Data Extraction The Cape Codd marketing department needs an analysis of in-store sales. The entire database is not needed for this, only an extraction of retail sales data. The data is extracted by the IS department from the operational database into a separate, off-line database for use by the marketing department. Three tables are used: RETAIL_ORDER, ORDER_ITEM, and SKU_DATA (SKU = Stock Keeping Unit). The extracted data is converted as necessary: –Into a different DBMS  MS SQL Server –Into different columns  OrderDate becomes OrderMonth and OrderYear

22 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-22 Extracted Retail Sales Data Format

23 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-23 Retail Sales Extract Tables [in MS SQL Server]

24 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-24 Running SQL statements For MS access

25 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-25 Running SQL statements For MS access

26 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-26 Running SQL statements For MS access

27 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-27 Running SQL statements For MS access

28 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-28 Running SQL statements For SQL Server

29 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-29 Running SQL statements For SQL Server

30 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-30 Running SQL statements For SQL Server

31 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-31 Running SQL statements For SQL Server

32 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-32 The SQL SELECT Statement The fundamental framework for SQL query states is the SQL SELECT statement: –SELECT{ColumnName(s)} –FROM{TableName(s)} –WHERE{Conditions} All SQL statements end with a semi-colon (;). Select statements return a temporary database table

33 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-33 Specific Columns on One Table SELECTDepartment, Buyer FROMSKU_DATA;

34 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-34 Specifying Column Order SELECTBuyer, Department FROMSKU_DATA;

35 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-35 The DISTINCT Keyword SELECTDISTINCT Buyer, Department FROMSKU_DATA;

36 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-36 Selecting All Columns: The Asterisk (*) Keyword SELECT* FROMSKU_DATA;

37 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-37 Specific Rows from One Table SELECT* FROMSKU_DATA WHEREDepartment = 'Water Sports'; NOTE:SQL wants a plain ASCII single quote: ' NOT “ !

38 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-38 Specific Columns and Rows from One Table SELECTSKU_Description, Buyer FROMSKU_DATA WHEREDepartment = 'Climbing';

39 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-39 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter Two Part One

40 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-40 David M. Kroenke’s Chapter Two: Introduction to Structured Query Language Part Two Database Processing: Fundamentals, Design, and Implementation

41 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-41 Using MS Access - Results

42 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-42 Using MS SQL Server [SQL Query Analyzer]

43 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-43 Using Oracle [SQL*Plus]

44 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-44 Using Oracle [Quest Software’s TOAD]Quest Software’s TOAD

45 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-45 Using MySQL [MySQL Command Line Client]

46 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-46 Using MySQL [MySQL Query Browser]MySQL Query Browser

47 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-47 Sorting the Results: ORDER BY SELECT* FROMORDER_ITEM ORDER BYOrderNumber, Price;

48 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-48 Sort Order: Ascending and Descending SELECT* FROMORDER_ITEM ORDER BYPrice DESC, OrderNumber ASC; NOTE: The default sort order is ASC – does not have to be specified.

49 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-49 WHERE Clause Options: AND SELECT* FROMSKU_DATA WHEREDepartment = 'Water Sports' ANDBuyer = 'Nancy Meyers';

50 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-50 WHERE Clause Options: OR SELECT* FROMSKU_DATA WHEREDepartment = 'Camping' ORDepartment = 'Climbing';

51 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-51 WHERE Clause Options:- IN SELECT* FROMSKU_DATA WHEREBuyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin');

52 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-52 WHERE Clause Options: NOT IN SELECT* FROMSKU_DATA WHEREBuyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin');

53 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-53 WHERE Clause Options: Ranges with BETWEEN SELECT* FROMORDER_ITEM WHEREExtendedPrice BETWEEN 100 AND 200;

54 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-54 WHERE Clause Options: Ranges with Math Symbols SELECT* FROMORDER_ITEM WHEREExtendedPrice >= 100 AND ExtendedPrice <= 200;

55 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-55 WHERE Clause Options: LIKE and Wildcards The SQL keyword LIKE can be combined with wildcard symbols: –SQL 92 Standard (SQL Server, Oracle, etc.): _ = Exactly one character % = Any set of one or more characters –MS Access (based on MS DOS) ? = Exactly one character * = Any set of one or more characters

56 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-56 WHERE Clause Options: LIKE and Wildcards (Continued) SELECT* FROMSKU_DATA WHEREBuyer LIKE 'Pete%'; MS ACCESS Buyer LIKE ‘Pete*’;

57 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-57 WHERE Clause Options: LIKE and Wildcards (Continued) SELECT* FROMSKU_DATA WHERESKU_Description LIKE '%Tent%';

58 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-58 WHERE Clause Options: LIKE and Wildcards SELECT* FROMSKU_DATA WHERESKU LIKE '%2_ _'; MS ACCESS SKU LIKE ‘*2??’;

59 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-59 SQL Built-in Functions There are five SQL Built-in Functions: –COUNT –SUM –AVG –MIN –MAX

60 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-60 SQL Built-in Functions (Continued) SELECTSUM (ExtendedPrice) ASOrder3000Sum FROMORDER_ITEM WHEREOrderNumber = 3000;

61 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-61 SQL Built-in Functions (Continued) SELECTSUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax FROMORDER_ITEM;

62 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-62 SQL Built-in Functions (Continued) SELECTCOUNT(*) AS NumRows FROMORDER_ITEM;

63 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-63 SQL Built-in Functions (Continued) SELECTCOUNT (DISTINCT Department) AS DeptCount FROMSKU_DATA;

64 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-64 Arithmetic in SELECT Statements SELECTQuantity * Price AS EP, ExtendedPrice FROMORDER_ITEM;

65 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-65 String Functions in SELECT Statements SELECTDISTINCT RTRIM (Buyer) + ' in ' + RTRIM (Department) AS Sponsor FROMSKU_DATA;

66 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-66 The SQL keyword GROUP BY SELECTDepartment, Buyer, COUNT(*) AS Dept_Buyer_SKU_Count FROMSKU_DATA GROUP BY Department, Buyer;

67 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-67 The SQL keyword GROUP BY (Continued) In general, place WHERE before GROUP BY. Some DBMS products do not require that placement, but to be safe, always put WHERE before GROUP BY. The HAVING operator restricts the groups that are presented in the result. There is an ambiguity in statements that include both WHERE and HAVING clauses. The results can vary, so to eliminate this ambiguity SQL always applies WHERE before HAVING.

68 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-68 The SQL keyword GROUP BY (Continued) SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> 302000 GROUP BY Department ORDER BY Dept_SKU_Count;

69 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-69 The SQL keyword GROUP BY (Continued) SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> 302000 GROUP BY Department HAVING COUNT (*) > 1 ORDER BYDept_SKU_Count;

70 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-70 Querying Multiple Tables: Subqueries SELECTSUM (ExtendedPrice) AS Revenue FROMORDER_ITEM WHERESKU IN (SELECTSKU FROMSKU_DATA WHERE Department = 'Water Sports'); Note: The second SELECT statement is a subquery.

71 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-71 Querying Multiple Tables: Subqueries (Continued) SELECTBuyer FROMSKU_DATA WHERESKU IN (SELECTSKU FROMORDER_ITEM WHEREOrderNumber IN (SELECTOrderNumber FROMRETAIL_ORDER WHEREOrderMonth = 'January' ANDOrderYear = 2004));

72 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-72 Querying Multiple Tables: Joins SELECTBuyer, ExtendedPrice FROMSKU_DATA, ORDER_ITEM WHERESKU_DATA.SKU = ORDER_ITEM.SKU;

73 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-73 Querying Multiple Tables: Joins (Continued) SELECTBuyer, SUM(ExtendedPrice) AS BuyerRevenue FROMSKU_DATA, ORDER_ITEM WHERESKU_DATA.SKU = ORDER_ITEM.SKU GROUP BYBuyer ORDER BYBuyerRevenue DESC;

74 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-74 Querying Multiple Tables: Joins (Continued) SELECTBuyer, ExtendedPrice, OrderMonth FROMSKU_DATA, ORDER_ITEM, RETAIL_ORDER WHERESKU_DATA.SKU = ORDER_ITEM.SKU ANDORDER_ITEM.OrderNumber = RETAIL_ORDER.OrderNumber;

75 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-75 Subqueries versus Joins Subqueries and joins both process multiple tables. A subquery can only be used to retrieve data from the top table. A join can be used to obtain data from any number of tables, including the “top table” of the subquery. In Chapter 7, we will study the correlated subquery. That kind of subquery can do work that is not possible with joins.

76 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-76 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter Two Part Two


Download ppt "DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2."

Similar presentations


Ads by Google