Database Management Systems

advertisement
University of Manitoba
Asper School of Business
3500 DBMS
Bob Travica
Chapter 4
Querying
Based on G. Post, DBMS: Designing & Building Business Applications
Updated 2016
D
B
S
Y
S
T
E
M
S
Why do we Need Queries
 To extract meaningful data from database, which help us
answer business questions. Querying databases is where
business value from a DB system is drawn.
 We need a standardized system so users and developers can
learn one method that works on any (most) systems.
 SQL vs. Query By Example (QBE)
2 of 35
D
B
S
Y
S
T
E
M
S
Four Questions to Create a Query
 What data do you need to get? (columns in tables)
 What table or tables are involved?
 What are the constraints (filtering conditions for data,
search terms)?
 If more tables needed, how to join them?
 Download Sally’s Pet Store 2010 (queries work with it)
3 of 35
D
B
S
Y
S
T
E
M
S
Your Querying Play Pen: Sally’s Pet Store Database
Animal
Animal
OrderItem
AnimalOrder
*
OrderID
OrderDate
ReceiveDate
SupplierID
ShippingCost
EmployeeID
*
OrderID
AnimalID
Cost
AnimalID
Name
Category
Breed
DateBorn
Gender
Registered
Color
ListPrice
Photo
*
*
Breed
*
Category
Breed
*
Employee
Supplier
SupplierID
Name
ContactName
Phone
Address
ZipCode
CityID
City
CityID
ZipCode
City
State
AreaCode
Population1990
Population1980
Country
Latitude
Longitude
*
*
*
*
PONumber
ItemID
Quantity
Cost
SaleID
AnimalID
SalePrice
*
Customer
Sale
*
Category
SaleID
SaleDate
EmployeeID
CustomerID
SalesTax
*
*
Category
Registration
CustomerID
Phone
FirstName
LastName
Address
ZipCode
CityID
SaleItem
Merchandise
OrderItem
Merchandise
Order
PONumber
OrderDate
ReceiveDate
SupplierID
EmployeeID
ShippingCost
EmployeeID
LastName
FirstName
Phone
Address
ZipCode
CityID
TaxPayerID
DateHired
DateReleased
SaleAnimal
*
ItemID
Description
QuantityOnHand
ListPrice
Category
*
*
*
SaleID
ItemID
Quantity
SalePrice
*
*
4 of 32
D
B
S
Y
S
T
E
M
S
Sample Questions


1) List all animals with yellow in their

8) How many cats are in the animal list?
color.

9) Show animals in each category with
the count above 10.

10) List the CustomerID of the customers
that purchased something between
4/1/01 and 5/31/2010.
2) List all dogs with yellow in their color
born after 6/1/2010.

3) List all merchandise for cats with a list
price greater than $10.

4) List all dogs who are male and
registered or who were born before
6/1/01 and have white in their color.

5) What is the average sale price of all
animals?

6) What is the value of merchandise sold
per transaction record?

7) List the top 10 customers and total
amount they spent.
5 of 35
D
B
S
Y
S
T
E
M
S
Query By Example vs. SQL
Query #1: List all animals with yellow in their color (only or
along with other colors).
Which tables?
Which
constraints
Which data
(columns)
SELECT *
FROM Animal
WHERE (Color LIKE “*yellow*”);
6 of 35
D
B
S
Y
S
T
E
M
S
SQL Query – Structure
(Partial)
SELECT columns
- What columns to display?
FROM
- What tables are involved?
tables
(INNER JOIN
- If more than one table used:
What columns are the tables joined on PK & FK ?)
WHERE constraints
- What are the constraints (filtering
conditions) on records to be searched
and displayed?
7 of 32
D
B
S
Y
S
T
E
M
S
More on Sample Query #1
 Query question: List IDs, category, breed and color for
animals that have yellow in their color.
 Query statement:
SELECT AnimalID, Category, Breed, Color
FROM Animal
WHERE Color LIKE “*yellow*”;
 Variations of filtering condition – What do you get?:
- WHERE Color like “yellow”, Color = “yellow”
- WHERE Color LIKE [Please type color] - Application in
parameter query (asking user to enter desired color)
8 of 32
D
B
S
Y
S
T
E
M
S
ORDER BY (Sorting)
SELECT
FROM
INNER JOIN
WHERE
ORDER BY
columns
tables
columns
constraints
columns ASC {or DESC}
SELECT Category, Breed
FROM Animal
ORDER BY Category, Breed;
Output
Category Breed
Bird
Parakeet
Bird
Parakeet
Bird
Parrot
Bird
Parrot
Bird
Parrot
Cat
Abyssinian
Cat
Abyssinian
Cat
American Short
…
Try to reverse Breed and Category in ORDER BY (and SELECT).
9 of 32
D
B
S
Y
S
T
E
M
S
Constraints
• Implemented as operators (Relational, Boolean, String)
 Query #2: List all dogs with yellow in their color and are
