Download cos346day2

Document related concepts
no text concepts found
Transcript
COS 346
Day 2
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-1
Agenda
•
•
•
•
Questions?
WebCt accounts should be available
Signed contracts sheets
Assignment 1 is posted in WebCt
– Due Jan 25 (Thursday) at beginning of Class
• Finish Introduction To Database
Processing
• Begin Intro to SQL
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-2
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter One: Introduction
Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-3
Three Types of Database Design
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-4
Database Design from Existing Data
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-5
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)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-6
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
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-7
Database Design from Database
Redesign
Database redesign is
covered in Chapter
Eight, after coverage of
DDL SQL in Chapter
Seven
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-8
What You Need to Learn
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-9
Knowledge Priorities
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-10
A Brief History of
Database
Processing
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-11
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
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-12
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter One – Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-13
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter Two:
Introduction to
Structured Query Language
Part One
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-14
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-15
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-16
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)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-17
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-18
Cape Codd Retail Sales Structure
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-19
Accessing the Cape Codd
Database
• Download an MS access version from
WebCT under Database section
(chapter_2.mdb)
• Use the Oracle Server
LittleBlack.advtech.local
– Database SID: CapCodd
– You all have read access
• Account: Cos346
• Password: Cos346
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-20
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 -> Oracle 10G
– Into different columns  OrderDate becomes OrderMonth and
OrderYear
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-21
Extracted
Retail
Sales Data
Format
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-22
Retail Sales Extract Tables
[in MS SQL Server]
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-23
Running SQL statements
For MS access
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-24
Running SQL statements
For MS access
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-25
Running SQL statements
For MS access
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-26
Running SQL statements
For MS access
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-27
Running SQL statements
For Oracle
Capcodd_littleblack.advtech.local
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-28
Running SQL statements
For Oracle
__________
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-29
The SQL SELECT Statement
• The fundamental framework for SQL query
states is the SQL SELECT statement:
– SELECT
– FROM
– WHERE
{ColumnName(s)}
{“Tablespace.”TableName(s)}
{Conditions}
• All SQL statements end with a semi-colon
(;).
• Select statements return a temporary
database table
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-30
Specific Columns on One Table
SELECT Department, Buyer
FROM
Chapter_2.SKU_DATA;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-31
Specifying Column Order
SELECT Buyer, Department
FROM
Chapter_2.SKU_DATA;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-32
The DISTINCT Keyword
SELECT
FROM
DISTINCT Buyer, Department
Chapter_2.SKU_DATA;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-33
Selecting All Columns:
The Asterisk (*) Keyword
SELECT *
FROM
Chapter_2.SKU_DATA;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-34
Specific Rows from One Table
SELECT
FROM
WHERE
*
Chapter_2.SKU_DATA
Department = 'Water Sports';
NOTE: SQL wants a plain ASCII single quote: ' NOT “ !
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-35
Specific Columns and Rows from
One Table
SELECT
FROM
WHERE
SKU_Description, Buyer
Chapter_2.SKU_DATA
Department = 'Climbing';
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-36
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter Two Part One
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-37
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter Two:
Introduction to
Structured Query Language
Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-38
Using MS Access - Results
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-39
Using MS SQL Server
[SQL Query Analyzer]
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-40
Using Oracle
[SQL*Plus]
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-41
Using Oracle
[Quest Software’s TOAD]
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-42
Using MySQL
[MySQL Command Line Client]
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-43
Using MySQL
[MySQL Query Browser]
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-44
Sorting the Results: ORDER BY
SELECT *
FROM
ORDER BY
Chapter_2.ORDER_ITEM
OrderNumber, Price;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-45
Sort Order:
Ascending and Descending
SELECT
*
FROM
Chapter_2.ORDER_ITEM
ORDER BY Price DESC, OrderNumber ASC;
NOTE: The default sort order is ASC – does not have to be specified.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-46
WHERE Clause Options: AND
SELECT
FROM
WHERE
AND
*
Chapter_2.SKU_DATA
Department = 'Water Sports'
Buyer = 'Nancy Meyers';
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-47
WHERE Clause Options: OR
SELECT
FROM
WHERE
OR
*
Chapter_2.SKU_DATA
Department = 'Camping'
Department = 'Climbing';
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-48
WHERE Clause Options:- IN
SELECT
FROM
WHERE
*
Chapter_2.SKU_DATA
Buyer IN ('Nancy Meyers',
'Cindy Lo', 'Jerry Martin');
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-49
WHERE Clause Options: NOT IN
SELECT
FROM
WHERE
*
Chapter_2.SKU_DATA
Buyer NOT IN ('Nancy Meyers',
'Cindy Lo', 'Jerry Martin');
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-50
WHERE Clause Options:
Ranges with BETWEEN
SELECT
FROM
WHERE
*
Chapter_2.ORDER_ITEM
ExtendedPrice
BETWEEN 100 AND 200;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-51
WHERE Clause Options:
Ranges with Math Symbols
SELECT
FROM
WHERE
AND
*
Chapter_2.ORDER_ITEM
ExtendedPrice >= 100
ExtendedPrice <= 200;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-52
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
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-53
WHERE Clause Options:
LIKE and Wildcards (Continued)
SELECT
*
FROM Chapter_2.SKU_DATA
WHERE
Buyer LIKE 'Pete%';
MS ACCESS Buyer LIKE ‘Pete*’;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-54
WHERE Clause Options:
LIKE and Wildcards (Continued)
SELECT
FROM
WHERE
*
Chapter_2.SKU_DATA
SKU_Description LIKE '%Tent%';
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-55
WHERE Clause Options:
LIKE and Wildcards
SELECT
*
FROM Chapter_2.SKU_DATA
WHERE
SKU LIKE '%2_ _';
MS ACCESS SKU LIKE ‘*2??’;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-56
SQL Built-in Functions
• There are five SQL Built-in Functions:
– COUNT
– SUM
– AVG
– MIN
– MAX
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-57
SQL Built-in Functions (Continued)
SELECT SUM (ExtendedPrice)
AS Order3000Sum
FROM
ORDER_ITEM
WHERE OrderNumber = 3000;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-58
SQL Built-in Functions (Continued)
SELECT
FROM
SUM (ExtendedPrice) AS
AVG (ExtendedPrice) AS
MIN (ExtendedPrice) AS
MAX (ExtendedPrice) AS
Chapter_2.ORDER_ITEM;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
OrderItemSum,
OrderItemAvg,
OrderItemMin,
OrderItemMax
1-59
SQL Built-in Functions (Continued)
SELECT COUNT(*) AS NumRows
FROM
ORDER_ITEM;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-60
SQL Built-in Functions (Continued)
SELECT COUNT
(DISTINCT Department)
AS DeptCount
FROM
Chapter_2.SKU_DATA;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-61
Arithmetic in SELECT Statements
SELECT Quantity * Price AS EP,
ExtendedPrice
FROM
Chapter_2.ORDER_ITEM;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-62
String Functions in SELECT
Statements
SELECT
FROM
DISTINCT RTRIM (Buyer)
+ ' in ' + RTRIM (Department)
AS Sponsor
Chapter_2.SKU_DATA;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-63
The SQL keyword GROUP BY
SELECT
FROM
GROUP BY
Department, Buyer,
COUNT(*) AS
Dept_Buyer_SKU_Count
Chapter_2.SKU_DATA
Department, Buyer;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-64
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-65
The SQL keyword GROUP BY
(Continued)
SELECT
FROM
WHERE
GROUP BY
ORDER BY
Department, COUNT(*) AS
Dept_SKU_Count
SKU_DATA
SKU <> 302000
Department
Dept_SKU_Count;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-66
The SQL keyword GROUP BY
(Continued)
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
Department, COUNT(*) AS
Dept_SKU_Count
Chapter_2.SKU_DATA
SKU <> 302000
Department
COUNT (*) > 1
Dept_SKU_Count;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-67
Querying Multiple Tables:
Subqueries
SELECT SUM (ExtendedPrice)
FROM
ORDER_ITEM
WHERE
SKU IN
(SELECT
FROM
WHERE
AS Revenue
SKU
SKU_DATA
Department = 'Water Sports');
Note: The second SELECT statement is a subquery.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-68
Querying Multiple Tables:
Subqueries (Continued)
SELECT Buyer
FROM
SKU_DATA
WHERE SKU IN
(SELECT
FROM
WHERE
SKU
ORDER_ITEM
OrderNumber IN
(SELECT
OrderNumber
FROM
RETAIL_ORDER
WHERE
OrderMonth = 'January'
AND
OrderYear = 2004));
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-69
Querying Multiple Tables:
Joins
SELECT
FROM
WHERE
Buyer, ExtendedPrice
SKU_DATA, ORDER_ITEM
SKU_DATA.SKU = ORDER_ITEM.SKU;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-70
Querying Multiple Tables:
Joins (Continued)
SELECT
FROM
WHERE
GROUP BY
ORDER BY
Buyer, SUM(ExtendedPrice)
AS BuyerRevenue
SKU_DATA, ORDER_ITEM
SKU_DATA.SKU = ORDER_ITEM.SKU
Buyer
BuyerRevenue DESC;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-71
Querying Multiple Tables:
Joins (Continued)
SELECT
FROM
WHERE
AND
Buyer, ExtendedPrice, OrderMonth
SKU_DATA, ORDER_ITEM, RETAIL_ORDER
SKU_DATA.SKU = ORDER_ITEM.SKU
ORDER_ITEM.OrderNumber =
RETAIL_ORDER.OrderNumber;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-72
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-73
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter Two Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
1-74
Related documents