handout5
handout5
IIMT 3601
Database Management
Chapter 5 1
Objectives
Definition of terms
List five properties of relations
State two properties of candidate keys
Define first, second, and third normal form
Describe problems from merging relations
Transform E-R and EER diagrams to relations
Create tables with entity and relational integrity
constraints
Use normalization to convert anomalous tables to
well-structured relations
Chapter 5 2
Relation
Definition: A relation is a named, two-dimensional table of data
Table consists of rows (records) and columns (attribute or field)
Requirements for a table to qualify as a relation:
It must have a unique name
Every attribute value must be atomic (not multivalued, not composite)
Every row must be unique (can’t have two rows with exactly the same
values for all their fields)
Attributes (columns) in tables must have unique names
The order of the columns must be irrelevant
The order of the rows must be irrelevant
Chapter 5 3
Correspondence with E-R Model
Chapter 5 4
Key Fields
Chapter 5 5
Figure 5-3 Schema for four relations (Pine Valley Furniture Company
Primary Key
Foreign Key
(implements 1:N relationship
between customer and order)
Chapter 5 6
Integrity Constraints
Domain Constraints
Allowable values for an attribute. See Table on next slide
Entity Integrity
No primary key attribute may be null. All primary key fields MUST have data
Action Assertions
Business rules. Recall from handout 4
Chapter 5 7
Domain definitions enforce domain integrity constraints
Chapter 5 8
Integrity Constraints
Referential Integrity–rule states that 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–don’t 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
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 9
Figure 5-5
Referential integrity constraints (Pine Valley Furniture)
Referential
integrity
constraints are
drawn via arrows
from dependent to
parent table
Chapter 5 10
Figure 5-6 SQL table definitions
Referential
integrity
constraints are
implemented with
foreign key to
primary key
references
Chapter 5 11
Transforming EER Diagrams into
Relations
Chapter 5 12
Figure 5-8 Mapping a regular entity
(a) CUSTOMER
entity type with
simple
attributes
Chapter 5 13
Figure 5-9 Mapping a composite attribute
(a) CUSTOMER
entity type with
composite
attribute
Chapter 5 14
Figure 5-10 Mapping an entity with a multivalued attribute
(a)
(b)
Chapter 5 16
Figure 5-11 Example of mapping a weak entity
Chapter 5 17
Figure 5-11 Example of mapping a weak entity (cont.)
Chapter 5 18
Transforming EER Diagrams into
Relations (cont.)
Chapter 5 19
Figure 5-12 Example of mapping a 1:M relationship
a) Relationship between customers and orders
Chapter 5 20
Figure 5-13 Example of mapping an M:N relationship
a) Completes relationship (M:N)
Chapter 5 21
Figure 5-13 Example of mapping an M:N relationship (cont.)
b) Three resulting relations
Foreign key
New
Foreign key intersection
relation
Chapter 5 22
Figure 5-14 Example of mapping a binary 1:1 relationship
a) In charge relationship (1:1)
Chapter 5 23
Figure 5-14 Example of mapping a binary 1:1 relationship (cont.)
b) Resulting relations
Chapter 5 24
Transforming EER Diagrams into
Relations (cont.)
Chapter 5 25
Figure 5-15 Example of mapping an associative entity
a) An associative entity
Chapter 5 26
Figure 5-15 Example of mapping an associative entity (cont.)
b) Three resulting relations
Chapter 5 27
Figure 5-16 Example of mapping an associative entity with
an identifier
a) SHIPMENT associative entity
Chapter 5 28
Figure 5-16 Example of mapping an associative entity with
an identifier (cont.)
b) Three resulting relations
Chapter 5 29
Transforming EER Diagrams into
Relations (cont.)
Chapter 5 30
Figure 5-17 Mapping a unary 1:N relationship
(a) EMPLOYEE
entity with unary
relationship
(b)
EMPLOYEE
relation with
recursive
foreign key
Chapter 5 31
Figure 5-18 Mapping a unary M:N relationship
(a) Bill-of-materials
relationships (M:N)
Chapter 5 32
Transforming EER Diagrams into
Relations (cont.)
Chapter 5 33
Figure 5-19 Mapping a ternary relationship
Chapter 5 34
Figure 5-19 Mapping a ternary relationship (cont.)
Chapter 5 36
Figure 5-20 Supertype/subtype relationships
Chapter 5 37
Figure 5-21
Mapping Supertype/subtype relationships to relations
Chapter 5 38
Data Normalization
Primarily a tool to validate and improve a logical design so that it
avoid unnecessary
satisfies certain constraints that
duplication of data
The process of decomposing relations with anomalies to produce
smaller, well-structured relations
Chapter 5 39
Well-Structured Relations
Chapter 5 41
Anomalies in this Table
Insertion–can’t enter a new course without having an
employee taking it
Deletion–if we remove employee 140, we lose
information about the existence of a Tax Acc class
Modification–giving a salary increase to employee 100
forces us to update multiple records
Chapter 5 43
Figure 5.22 Steps in normalization
Chapter 5 44
First Normal Form
No multivalued attributes
Every attribute value is atomic
Fig. 5-25 is not in 1st Normal Form
(multivalued attributes) it is not a relation
Fig. 5-26 is in 1st Normal form
All relations are in 1st Normal Form
Put the relation in a higher NF
Chapter 5 45
Table with multivalued attributes, not in 1st normal form
Chapter 5 46
Table with no multivalued attributes and unique rows, in 1st
normal form
Chapter 5 47
Anomalies in this Table
Insertion–if new product is ordered for order 1007 of
existing customer, customer data must be re-entered,
causing duplication
Deletion–if we delete the Dining Table from Order 1006,
we lose information concerning this item's finish and price
Update–changing the price of product ID 4 requires
update in several records
Chapter 5 49
Figure 5-27 Functional dependency diagram for INVOICE
Getting it into
Second Normal
Form
Chapter 5 52
Figure 5-29 Removing transitive dependencies
Getting it into
Third Normal
Form
Chapter 5 53
Merging Relations
View Integration–Combining entities from multiple
ER models into common relations
Issues to watch out for when merging entities from
different ER models:
Synonyms–two or more attributes with different names but
same meaning
Homonyms–attributes with same name but different
meanings (Phone: mobile/home?)
Transitive dependencies–even if relations are in 3NF prior
to merging, they may not be after merging
Supertype/subtype relationships–may be hidden prior to
merging
Chapter 5 54
Enterprise Keys
Chapter 5 55
Figure 5-31 Enterprise keys
a) Relations with
enterprise key
b) Sample data
with enterprise
key
Chapter 5 56