Database Management
Database Management
Database Systems
Chapter 0:
Course Information
A/P Wang Lipo
Chapter 0 Course Information
Lectures: Weeks 1 6
Tutorials: Weeks 2 - 7
Lectures: Weeks 7 12
Tutorials: Weeks 8 - 13
Course Outline
Part I: A/P Wang Lipo
Database Concepts (Chapter 1)
Database Design (Chapters 3 - 5)
Structured Query Language (SQL) (Chapters 7
& 8)
Part II: A/P Tan Hee Beng Kuan
Physical Design
DB Administration
Client/Server DB
Data Warehousing
Chapter 0 Course Information
Textbook
Modern Database Management
11th Edition
Jeffrey A. Hoffer, et al
Prentice Hall
(other editions should be OK, too)
LOTS of reference books on databases !!!
Chapter 0 Course Information
Assessment
Continuous Assessment (CA) (20%)
Part 1, 10%, an assignment on database
design & implementation (to be announced)
Part 2, 10%, to be announced
Examination (80%)
4/4 questions
Consulting Hours
Monday 13:30 15:30
Tuesday 10:30 11:30
Or by appointment
Or just drop by at any time
Your suggestions/comments are always
welcome!
Chapter 1:
The Database Environment
Modern Database Management
11th Edition
Jeffrey A. Hoffer, Ramesh Venkataraman,
Heikki Topi
Chapter 1 The Database Environment
Objectives
Definitions
Limitations of conventional file processing
Advantages of databases
Evolution of database systems
Definitions
Database: organized collection of logically
related data
Data: stored objects and events
Structured: numbers, text, dates
Unstructured: images, video
10
11
Duplication of Data
Different systems/programs have separate copies of the same data
12
13
14
SOLUTION:
The DATABASE Approach
Central repository of shared data
Minimum data redundancy
A standard software
Database Management System (DBMS)
15
16
Evolution of DB Systems
17
18
19
20
21
22
Chapter 3:
Entity-Relationship (ER)
Modeling
Modern Database Management
11th Edition
Jeffrey A. Hoffer, Ramesh Venkataraman,
Heikki Topi
Chapter 3 Modeling Data in the Organization
Objectives
Write good names and definitions for entities,
relationships, and attributes
Distinguish unary, binary, and ternary relationships
Model different types of attributes, entities, relationships,
and cardinalities
Draw Entity-Relationship (ER) diagrams for common
business situations
Convert many-to-many relationships to associative
entities
Entity-Relationship (ER)
Model
Entities:
Entity instance: a person, a place, an object, an event (often
corresponds to a row in a table)
Entity Type: collection of entity instances, a type of persons, a type
of places, (often corresponds to a table)
Relationships:
Relationship instance: link between entity instances
Relationship type: category of relationshiplink between entity
types
(Figure 2-11)
Chapter 3 Modeling Data in the Organization
Attribute
Entity Instance
Chapter 3 Modeling Data in the Organization
6
Relationship Instance
b) Relationship
instances
Inappropriate
entities
Appropriate
entity
Inappropriate
entity: System
output
Appropriate
entity
10
Attributes
Classifications of attributes:
Required versus Optional Attributes
Simple versus Composite Attribute
Single-Valued versus Multivalued Attribute
Stored versus Derived Attributes
Identifier Attributes
11
Identifiers (Keys)
Identifier (Key): An attribute (or combination
of attributes) that uniquely identifies
individual instances of an entity type
Simple identifier (a single attribute) versus
Composite Identifier (a combination of
multiple attributes)
Candidate Identifier: an attribute or a
combination of attributes that could be a
keysatisfies the requirements for being an
identifier
Chapter 3 Modeling Data in the Organization
12
Characteristics of Identifiers
Will not be null
No intelligent identifiers (e.g., containing
locations or phone numbers that might
change)
Substitute new, simple keys for long,
composite keys, when worthwhile
13
Multivalued
an employee can have
more than one skill
Derived
from date
employed and
current date
14
15
More on Relationships
Relationships can have attributes
These describe features pertaining to the association
between the entities in the relationship
More on Relationships
Two entities can have more than one type of
relationship between them (multiple
relationships)
17
Degree of Relationships
Degree of a relationship is the
number of entity types that
participate in it
Unary Relationship
Binary Relationship
Ternary Relationship
Chapter 3 Modeling Data in the Organization
18
One entity
instance
related to
another of
the same
entity type
Entities of
two different
types related
to each other
Entities of three
different types
related to each
other
19
Cardinality of Relationships
One-to-One
An instance in an entity type in the relationship is
related to at most 1 instance in another entity type
One-to-Many
An instance in the entity type on one-side of the
relationship can be related to many instances in the
many-side entity type, but an entity instance on the
many-side of the relationship can be related to a
maximum of 1 entity instance in the one-side entity
type
Many-to-Many
Entity instances on both sides of the relationship can
be related to many entity instances on the other side
Chapter 3 Modeling Data in the Organization
20
Cardinality Constraints
Cardinality Constraints - the number of
instances of one entity that can or must be
associated with each instance of another
entity
Minimum Cardinality
If zero, then optional
If one or more, then mandatory
Maximum Cardinality
The maximum number
Chapter 3 Modeling Data in the Organization
21
22
23
24
a) Mandatory cardinalities
A patient history is
recorded for one and
only one patient
25
A project must be
assigned to at least one
employee, and may be
assigned to many
26
a) Optional cardinalities
A person is is
married to at most
one other person,
or may not be
married at all
27
A many-to-many relationship (top) can be replaced by two oneto-many relationships with an associative entity (CERTIFICATE
at bottom).
Chapter 3 Modeling Data in the Organization
28
Here, min
cardinality
constraint is 2
29
simple
Course Table
Pre-Requisite
Course ID
Course Title
Course ID
Pre-Req ID
EE4791
Database
EP122
EE4791
EP122
Accounting
EP233
EP122
EP233
Finance
EP233
EE4791
PH321
Physics
30
EMPLOYEE
POSSESSES
Employee
_ID
Employee_
Name
Employee
_ID
Skill_Code
981
Wang
122
C98
122
Singh
233
C98
233
Uno
233
E98
981
E98
981
C98
SKILL
Skill Code
Skill-Type
E98
Programming
C98
Writing
31
Weak entity
The existence of an instance in a weak entity depends on the
existence of an intance in a strong entity (identifying
owner)cannot exist on its own
A weak entity does not have a unique identifier (only a partial
identifier)
Partial identifier underlined with double-line
Entity box has double line
Identifying relationship
links strong entities to weak entities
Chapter 3 Modeling Data in the Organization
32
Strong entity
Weak entity
33
34
35
Microsoft Visio
Notation for Pine
Valley Furniture
ER diagram
Different modeling
software tools may have
different notation for the
same constructs
36
Chapter 4:
Supertype/Subtype
Relationship and Business
Rules
Modern Database Management
11th Edition
Jeffrey A. Hoffer, Ramesh Venkataraman,
Heikki Topi
Chapter 4 More on ER Model and Business Rules
Objectives
Use of supertype/subtype relationships
Use of generalization and specialization techniques
Specification of completeness and disjointness
constraints
Name categories of business rules
Define operational constraints graphically and in English
Attribute Inheritance:
Subtype entities inherit values of all attributes of
the supertype
An instance of a subtype is also an instance of the
supertype
Chapter 4 More on ER Model and Business Rules
a) ER
notation
Generalization and
Specialization
Generalization: defining a more general
entity type from a set of more specialized
entity types.
BOTTOM-UP
So we put
the shared
attributes in
a supertype
10
Only applies to
manufactured parts
11
Created 2
subtypes
12
Constraints in Supertype/
Completeness Constraint
Completeness Constraints: Whether an
instance of a supertype must also be a
member of at least one subtype (in other
words, whether the instances in the subtypes
completely cover the instances in the
supertype)
Yes: Total Specialization Rule (double line)
No: Partial Specialization Rule (single line)
Chapter 4 More on ER Model and Business Rules
13
14
A vehicle
does not
need to be
a car or a
truck
15
Constraints in Supertype/
Disjointness constraint
Disjointness Constraints: Whether
an instance of a supertype may
simultaneously be a member of two (or
more) subtypes (in other words, whether
overlap exists between subtypes)
Yes: Overlap Rule (o)
No: Disjoint Rule (d)
16
17
18
Constraints in Supertype/
Subtype Discriminators
Subtype Discriminator: An attribute of the
supertype whose values determine the target
subtype(s)
Disjoint a simple attribute (scalar) with alternative
values to indicate the possible subtypes
Overlapping a composite attribute whose subparts
pertain to different subtypes (vector). Each subpart
contains a boolean value to indicate whether or not
the instance belongs to the associated subtype
19
20
21
22
Business rules
Statements that define or constrain some aspect of
the business
Automated through DBMS software
A Good Business Rule is:
23
Structural assertion
rule expressing static structure. Includes attributes,
relationships, and definitions
E.g., each 2nd year student must have a major
Action assertion
rule expressing constraints/control of organizational actions
Chapter 4 More on ER Model and Business Rules
24
25
26
Corresponding object
Action assertion
Anchor object
Corresponding object
27
Corresponding
object
Upper LIMit
Action assertion
Anchor object
28
Chapter 5:
Logical Database Design and
the Relational Model
Modern Database Management
11th Edition
Jeffrey A. Hoffer, Ramesh Venkataraman,
Heikki Topi
Objectives
Properties of relations
Properties of candidate keys
First, second, and third normal form
Transform ER diagrams to relations
Create tables with entity and relational integrity
constraints
Use normalization to convert anomalous tables
to well-structured relations
Relation
Definition: A relation is a named, two-dimensional table of
data
Table consists of rows (records) and columns (attribute or
field)
Key Fields
Keys are special fields that serve two main purposes:
Primary keys are unique identifiers of the relation in question.
Examples include employee numbers, passport numbers, etc.
This is how we can guarantee that all rows are unique
Foreign keys are identifiers that enable a dependent relation
(on the many side of a relationship) to refer to its parent relation
(on the one side of the relationship)
Figure 5-3 Schema for four relations (Pine Valley Furniture Company)
Primary Key
Foreign Key
(implements 1:N relationship
between customer and order)
Integrity Constraints
Domain Constraints
Allowable values for an attribute
Metadata
Entity Integrity
No primary key attribute may be null: All
primary key fields MUST have data
Action Assertions
Business rules. Recall from Chapter 4
Chapter 5 Logical Database Design and the Relational Model
10
Integrity Constraints
Referential Integrity: any foreign key value (on the relation of
the many side) MUST match a primary key value in the relation
of the one side. Or the foreign key can be null.
For example: Delete Rules
Restrict: dont allow delete of parent side if related rows
exist in dependent side
Cascade: automatically delete dependent side rows that
correspond with the parent side row to be deleted (e.g.,
in weak entities)
Set-to-Null: set the foreign key in the dependent side to
null if deleting from the parent side not allowed for
weak entities
Chapter 5 Logical Database Design and the Relational Model
11
Figure 5-5
Referential integrity constraints (Pine Valley Furniture)
Referential
integrity
constraints are
drawn via arrows
from dependent to
parent table
12
13
(a) CUSTOMER
entity type with
simple
attributes
14
15
16
17
18
19
20
Foreign key
Chapter 5 Logical Database Design and the Relational Model
21
22
23
Foreign key
Foreign key
New
relationship
table
24
25
Foreign key
Foreign key
26
27
28
29
30
Nurse_Name Birth_Date
1001
Jane
1973.2.2
1002
Mary
1977.2.1
1003
Irene
1979.2.5
Center_ID
Location
Nurse_in_Charg
Date_Assigned
201
Jurong
1002
2014.12.20
Jane
1973.2.2
1002
Mary
1977.2.1
1003
Irene
1979.2.5
Center_ID
Center_ID Date_Assigned
201
2014.12.20
Location
201 5 LogicalJurong
Chapter
Database Design and the Relational Model
31
32
33
34
35
36
37
38
39
(a) Bill-of-materials
relationships (M:N)
40
41
42
43
Remember
that the
primary key
MUST be
unique
This is why
But this makes a
treatment date and
very
time are included in
cumbersome
the composite
key
primary key
It would be
better to create a
surrogate key
like Treatment#
44
45
46
47
48
Transforming ER Diagrams
into Relations (cont.)
Mapping Supertype/Subtype Relationships
1:1 relationship established between supertype
and each subtype, with supertype as primary
table:
One relation for supertype and for each subtype
Supertype attributes (including identifier and
subtype discriminator) go into supertype relation
Subtype attributes go into each subtype;
primary key of supertype relation also becomes
primary key of subtype relation
Chapter 5 Logical Database Design and the Relational Model
49
50
Figure 5-21
Mapping Supertype/subtype relationships to relations
51
Normalization
Primarily a tool to validate and improve a logical
design so that it satisfies certain constraints that
52
Well-Structured Relations
A relation that contains minimal data redundancy and
allows users to insert, delete, and update rows
without causing data inconsistencies
Goal is to avoid anomalies
Insertion Anomalyadding new rows forces user
to create duplicate data
Deletion Anomalydeleting rows may cause a
loss of data that would be needed for other future
rows
Modification Anomalychanging data in a row
forces changes to other rows because of
duplication
53
ExampleFigure 5-2b
54
55
56
57
58
59
60
61
62
63
64
Getting it into
Second Normal
Form
Partial dependencies are removed, but there are
still transitive dependencies
Chapter 5 Logical Database Design and the Relational Model
65
Getting it into
Third Normal
Form
66
67
Chapter 7:
Introduction to SQL
Modern Database Management
11th Edition
Jeffrey A. Hoffer, Ramesh Venkataraman,
Heikki Topi
Objectives
Define a database using SQL data
definition language
Establish referential integrity using SQL
Write single table queries using SQL
SQL Overview
Structured Query Language (SQL)
The standard for relational database
management systems (RDBMS)
RDBMS: A database management system
that manages data as a collection of tables in
which all relationships are represented by
common values in related tables
History of SQL
1970: E. Codd develops relational database
concept
1974: System R with Sequel (later SQL)
created at IBM Research Lab
1979: Oracle markets first relational DB with
SQL
1986: ANSI (American National Standards
Institute) SQL standard released
Current: SQL is supported by most major
database vendors
Benefits of a Standardized
Relational Language
SQL Environment
Schema
The structure that contains descriptions of objects created
by a user (base tables, views, constraints)
Catalog
A set of schemas that constitute the description of a
database
Overall table
definitions
10
11
Non-nullable specification
Primary keys
can never have
NULL values
12
Non-nullable specifications
Primary key
13
Domain constraint
14
Primary key of
parent table
Foreign key of
dependent table
Referential integrity constraint
Chapter 7 Introduction to SQL
15
16
17
Insert Statement
Adds data to a table
Inserting an entire row into a table
INSERT INTO CUSTOMER_T VALUES (001, Contemporary
Casuals, 1355 S. Himes Blvd., Gainesville, FL, 32601);
18
Update Statement
Modifies data in existing rows
Change the unit price of the product whose
product id is 7 to 775:
UPDATE PRODUCT_T SET UNIT_PRICE = 775
WHERE PRODUCT_ID = 7;
Chapter 7 Introduction to SQL
19
Delete Statement
Removes rows from a table
Delete certain rows
Delete all customers from Hawaii:
DELETE FROM CUSTOMER_T
WHERE STATE = HI;
20
Removing Tables
DROP TABLE statement allows you to
remove tables from your schema:
DROP TABLE CUSTOMER_T
Be careful:
Both the data and the table structure will be deleted !!!
21
22
SELECT Statement
Used for queries on single or multiple tables
Clauses of the SELECT statement:
SELECT
List the columns (and expressions) that should be returned from the query
FROM
Indicate the table(s) or view(s) from which data will be obtained
WHERE
Indicate the conditions under which a row will be included in the result
GROUP BY
Indicate categorization of results
HAVING
Indicate the conditions under which a category (group) will be included
23
SELECT Example
Find products with standard price less than
$275
SELECT PRODUCT_NAME, STANDARD_PRICE
FROM PRODUCT_T
WHERE STANDARD_PRICE < 275;
24
Same as:
SELECT CUSTOMER_T.CUSTOMER,
CUSTOMER_T.CUSTOMER_ADDRESS
FROM CUSTOMER_T
WHERE CUSTOMER_T.CUSTOMER = Home
Furnishings;
Chapter 7 Introduction to SQL
25
26
27
28
SELECT Example
Sorting Results with the ORDER BY Clause
Sort the results first by STATE, and within a state by
CUSTOMER_NAME
SELECT CUSTOMER_NAME, CITY, STATE
FROM CUSTOMER_T
WHERE STATE IN (FL, TX, CA, HI)
ORDER BY STATE, CUSTOMER_NAME DESC;
Note: the IN operator in this example allows you to include rows
whose STATE value is either FL, TX, CA, or HI. It is more
efficient than separate OR conditions
Chapter 7 Introduction to SQL
29
SELECT Example
Categorizing Results Using GROUP BY
List all states and the number of customers in
each state
SELECT CUSTOMER_STATE, COUNT(CUSTOMER_ID)
FROM CUSTOMER_T
GROUP BY CUSTOMER_STATE;
30
SELECT Example
Qualifying Results by Categories
Using the HAVING Clause
List all states and the number of customers in each
state (limit to only states with more than 1 customer)
SELECT CUSTOMER_STATE, COUNT(CUSTOMER_ID)
FROM CUSTOMER_T
GROUP BY CUSTOMER_STATE
HAVING COUNT(CUSTOMER_ID) > 1;
Like a WHERE clause, but it operates on groups (categories),
not on individual rows. WHERE clause operates on
individual rows.
Chapter 7 Introduction to SQL
31
Materialized View
Copy or replication of data: Data actually stored
Must be refreshed periodically to match the corresponding
base tables
Chapter 7 Introduction to SQL
32
33
Disadvantages
Use processing time each time view is referenced
34
Chapter 8:
Advanced SQL
Modern Database Management
11th Edition
Jeffrey A. Hoffer, Ramesh Venkataraman,
Heikki Topi
Objectives
Left Outer Join: a join in which rows that do not have matching
values in common columns are nonetheless included in the result
table
Union Join: includes all columns from each table in the join, and
an instance for each row of each table
Example
Unlike
natural join,
this will
include
customer
rows with
no matching
order rows
10
A SQL Query walks into a bar. In one corner of
the bar are two tables. The Query walks up to
the tables and asks:
- Mind if I join you?
12
13
Subquery Example
Show the names of all customers who have
placed an order
SELECT CUSTOMER_NAME
FROM CUSTOMER_T
WHERE CUSTOMER_ID IN
(SELECT DISTINCT CUSTOMER_ID FROM ORDER_T);
Subquery returns a list that will be
used in the WHERE clause of the
outer query
14
Correlated subqueries:
Make use of data from the outer query
Execute once for each row of the outer query
Can use the EXISTS operator
15
16
17
18
19
OrderLine_T
Subquery refers
to outer-query
data, so
executes once
for each row of
outer query
Note: only the
Product_T
orders that
involve products
with Natural
Ash will be
included in the
final results
Query
Result:
1001
1002
1003
1007
1008
1009
20
2.
3.
4.
5.
6.
7.
21
22
23
Union Queries
Combine the output (union of multiple queries)
together into a single result table
First query
Combine
Second query
Chapter 8 Advanced SQL
24
Dynamic SQL
Ability for an application program to generate
SQL code on the fly, as the application is
running
25
Persons
LastName
Hansen
Svendson
Pettersen
table
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
SQL Queries
With SQL, we can query a database and have a result set
returned.
A query like this:
SELECT LastName
FROM Persons;
Gives a result set like this:
LastName
Hansen
Svendson
Pettersen
3
FirstName
Ola
Tove
Kari
FirstName
Ola
Tove
Kari
Address
Timoteivn 10
Borgvn 23
Storgt 20
City
Sandnes
Sandnes
Stavanger
5
DISTINCT
To select ALL values from the column named "Company" we use
a SELECT statement like this:
SELECT Company
FROM Orders;
Orders
Company
Sega
W3Schools
Trio
W3Schools
OrderNumber
3412
2312
4678
6798
Company
Sega
W3Schools
Trio
W3Schools
Company
Sega
Company
OrderNumber
Sega
3412
W3Schools 2312
Trio
W3Schools
Trio
4678
W3Schools 6798
FirstName
Ola
Tove
Stale
Kari
Address
Timoteivn 10
Borgvn 23
Kaivn 18
Storgt 20
City
Sandnes
Sandnes
Sandnes
Stavanger
Year
1951
1978
1980
1960
Query Results
LastName
Hansen
Svendson
Svendson
FirstName
Ola
Tove
Stale
Address
Timoteivn 10
Borgvn 23
Kaivn 18
City
Sandnes
Sandnes
Sandnes
Year
1951
1978
1980
10
table
LastName
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
table
LastName
FirstName
Address
City
Pettersen
Kari
Storgt 20
Stavanger
Hetland
Camilla
Hagabakka 24
Sandnes
11
City
Pettersen
Kari
Storgt 20
Stavanger
Hetland
Camilla
Hagabakka 24 Sandnes
FirstName
Kari
Camilla
Address
Storgt 20
Hagabakka 24
Storgt 67
City
Stavanger
Sandnes
12
FirstName
Fred
Address
Kirkegt 56
Storgt 67
City
Stavanger
FirstName
Fred
Nina
Address
Kirkegt 56
Storgt 67
City
Stavanger
13
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
Storgt 67
We want to change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen' ;
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
Stien 12
Stavanger
14
Updates
Example:
UPDATE PRODUCT
SET price = price/2
WHERE Product.name IN
(SELECT product
FROM Purchase
WHERE Date =Oct. 25, 1999);
15
Delete a Row
LastName
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
Rasmussen
Nina
Stien 12
Stavanger
FirstName
Address
City
Nilsen
Fred
Kirkegt 56
Stavanger
16
Key
CName
StockPrice
Country
GizmoWorks
25
USA
Canon
65
Japan
Hitachi
15
Japan
Product
PName
Price
Category
Manufacturer
Gizmo
$19.99
Gadgets
GizmoWorks
Powergizmo
$29.99
Gadgets
GizmoWorks
SingleTouch
$149.99
Photography
Canon
MultiTouch
$203.99
Household
Hitachi
Foreign
key
17
Joins
Product (pname, price, category, manufacturer)
Company (cname, stockPrice, country)
Find all products under $200 manufactured in Japan;
return their names and prices.
Join
between Product
SELECT PName, Price
and Company
FROM
Product, Company
WHERE Manufacturer=CName AND Country=Japan
AND Price <= 200;
18
Joins
Product
Company
PName
Price
Category
Manufacturer
Cname
StockPrice
Country
Gizmo
$19.99
Gadgets
GizmoWorks
GizmoWorks
25
USA
Powergizmo
$29.99
Gadgets
GizmoWorks
Canon
65
Japan
SingleTouch
$149.99
Photography
Canon
Hitachi
15
Japan
MultiTouch
$203.99
Household
Hitachi
PName
Price
SingleTouch
$149.99
19
Subqueries
Company(name, city)
Product(pname, maker)
Purchase(id, product, buyer)
Return cities where one can find companies that manufacture
products bought by Joe Blow
SELECT Company.city
FROM Company
WHERE Company.name IN
(SELECT Product.maker
FROM Purchase , Product
WHERE Product.pname=Purchase.product
AND Purchase .buyer = Joe Blow);
20
Subqueries
Is it equivalent to this ?
SELECT Company.city
FROM
Company, Product, Purchase
WHERE Company.name= Product.maker
AND Product.pname = Purchase.product
AND Purchase.buyer = Joe Blow;
Beware of duplicates !
21
Removing Duplicates
SELECT DISTINCT Company.city
FROM Company
WHERE Company.name IN
(SELECT Product.maker
FROM Purchase , Product
WHERE Product.pname=Purchase.product
AND Purchase .buyer = Joe Blow);
SELECT DISTINCT Company.city
FROM
Company, Product, Purchase
WHERE Company.name= Product.maker
AND Product.pname = Purchase.product
AND Purchase.buyer = Joe Blow;
Now
they are
equivalent
22
Subqueries
ALL
ANY
EXISTS
Find products that are more expensive than all those produced
By Gizmo-Works
SELECT name
FROM Product
WHERE price > ALL (SELECT price
FROM Purchase
WHERE maker=Gizmo-Works); 23
Correlated Queries
Movie (title, year, director, length)
Find movies whose title appears more than once (in different years).
SELECT DISTINCT title
FROM Movie AS x
WHERE year <> ANY
(SELECT year
FROM Movie
WHERE title = x.title);
correlation
24
25
Aggregation
SELECT avg(price)
FROM
Product
WHERE maker=Toyota;
SELECT count(*)
FROM Product
WHERE year > 1995;
Aggregation: Count
COUNT applies to duplicates, unless otherwise stated:
SELECT Count(category)
FROM Product
WHERE year > 1995;
same as Count(*)
We probably want:
SELECT Count(DISTINCT category)
FROM Product
WHERE year > 1995;
27
More Examples
Purchase(product, date, price, quantity)
Purchase
Simple Aggregations
Product
Date
Price
Quantity
Bagel
10/21
20
Banana
10/3
0.5
10
Banana
10/10
10
Bagel
10/25
1.50
20
50 (= 20+30)
29
SELECT
FROM
WHERE
GROUP BY
30
Product
Date
Price
Quantity
Bagel
10/21
20
Bagel
10/25
1.50
20
Banana
10/3
0.5
10
Banana
10/10
10
SELECT
FROM
WHERE
GROUP BY
Product
TotalSales
Bagel
50
Banana
15
HAVING Clause
SELECT
product, Sum(price * quantity)
FROM
Purchase
WHERE
date > 10/1/2005
GROUP BY product
HAVING
Sum(price * quantity) > 30;
HAVING clause contains conditions on aggregates
(outputs Bagel only).
32
2. Find all companies with all their products having prices < 100
SELECT DISTINCT Company.cname
FROM Company
WHERE Company.cname NOT IN (SELECT Product.company
FROM Product
WHERE Produc.price >= 100;
33
Group-by
Author(login, name)
Wrote(login, url)
Mentions(url, word)
Group-by
Find all authors who wrote at least 10
documents:
SELECT
Author.name
FROM
Author, Wrote
WHERE
Author.login=Wrote.login
GROUP BY Author.name
HAVING
count(wrote.url) > 10;
No need for DISTINCT: automatically from GROUP BY
35
Store(sid, sname)
Product(pid, pname, price, sid)
For each store, find its most expensive product
SELECT Store.sname, x.pname
FROM Store, Product x
WHERE Store.sid = x.sid and
x.price >=
ALL (SELECT y.price
FROM Product y
WHERE Store.sid = y.sid)
GROUP BY Store.sname;
36
NULLS in SQL
Whenever we dont have a value, we can put a NULL
Can mean many things:
Value does not exists
Value exists but is unknown
Value not applicable
Etc.
The schema specifies for each attribute if it can be null
(nullable attribute) or not
How does SQL cope with tables that have NULLs ?
37
Null Values
If x= NULL then 4*(3-x)/7 is still NULL
If x= NULL then (x=Joe) is UNKNOWN
In SQL there are three boolean values:
FALSE
UNKNOWN
TRUE
=
=
=
0
0.5
1
38
Null Values
Unexpected behavior:
SELECT *
FROM Person
WHERE age < 25 OR age >= 25;
Some Persons are not included !
39
Null Values
Can test for NULL explicitly:
x IS NULL
x IS NOT NULL
SELECT *
FROM Person
WHERE age < 25 OR age >= 25 OR age IS NULL ;
Now it includes all Persons
40