born after 6/1/2010.
Relational Operator
Boolean Operator
String Operator
SELECT AnimalID, Category, Color, DateBorn
FROM Animal
WHERE ((Category="Dog") AND (Color Like "*Yellow*")
AND (DateBorn > #6/1/2010#));
10 of 35
D
B
S
Y
S
T
E
M
S
More on AND and > Operators
 Query #3: List IDs and descriptions for the merchandise for cats with a
list price greater than $10.
 SQL statement:
SELECT ItemID, Description, Category, ListPrice
FROM Merchandise
WHERE Category = "Cat" AND ListPrice > 10;
Output:
ItemID
Description
Category ListPrice
5 Cat Bed-Small
Cat
25,00 $
6 Cat Bed-Medium
Cat
35,00 $
14 Cat Food-Dry-25 pound Cat
18,00 $
40 Litter Box-Covered
15,00 $
Cat
Note how the ListPrice constraint is presented as a number (no quotes).
11 of 35
D
B
The AND vs. OR Operators
 Query #4: List all dogs that are male and registered or that were born
before 6/1/2010 and have white in their color.
S
Y
S
T
E
M
S
SELECT AnimalID, Category, Gender, Registered, DateBorn, Color
FROM Animal
WHERE (Category="Dog" AND Gender="Male" AND Registered Is Not Null)
OR
(Category="Dog" AND DateBorn < #June 1, 2010# AND Color Like "*White*") ;
• In addition to consequences of the first WHERE line, the output will
also include some older non-registered dogs of either gender with
white color in their fur.*
• Note the Registered constraint (a null value = a blank cell, missing data).
- cannot be replaced by zero (0)
- a reserved word in SQL syntax
- can be understood as “we don’t know what the value is”
• Best to write this query in two parts, test each separately, then
put them together.
12 of 35
D
B
S
Y
S
T
E
M
S
Boolean Algebra
AND:
OR:
NOT:
Example a = 3
True if both A and B exist.
Record
True if either A or B or both exist.
attributes a & b: b = -1
A (or B) does not exist.
If ……….. Then…………………..…
A
T
T
F
F
B
T
F
T
F
T
-
F
-
A AND B
T
F
F
F
NOT A
NOT A
A OR B
T
T
T
F
F
T
1. Query with AND:
(a > 4) AND (b < 0) =
(3 > 4) AND (-1 < 0)
F
T
F
No match,
Record not fethced
2. Query with OR:
(a > 4) OR (b < 0)
F
T
Match!
T
3. Query with NOT:
NOT (a > 4)
F
Match
T
13 of 32
D
B
S
Y
S
T
E
M
S
BOOlean Algebra
Parentheses indicate order of operations –
start working from within parentheses,
then work out with resulting values.
a=7
b = 200
c=2
( (a > 10) AND (b => 200) ) OR (c > 1) *
1
F
T
2
F
T
T
3
14 of 32
D
B
DeMorgan’s Law
Customer: "I want to look at a cat, but I don’t want any cats that are
registered or that have red in their color."
Used for simplifying cumbersome statements using NOT:
S
Y
S
T
E
M
S
SELECT Category, Registered, Color
FROM Animal
WHERE (Category="cat") AND
NOT ((Registered Is NOT NULL) OR (Color LIKE "*red*"));
Output:
Category
Registered
Color
Cat
Gray/Blue
Cat
White
Cat
White
Cat
Yellow
Cat
White
More…
15 of 32
Transformed into a simpler form by DeMorgan’s Law:
SELECT Category, Registered, Color
FROM Animal
WHERE Category="cat" AND Registered Is Null AND
Color NOT LIKE "*red*";
More…
16 of 35
D
B
S
Y
S
T
E
M
S
 Negation of clauses
 NOT (A AND B) becomes NOT A OR NOT B, and the global NOT is
deleted
 NOT (A OR B) becomes NOT A AND NOT B, global NOT is deleted
 IS NOT becomes IS
 IS, = become NOT
• Example: Constraints are Registered=ASCF, Color=Black
NOT ((Registered IS NOT null) OR (Color LIKE “*red*”))
T
F
OR
NOT
T
F
DeMorgan’s Law applied:
(Registered IS null) AND NOT (Color LIKE “*red*”)
F
F
AND
T NOT
F
17 of 35
D
B
S
Y
S
T
E
M
S
Computations – Row By Row
Query #6: What is the value of merchandise sold per transaction record?
SELECT SaleID, ItemID, SalePrice, Quantity, SalePrice*Quantity As Sum
FROM SaleItem;
Partial output (307 rows):
SaleID ItemID SalePrice Quantity Sum
187
18
$0,90
50 45,00 $
188
4
$58,50
1 58,50 $
188
11
$27,00
6 162,00 $
188
16
$0,72
25 18,00 $
189
17
$0,45
30 13,50 $
189
22
$67,50
2 135,00 $
18 of 32
D
B
S
Y
S
T
E
M
S
Computations On All Rows
Query #5: What is the average donation price of all animals?
DBMS supports math & stats functions:
 Sum
 Avg
Play with these functions
 Min
to learn more about
 Max
animals sold!
 Count
 StDev
 Var
SELECT Avg(Donation) AS [Average Donation Price]
FROM Animal;
Output: $162.16
19 of 35
D
B
S
Y
S
T
E
M
S
Computations On Select Rows
(with WHERE clause)
Query #8: How many cats are in the Animal list?
SELECT
FROM
WHERE
Count(AnimalID) AS [Cats Total]
Animal
Category = “Cat” ;
How would you make a general (parameter) query to find the
count of animals in any category the user wants?
20 of 35
D
B
S
Y
S
T
E
M
S
Aggregate Functions – GROUP BY Clause
Arithmetic and statistical functions that work on an appropriate set of records.
Question: What is the average donation price per animal category?
The first idea is to build the query on the previous, with addition of attribute
Category:
SELECT Category, Avg(Donation) AS [Average Donation Price]
FROM Animal;
But DB system will report an error that Category is not part of an aggregate function.
Correct query is:
SELECT Category, Avg(Donation) AS [Average Donation Price]
FROM Animal
GROUP BY Category ;
21 of 35
D
B
S
Y
S
T
E
M
S
Computations On Select Rows
(with HAVING clause)
 Query #9: Show Categories of animals with the count above 10.
optional
SELECT
FROM
GROUP BY
HAVING
ORDER BY
Category, Count(AnimalID) AS CountOfAnimalID
Animal
Category
Count(AnimalID) > 10
Count(AnimalID) DESC;
Processing order: (1) Group rows by attribute Category; (2) Find the
average for each group; (3) Count rows in each group; (4) Compare
the counts with number 10; (5) select and display Category names and
their counts where the count > 10.
22 of 35
D
B
S
Y
S
T
E
M
S
SQL Syntax
(May Be Useful for later use)
23 of 32
D
B
S
Y
S
T
E
M
S
SELECT
SELECT DISTINCT table.column {AS alias} , . . .
FROM table/query
INNER JOIN table/query ON T1.ColA = T2.ColB
WHERE (condition)
GROUP BY column
HAVING (group condition)
ORDER BY table.column
{ UNION, INTERSECT, EXCEPT … }
GROUP BY CUBE (dimension1, dimension2, …)
TRANSFORM aggfunction
{Crosstab values}
SELECT . . . FROM . . . GROUP BY {Crosstab rows}
PIVOT pivot column
{Crosstab columns}
24 of 32
D
B
S
Y
S
T
E
M
S
CREATE TABLE
CREATE TABLE table
(
column1
datatype (size) [NOT NULL] [index1] ,
column2
datatype (size) [NOT NULL] [index2],
…,
CONSTRAINT pkname PRIMARY KEY (column, …),
CONSTRAINT fkname FOREIGN KEY (column)
REFERENCES existing_table (key_column),
)
ALTER TABLE
See also:
DROP TABLE
25 of 32
D
B
S
Y
S
T
E
M
S
ALTER TABLE
ALTER TABLE table
ADD COLUMN column datatype (size)
DROP COLUMN column
See also:
CREATE TABLE
DROP TABLE
SQL Syntax: COMMIT
COMMIT WORK
See also:
ROLLBACK
26 of 32
D
B
S
Y
S
T
E
M
S
CREATE INDEX
CREATE [UNIQUE] INDEX index
ON table (column1, column2, … )
WITH {PRIMARY | DISALLOW NULL | IGNORE NULL}
See also:
CREATE TABLE
27 of 32
D
B
S
Y
S
T
E
M
S
DROP
DROP INDEX index ON table
DROP TABLE
DROP VIEW
See also:
DELETE
28 of 32
D
B
S
Y
S
T
E
M
S
DELETE
DELETE
FROM table
WHERE condition
See also:
DROP
INSERT
INSERT INTO table (column1, column2, …)
VALUES (value1, value2, … )
INSERT INTO newtable (column1, column2, …)
SELECT …
29 of 32
D
B
S
Y
S
T
E
M
S
UPDATE
UPDATE TABLE table
SET column1 = value1, column2 = value2, …
WHERE condition
See also:
DELETE
30 of 32
D
B
S
Y
S
T
E
M
S
GRANT
GRANT privilege
ON object
TO user | PUBLIC
See also:
privileges
ALL, ALTER, DELETE, INDEX,
INSERT, SELECT, UPDATE
REVOKE
31 of 32
D
B
S
Y
S
T
E
M
S
REVOKE
REVOKE privilege
ON object
FROM user | PUBLIC
See also:
privileges
ALL, ALTER, DELETE, INDEX,
INSERT, SELECT, UPDATE
GRANT
32 of 32
D
B
S
Y
S
T
E
M
S
ROLLBACK
SAVEPOINT savepoint
{optional}
ROLLBACK WORK
TO savepoint
See also:
COMMIT
33 of 32
Download