Database Application Design January 28, 2000 Handout #4 (C) 2000, The

Download Report

Transcript Database Application Design January 28, 2000 Handout #4 (C) 2000, The

January 28, 2000
Database Application Design
Handout #4
(C) 2000, The
University of Michigan
1
Course information
•
•
•
•
•
•
Instructor: Dragomir R. Radev ([email protected])
Office: 305A, West Hall
Phone: (734) 615-5225
Office hours: Thursdays 3-4 and Fridays 1-2
Course page: http://www.si.umich.edu/~radev/654w00
Class meets on Fridays, 2:30 - 5:30 PM, 311 WH
(C) 2000, The
University of Michigan
2
The relational model and
normalization
(cont’d)
(C) 2000, The
University of Michigan
3
Third normal form (3NF)
• HOUSING (SID, Building, Fee)
• Key: SID
• FD : Building  Fee, SID  Building 
Fee
• Transitive dependencies
• Is HOUSING in 1NF, in 2NF?
(C) 2000, The
University of Michigan
4
Example
SID Building
(C) 2000, The
University of Michigan
Fee
100
Randolph 1200
150
Ingersoll
200
Randolph 1200
250
Pitkin
300
Randolph 1200
1100
1100
5
Third Normal Form (Cont’d)
• Definition: 2NF with all transitive
functional dependencies removed
• Example: STU-HOUSING (SID, Building)
and BLDG-FEE (Building, Fee)
(C) 2000, The
University of Michigan
6
Boyce-Codd Normal Form
(BCNF)
•
•
•
•
ADVISER (SID, Major, Fname)
SID doesn’t determine Major
SID doesn’t determine Fname either
Candidate keys: (SID, Major) or (SID,
Fname)
• Primary key
• Is ADVISER in 1NF? 2NF?
(C) 2000, The
University of Michigan
7
Example
(C) 2000, The
University of Michigan
SID Major
Fname
100
Math
Cauchy
150
Psychology Jung
200
Math
Riemann
250
Math
Cauchy
300
Psychology Perls
300
Math
Riemann
8
BCNF (Cont’d)
• Are modification anomalies possible in
ADVISER?
• BCNF: every determinant must be a
candidate key
• ADVISER: Fname is not a candidate key
• Relations in BCNF have no anomalies as far
as functional dependencies are concerned
(C) 2000, The
University of Michigan
9
Fourth Normal Form (4NF)
• STUDENT (SID, Major, Activity)
• Key: (SID, Major, Activity)
• Multivalued dependencies:
SID   Major
SID   Activity
• Is the relationship between SID and Major a
functional dependency?
(C) 2000, The
University of Michigan
10
Update anomalies
SID Major
Activity
100
Music
Swimming
100
Accounting Swimming
100
Music
100
Accounting Tennis
150
Math
(C) 2000, The
University of Michigan
Tennis
Jogging
11
4NF (Cont’d)
• Eliminating multi-valued dependencies
• Definition of 4NF: BCNF with no multivalue dependencies
(C) 2000, The
University of Michigan
12
Domain/Key Normal Form
(DK/NF)
• Fagin (1981) showed that a relation in
DK/NF is one with no modification
anomalies and vice versa
• Definition: every constraint on a relation is
a logical consequence of the definition of
keys and domains
(C) 2000, The
University of Michigan
13
Constraints in DK/NF
• Any rule governing static values of
attributes that is precise enough so that we
can determine its truth value
• Not included: “Salesperson salary in the
current period can never be less than salary
in the prior period”. Why?
(C) 2000, The
University of Michigan
14
Keys and domains
• Keys
• Domains: physical description and logical
description
• Definition: a relation is in DK/NF if
enforcing key and domain restrictions
causes all of the constraints to be met
• No formal algorithm
(C) 2000, The
University of Michigan
15
Example 1
• STUDENT (SID, GradeLevel, Building,
Fee)
• Key: SID
• Constraints: Building  Fee
SID mustn’t start with a 1
• Solution: modify domain of SID
(C) 2000, The
University of Michigan
16
Example 1 (cont’d)
• Need to make Building  Fee a logical
consequence of a key
• However, building is NOT a key!
– Remove building from STUDENT
– Remove fee from STUDENT
• Every relation should have a single theme!
(C) 2000, The
University of Michigan
17
DK definition
• Domain Definitions
– SID in CDDD, where C is in {0,2-9} and D is
in {0-9}
– GradeLevel in {FR,SO,JR,SR,GR}
– Building in CHAR(4)
– Fee in DEC(4)
(C) 2000, The
University of Michigan
18
DK definition (Cont’d)
• Relation and Key Definitions
STUDENT (SID, GradeLevel, Building)
Key: SID
BLDG-FEE (Building, Fee)
Key: Building
(C) 2000, The
University of Michigan
19
Two more examples
2. PROFESSOR (FID, Fname, Class, SID,
Sname)
3. STU-ADVISER (SID, Sname, FID, Fname,
GradFacultyStatus)
(C) 2000, The
University of Michigan
20
Foundations of relational
implementation
(C) 2000, The
University of Michigan
21
Defining relational data
•
•
•
•
•
•
•
Relations (tables)
Domains, attributes, constraints
Relational structure (RS)
Occurrence (RS + data)
Relational schemas
Keys: design and implementation semantics
Logical and physical keys
(C) 2000, The
University of Michigan
22
Implementing a relational
database
• Defining the database structure using a data
definition language (not always needed)
• Allocating media space
• Creating the database data
(C) 2000, The
University of Michigan
23
Relational data manipulation
• Relational algebra
• Transform-oriented languages
• Query-by-example
(C) 2000, The
University of Michigan
24
Interfaces to DBMS
• Forms-based data manipulation
• Query/Update language interface:
– SELECT Name, Age
FROM PATIENT
WHERE Physician = ‘Levy’
• Stored procedure interface
– DO BILLING FOR BDATE = ‘9/15/1999’
• Application program interface (API)
(C) 2000, The
University of Michigan
25
Msql example
$dbh = Msql->connect ($msql_hostname,$msql_databasename)
$sth = $dbh->query (“select title, id from $table_name”)
($title,$id) = $sth->fetchrow();
(C) 2000, The
University of Michigan
26
Relational algebra
• Relational operators:
–
–
–
–
–
–
–
UNION (for union compatible relations)
DIFFERENCE: STUDENT[Name]-JUNIOR[Name]
INTERSECTION
PRODUCT (Cartesian product)
PROJECTION
SELECTION
JOIN: STUDENT JOIN (SID = StudentNumber) ENROLLMENT
(C) 2000, The
University of Michigan
27
Types of JOIN
•
•
•
•
Equijoin
Natural join
Inner join
Outer join (left and right)
(C) 2000, The
University of Michigan
28
Expressing queries in relational
algebra
• Examples (from book, Chapter 8)
(C) 2000, The
University of Michigan
29
Readings for next time
• Kroenke
– Chapter 8: Foundations of Relational Implementation
– Chapter 9: Structured Query Language
• YRK (optional)
– Chapter 6: SQL according to MySQL and mSQL
(C) 2000, The
University of Michigan
30