Session 7
Designing a Database
(Part 2 – some
gentle repetition)
ITE 252 international, open membership, notfor-profit technology standards
Database
Management
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
1
Homework
• HW#1:
Create an E-R diagram modeled after the
Good Reading Bookstores E-R diagram
(Figure 3-19 in your text and slide 14 here) for
international, open membership, nota publisher
of music (limit to MP3s). Draw
for-profit technology standards
the diagram
by hand and write up a
consortium.
description similar to the one in section
3.4.3 (pp.95-95) in your text.
• Some are outstanding
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
2
Designing Relational Tables
• Typical process for converting E-R
diagrams to relational tables:
– Each entity converts to table
– For many-to-many relationships, each
associative entity converts to table
– Attributes become table columns
– Ensure foreign keys appear in proper places in
tables
• To convert single entity, create table of
same name with column for each
attribute
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
3
Salesperson Entity and Table
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
4
Converting Binary Relationships
• Greater importance in selecting identifier and
primary key
– Identifier and primary key define foreign key that
establishes relationships between tables
international, open membership, not• Often more
than one way to represent entity
for-profit technology standards
relationships
as relational tables
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
5
Example: Converting 1-1 Binary Relationship
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
6
Example: Converting 1-1 Binary Relationship
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
7
Example: Converting 1-1 Binary Relationship
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
8
Example: Converting 1-1 Binary Relationship
• Converting one-to-one Salesperson/Office
relationship
– Options:
• Convert relationship to single/combined table
international, open membership, not• Convert relationship to two tables
for-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
9
Example: Converting 1-1 Binary Relationship
• Considerations:
– Business environment considers Salesperson and Office
as separate entities
– Modality of zero at Salesperson entity in E-R diagram
(office may have no one assigned)
international, open membership, not– Salesperson
entity
in E-R standards
diagram has relationships
for-profit
technology
with other
entities
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
10
Example: Converting 1-1 Binary Relationship
• Solution 1:
– Combine two entities into one table
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
11
Example: Converting 1-1 Binary Relationship
• Solution 2:
– Two separate tables
– Office Number as foreign key in Salesperson
table
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
12
Example: Converting 1-1 Binary Relationship
• Solution 3:
– Two separate tables
– Salesperson Number as foreign key in Office
table
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
13
Example: Converting 1-M Binary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
14
Example: Converting 1-M Binary Relationship
• Each occurrence of Salesperson is related
to zero or more occurrences of
Customer
• Unique identifier of entity on “one” side
is placed as foreign key in entity of “many”
side
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
15
Example: Converting 1-M Binary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
16
Example: Converting M-M Binary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
17
Example: Converting M-M Binary Relationship
• Most relational DBMS systems DO NOT
directly support many-to-many relationships
• Solution: Include associative entity to
establish relationship
• May use composite key as primary key in
associative entity
• A key that is defined by multiple columns is
called a composite key
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
18
Example: Converting M-M Binary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
19
Example: Converting M-M Binary Relationship
not required
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
20
Example: Converting 1-1 Unary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
21
Example: Converting 1-1 Unary Relationship
• With:
– Only one entity type involved, and
– One-to-one relationship
• Conversion requires only one table
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
22
Example: Converting 1-M Unary Relationship
each salesperson is managed
by a manager
each salesperson manages
zero or more salespersons
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
23
Example: Converting 1-M Unary Relationship
each salesperson is managed
by a manager
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
24
Example: Converting M-M Unary Relationship
• Many-to-many unary relationship:
– For example: one product can be constructed
out of set or subset of other products
– General rule in conversion:
• Number of tables equal to number of entity types
plus one more table for many-to-many relationship
– M-M unary relationship - 2 tables
– M-M binary relationship - 3 tables
– M-M ternary relationship - 4 four tables
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
25
Example: Converting M-M Unary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
26
Example: Converting M-M Unary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
27
Homework
• Read Chapter 4.1- pp.104-116
• Read several times!
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
28
Key Terms
• Composite key - A key that is defined by multiple columns
• Data integrity - Ensuring that data values are properly
entered and maintained
• Defining association - A means of expressing that the
value of one particular attribute is associated with a single,
specific value of another attribute
• Null value - An undefined value, usually used to identify that
no value is provided for that attribute
• Referential integrity - Another name for relational
integrity, referring to the fact that you are maintaining the
relationship between referencing and referenced tables.
• Relational integrity - Ensuring that relationships between
tables are correctly created and maintained.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
29
Q&A
Which statement best describes the process of
converting a one-to-one binary relationship to
related tables?
a) You should
convert
the
entities
into
a
single
international, open membership, notrelated table.
for-profit technology standards
consortium.
b) There will
typically be only one way to define
the tables.
c) There will typically be more than one way to
define the tables.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
30
Q&A
Which statement best describes the process of
converting a one-to-one binary relationship to
related tables?
a) You should
convert
the
entities
into
a
single
international, open membership, notrelated table.
for-profit technology standards
consortium.
b) There will
typically be only one way to define
the tables.
c) There will typically be more than one
way to define the tables.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
31
Q&A
You are converting a many-to-many unary
relationship. How many relational tables will
result from this conversion?
international, open membership, nota) one
for-profit technology standards
b) two
consortium.
c) three
d) four
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
32
Q&A
You are converting a many-to-many unary
relationship. How many relational tables will
result from this conversion?
international, open membership, nota) one
for-profit technology standards
b) two consortium.
c) three
d) four
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
33
Q&A
Manager-to-Employee is a one-to-many unary
relationship.You create an EMPLOYEE table.
How any additional tables do you need?
a) none international, open membership, notfor-profit technology standards
b) one
consortium.
c) two
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
34
Q&A
Manager-to-Employee is a one-to-many unary
relationship.You create an EMPLOYEE table.
How any additional tables do you need?
a) none international, open membership, notfor-profit technology standards
b) one
consortium.
c) two
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
35
Q&A
• Data integrity ensures that data is entered and
stored correctly. True or False?
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
36
Q&A
• Data integrity ensures that data is entered and
stored correctly. True or False?
international, open membership, notfor-profit technology standards
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
37
Example: Good Reading Bookstores
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
38
Example: Good Reading Bookstores
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
39
Example: Good Reading Bookstores
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
40
Normalizing Data
• Data normalization: Methodology for organizing
attributes into tables to eliminate redundancy
among non-key attributes
• Goals:
international, open membership, not– Each resultant
describes
single entity type or
for-profittable
technology
standards
single many-to-many
relationship
consortium.
– Foreign keys appear exactly where needed
– Properly structured relational database
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
41
Normalization Techniques
•
Two types of input needed for data
normalization process
1. List of all attributes to be incorporated in database, including
intersection data attributes
2. List of functional dependencies: all defining associations
between
attributes open membership, notinternational,
•
for-profit
technology standards
In functional
dependencies,
one attribute
consortium.
(determinate attribute – left side) defines value of
another attribute – functionally dependent on the
left side
Salesperson Number → Salesperson Name
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
42
Example: Converting M-M Binary Relationship
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
43
Entity Attributes
• Assume the salespersons are organized into
departments and that each department
has a manger who is not herself a sales
person international, open membership, notfor-profit technology standards
• We get this
list of attributes…
consortium.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
44
Salesperson Entity Attributes
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
45
Salesperson Entity
Functional Dependencies
(Defining Associations)
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
46
Example: Defining Attributes and
Functional Dependencies
• Quantity is defined by two combined
attributes
• Manager is defined independently by two
attributes
– Department Number and Salesperson Number
• Salesperson Number also defines Department
Number
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
47
Normalizing Data
•
•
Normal forms: Rules for data normalization
Three main normal forms
–
–
–
•
international, open membership, notNormalization:
–
•
•
First normal form
Second normal form
Third normal form
for-profit
technology
standards process” that
Uses normal
forms to step
through “decomposition
decomposes
attributes into subgroups
consortium.
In third normal form, group of tables is well-structured
relational database with no data redundancy
Goal: no data redundancy
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
48
Normalizing Data
First normal form:
•
–
Eliminates multiple values
Second normal form:
•
–
–
Eliminates partial functional dependencies (data
dependent on part of primary key)
Every nonkey attribute must be fully functionally dependent
on entire key of table
Third normal form:
•
–
–
Eliminates transitive dependencies (one nonkey
attribute is functionally dependent on another)
Nonkey attributes are not allowed to define other nonkey
attributes
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
49
Example: Unnormalized Data
multiple
values
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
50
First Normal Form
multiple
values
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
51
Second Normal Form
eliminate partial
functional
dependencies
Prof. Vincent Costa
(See figure 4-29
in text; some
redundant data)
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
52
Third Normal Form
(See figure 4-31
in text; notice
DEPARTMENT table)
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
53
Denormalizing Data
• Denormalizing may be needed when:
– Normalization has been taken to extreme
• Too many small tables creating more work and storage space
– E.g. Using State table to be referenced instead of entering two-digit
code)
international, open membership, not– More efficient
data
retrieval
is needed:
for-profit
technology
standards
consortium.
• Many queries
requiring resource-intensive joining
• In denormalizing, you join two or more tables
into one less normalized table
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
54
Summary
• In converting E-R diagrams to relational tables, each
entity typically converted into table, with attributes as
table columns.
• Considerations in conversion: Business needs, cardinalities,
modalities, and defining foreign keys to establish relationships.
• Normalization:
Uses three
main normal
international,
open membership,
not- forms to step
through “decomposing”
attributes
into subgroups that allow
for-profit technology
standards
data redundancies to be eliminated.
consortium.
• Denormalizing may be needed in cases where storage
space and speed of data retrieval are important factors.
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
55
Key Terms
• Data normalization - A methodology for organizing
attributes into tables so that redundancy among the nonkey
attributes is eliminated
• Decomposition process - Another term for the data
normalization process. Also called “non-loss decomposition”
• Determinant attribute - The attribute in a defining
association whose value determines the value of the
attribute on the right side of the association
• Exception conditions - Less commonly used normal
forms
• First normal form - Each attribute value is atomic, that
is, no attribute is multivalued
• Functional dependency - See Defining association
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
56
Key Terms
• Joining - Combining the data from two tables based on
linking columns
• Non-loss decomposition - Normalization process in
which neither data nor relationships are lost
• Normal forms - Defined rules for data normalization
• Partial functional dependency - A dependency where
data is dependent on part of the primary key
• Second normal form - Every nonkey attribute must be
fully functionally dependent on the entire key
• Third normal form (3NF) - Transitive dependencies are
not allowed
• Transitive dependency - One nonkey attribute is
functionally dependent
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
57
Important Links
• Database Normalization - good description
from Wikipedia
• An Introduction to Database Normalization –
notif you caninternational,
get past open
the membership,
computerized
female
for-profit technology standards
voice, it’s consortium.
a pretty good overview
• Database Normalizaion Basics – Microsoft
Overview
Prof. Vincent Costa
Nassau Community College
Acknowledgements: Introduction to
Database Management, All Rights Reserved
58