07 - Modeling Data in Organization
07 - Modeling Data in Organization
1
A Good Business Rule is:
Declarative – what, not how
Precise – clear, agreed-upon meaning
Atomic – one statement
Consistent – internally and externally
Expressible – structured, natural language
Distinct – non-redundant
Business-oriented – understood by
business people
2
A Good Data Name is:
Related to business, not technical,
characteristics
Meaningful and self-documenting
Unique
Readable
Composed of words from an approved list
Repeatable
Chapter 3 3
Overview of db design
Requirement analysis
– Data to be stored
– Applications to be built
– Operations (most frequent) subject to performance requirement
Conceptual db design
– Description of the data (including constraints)
– By high level model such as ER
Logical db design
– Choose DBMS to implement
– Convert conceptual db design into database schema
Beyond ER design
Schema refinement (normalization)
Physical db design
– Analyze the workload
– Refine db design to meet performance criteria (focus on Indexing)
Security design
4
A
simplified
diagram to
illustrate
the main
phases of
database
design.
Chapter 3 5
Conceptual design
Issues to consider: (ER Model is used at this stage.)
What are the entities and relationships in the enterprise?
What information about these entities and relationships
should we store in the database (i.e., attributes)?
What are the integrity constraints or business rules that
hold?
Solution:
Can map an ER diagram into a relational schema.
Chapter 3 6
E-R Model Constructs
Entities:
Entity instance–person, place, object, event, concept (often corresponds to a
row in a table)
Entity Type–collection of entities (often corresponds to a table)
Relationships:
Relationship instance–link between entities (corresponds to primary key-
foreign key equivalencies in related tables)
Relationship type–category of relationship…link between entity types
Chapter 3 7
ER Model Concepts
Entities and Attributes
Entities are specific objects or things in the mini-
world that are represented in the database.
For example the EMPLOYEE John Smith, the Research
DEPARTMENT, the Airline Client
OR
Entities are Real-world object distinguishable from other objects.
An entity is described (in DB) using a set of
attributes.
name
ssn address
Employee
Chapter 3 8
Sample E-R Diagram (Figure 3-1)
9
Basic E-R notation (Figure 3-2)
Entity
Attribute
symbols
symbols
A special entity
that is also a Relationship
relationship symbols
Relationship
degrees specify
number of
entity types Relationship
involved cardinalities
specify how
many of each
entity type is
allowed
10
Figure 3-4 Example of inappropriate entities
System System
user Inappropriate output
entities
Appropriate
entities
Chapter 3
ER Model Concepts
Attributes
Are properties used to describe an entity.
For example an EMPLOYEE entity may have a
Name, SSN, Address, Sex, BirthDate
A specific entity will have a value for each of its
attributes.
For example a specific employee entity may have
Name='John Smith', SSN='123456789', Address ='731,
Fondren, Houston, TX', Sex='M', BirthDate='09-JAN-55‘
Each attribute has a value set (or data type)
associated with it – e.g. integer, string, subrange,
enumerated type, …
Chapter 3 12
Types of Attributes (1)
Simple
Each entity has a single atomic value for the attribute. For example,
SSN or Email.
Composite
The attribute may be composed of several components.
components For example,
Address (Apt#, House#, Street, City, State, ZipCode, Country)
Name (FirstName, MiddleName, LastName).
Composition may form a hierarchy where some components are
themselves composite.
Multi-valued
An entity may have multiple values for that attribute. For example,
Color of a CAR (Blue, White, ..)
PreviousDegrees of a STUDENT.
Denoted as {Color} or {PreviousDegrees}.
Chapter 3 13
Types of Attributes (2)
In general, composite and multi-valued attributes
may be nested arbitrarily to any number of levels
although this is rare. E.g.
PreviousDegrees of a STUDENT is a composite multi-
valued attribute denoted by {PreviousDegrees (College,
Year, Degree, Field)}.
Address
Chapter 3 14
Figure 3-7 A composite attribute
An attribute
broken into
component parts
Multivalued
an employee can have
Derived
more than one skill
from date
employed and
current date
15
Entity Types and Key Attributes
Entities with the same basic attributes are grouped or typed into an entity
type.
type For example, the EMPLOYEE entity type or the PROJECT entity type.
Entity Set: A collection of similar entities. E.g., all employees.
An attribute of an entity type for which each entity must have a unique value is
called a key attribute of the entity type. For example, SSN of EMPLOYEE.
A key attribute may be composite. For example, VehicleTagNumber is a key of
the CAR entity type with components (Number, State).
An entity type may have more than one key. For example, the CAR entity type
may have two keys:
VehicleIdentificationNumber (popularly called VIN) and
VehicleTagNumber (Number, State), also known as license_plate number.
Chapter 3 16
Identifiers (Keys)
Identifier (Key) –An attribute (or combination
of attributes) that uniquely identifies individual
instances of an entity type
Simple versus Composite Identifier
Candidate Identifier/Key –an attribute that
could be a key…satisfies the requirements for
being an identifier
Chapter 3 17
Identifiers / Key
A key is a minimal set of attributes whose
values uniquely identify an entity in the
set.
Candidate key.
Primary key.
Chapter 3 18
Characteristics of Identifiers/Keys
Will not change in value
Will not be null
No intelligent identifiers (e.g., containing locations or
people that might change)
Substitute new, simple keys for long, composite keys
Chapter 3 19
Figure 3-9 Simple and composite identifier attributes
20
Figure 3-19 Simple example of time-stamping
This attribute
that is both
multivalued and
composite
21
ENTITY SET corresponding to the
ENTITY TYPE CAR
Chapter
22 3
Entity, Entity Set, Attribute, and
Schema
ID or SSN Name UserID Age GPA
999-38-4431 John Smith jsmith 21 3.68
Chapter 3 23
Entity
Entity is basically a single
ROW in the table.
Attributes
Attributes are columns in
the database table
Chapter 3 24
More on Relationships
Relationship Types vs. Relationship Instances
The relationship type is modeled as lines between entity types…
the instance is between specific entity instances
Relationships can have attributes
These describe features pertaining to the association between the entities in
the relationship
Two entities can have more than one type of relationship
between them (multiple relationships)
Associative Entity –combination of relationship and
entity
Chapter 3 25
Figure 3-10 Relationship types and instances
a) Relationship type
b) Relationship
instances
26
Example of Keys
Chapter 3 27
Relationship vs. Relationship Set
John Smith
Relationship
ITCS3160
Chapter 3 28
Relationship vs. Relationship Set
999-21-3415, jsmith@, John Smith, 18, 3.5 Students
999-31-2356, jzhang@, Jie Zhang, 20, 3.0
999-32-1234, ajain@, Anil Jain, 21, 3.8
Chapter 3 29
Example 1
Build an ER Diagram for the following
information:
Students
Have an Id, Name, Login, Age, GPA
Courses
Have an Id, Name, Credit Hours
Students enroll in courses
Receive a grade
Chapter 3 30
Example 1 Answer
Name Login Age Id Name Credit
Id GPA
Students Courses
Enrolled_In
Grade
Chapter 3 31
Example 2
Build an ER Diagram for the following
information:
Patients
Name, Address, Phone #, Age
Drugs
Name, Manufacturer , Expiration Date
Patients are prescribed drugs
Dosage, # Days
Chapter 3 32
Example 2 Answer
Age
Patients Drug
Prescribed
Dosage #days
Chapter 3 33
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 34
Degree of relationships – from Figure 3-2
Entities of
One entity two different
related to types related
another of to each other Entities of three
the same different types
entity type related to each
other
35
Cardinality of Relationships
One-to-One
Each entity in the relationship will have exactly one related
entity
One-to-Many
An entity on one side of the relationship can have many
related entities, but an entity on the other side will have a
maximum of one related entity
Many-to-Many
Entities on both sides of the relationship can have many
related entities on the other side
Chapter 3 36
Potential Relationship Types
Chapter 3 37
Many-to-one (N:1) RELATIONSHIP
EMPLOYEE WORKS_FOR DEPARTMENT
r1
e1 d1
r2
e2
e3 r3 d2
e4 r4
d3
e5 r5
e6
r6
e7
r7
Chapter
38 3
Many-to-many (M:N) RELATIONSHIP
Chapter 3 39
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 40
Figure 3-12 Examples of relationships of different degrees
a) Unary relationships
41
Figure 3-12 Examples of relationships of different degrees (cont.)
b) Binary relationships
42
Figure 3-12 Examples of relationships of different degrees (cont.)
c) Ternary relationship
a) Mandatory cardinalities
44
Figure 3-17 Examples of cardinality constraints (cont.)
45
Figure 3-17 Examples of cardinality constraints (cont.)
a) Optional cardinalities
A person is is
married to at most
one other person,
or may not be
married at all
46
Figure 3-21 Examples of multiple relationships
47
Figure 3-21 Examples of multiple relationships (cont.)
Here, min
cardinality
constraint is 2
48
Participation Constraints
The participation constraint specifies whether the
existence of an entity depends on its being related
to another entity via the relationship type.
The constraint specifies the minimum number of
relationship instances that each entity can
participate in.
There are two types of participation constraints:
Total
Partial
Chapter 3 49
Types of Participation Constraints
Total:
If an entity can exist, only if it participates in at least one
relationship instance, then that is called total participation,
meaning that every entity in one set, must be related to at least
one entity in a designated entity set.
An example would be the Employee and Department
relationship. If company policy states that every employee
must work for a department, then an employee can exist only
if it participates in at lest one relationship instance (i.e. an
employee can’t exist without a department)
It is also sometimes called an existence dependency.
Total participation is represented by a double line, line going
from the relationship to the dependent entity.
Chapter 3 50
Types of Participation Constraints
Partial:
If only a part of the set of entities participate in a
relationship, then it is called partial participation.
Using the Company example, every employee will
not be a manager of a department,
so the participation of an employee in the
“Manages” relationship is partial.
partial
Partial participation is represented by a single line.
Chapter 3 51
Participation Constraints
Every “did” value in Departments table must appear in a row
of the Manages table (with a non-null ssn value!)
since
name dname
ssn lot did budget
since
Chapter 3 52
Attributes of Relationship Types
Relationships can have attributes similar to entity types.
For example, in the relationship Works_On, between the
Employee entity and the Department entity we would like to keep
track of the number of hours an employee works on a project.
Therefore we can include “Number_of_ Hours” as an attribute of
the relationship.
Another example is for the “manages” relationship between
employee and department, we can add “Start_Date” as an
attribute of the Manages relationship.
For some relationships (1:1, or 1:N), the attribute can be
placed on one of the participating entity types.
For example the “Manages” relationship is 1:1, StartDate can
either be migrated to Employee or Department.
Chapter 3 53
Weak Entity
“An entity set may not have sufficient attributes to
form a PRIMARY KEY and is fully existent
dependant on Strong Entity. Such entity set is termed
as WEAK ENTITY SET”.
Chapter 3 54
Weak Entity Types
An entity that does not have a key attribute
Entities that belong to a weak entity type are identified by being
related to specific entities from another entity type in combination
with one of their attribute values.
This entity type is called an identifying or owner entity type.
The relationship that relates the identifying entity type with the weak entity
type is called an identifying relationship.
Example:
Suppose that a DEPENDENT entity is identified by the dependent’s first name
and birhtdate, and the specific EMPLOYEE that the dependent is related to.
DEPENDENT is a weak entity type with EMPLOYEE as its identifying entity
type via the identifying relationship type DEPENDENT_OF
Chapter 3 55
Weak Entity Types
A weak entity must participate in an identifying relationship type
with an owner or identifying entity type
A weak entity type always has a total participation constraint with respect
to the identifying relationship, because a weak entity cannot exist without
its owner.
Not all existence dependencies (Total participation ) result in a
weak entity type; if an entity has a key attribute then it is not a
weak entity.
A weak entity type usually has a partial key, which is the set of
attributes that can uniquely identify weak entities that are related
to the same owner entity
Entities are identified by the combination of:
A partial key of the weak entity type
The particular entity they are related to in the identifying
entity type
Chapter 3 56
Example - Weak Entity Types (1)
For example, lets assume in a library database, we have an
entity type Book.
Book For each book, we keep track of the
author, ISBN, and title. The library may own several
copies of the same book, and for each copy,
copy it keeps track
of the copy number (a different copy number for each
copy of a given book) and price of each copy.
Chapter 3 57
Example - Weak Entity Types (1)
Because the copy number is only unique for each book (meaning
Book 123 may have copy 1, copy 2, copy 3, and book 456 may
also have copy 1, copy 2 and copy 3) and not for all copies of all
books, it cannot be considered unique for each copy.
Therefore because the Copy entity does not have a key attribute,
it is considered a weak entity type, an is identified by being
related to the Book entity.
entity The book entity is the identifying
entity, and the relationship is the identifying relationship.
Because a copy cannot exist without the owner (Book) the Copy
entity type has a total participation constraint with respect to the
identifying relationship.
The partial key of the Copy entity is Copy Number, for each
owner entity Book, the Copy Number uniquely identifies the
copy.
Chapter 3 58
SUMMARY OF
ER-DIAGRAM
NOTATION
FOR ER
SCHEMAS
Chapter 3 59
An ER schema diagram for the
COMPANY database.
Chapter 3 61
Foreign Key contd..
Example:
In Faculty Class relationship, FacID can appear
as Foregin Key in Class. As FacID is a primary Key
in faculty table.
Chapter 3 62
One to many
“Each entity in X is associated with many
entities in Y and each entity in Y associated
with at most one entity in X”.
. .
. .
. .
Chapter 3 63
One to many
Smith . . JAVA
Jane . . C++
Kyle . . Databases
64
Many to one (Exactly same as one-many)
“Each entity in X is associated with at most one entity in Y and
each entity in Y associated with many entities in X”.
.
. .
. .
. .
.
Chapter 3 65
Many to one
.
. .
. .
. .
.
Chapter 3 66
Many to many
“Each entity in X is associated with many entities in Y and each
entity in Y associated with many entities in X”.
. .
. .
. .
. .
. .
Chapter 3 67
Many to many
. .
. .
. .
. .
. .
Chapter 3 68
Many to many
For Many to many relationship
we make a bridge table.
table
That contains PRIMARY Keys
of both sides
Along some extra required attributes
Chapter 3
One to one
If each entity is associated with at most one entity.
“Each entity in X is associated with at most one entity in Y and
each entity in Y associated with at most one entity in X”.
S101 M_01
S102 M_02
S103 M_03
Chapter 3 70
For ONE to ONE Relationship
if one side is optional
then Foreign Key will be on
optional side
71
One-to-One Relationships
One employee has one employment history; no
others have same history
Chapter
72 3
One-to-Many Relationships
One building can have many apartments, but
one apartment is only in one building
73
One-to-Many Relationships
One department can have many projects
Chapter 3 74
Weak Entity (once again)
Example:
Case: Payments of loan.
pay-date
Loan-No Amount
pay-num
amount
loan LP payment
Chapter 3 76
Weak Entity (once again)
Identifying Relationship
loan R payment
Chapter
77 3
Weak Entity (once again)
1:N
1:N
loan R payment
Chapter
78 3
Relationships and Relationship Types (3)
We can also have a recursive relationship type.
Both participations are same entity type in different roles.
roles
For example, SUPERVISION relationships between
EMPLOYEE (in role of supervisor or boss) and (another)
EMPLOYEE (in role of subordinate or worker).
In ER diagram, need to display role names to distinguish
participations.
Chapter 3 79
A RECURSIVE RELATIONSHIP
SUPERVISION
EMPLOYEE SUPERVISION
e1 2
1 r1
e2 2
1
r2
e3 2
1
e4 2 r3
1
e5 1
2 r4
e6 1
2 r5
e7
In figure, first role participation labeled with 1
and second role participation labeled with 2. r6
Chapter
80 3
Recursive Relationship Type is: SUPERVISION
(participation role names are shown)
Chapter
81 3
Structural Constraints –
one way to express semantics of relationships
Structural constraints on relationships:
Cardinality ratio (of a binary relationship): 1:1, 1:N,
N:1, or M:N
SHOWN BY PLACING APPROPRIATE NUMBER ON
THE LINK.
Participation constraint (on each participating
entity type): total (called existence dependency) or
partial.
SHOWN BY DOUBLE LINING THE LINK
NOTE: These are easy to specify for Binary
RelationshipTypes.
Chapter 3 82
Alternative (min, max) notation for
relationship structural constraints:
Specified on each participation of an entity type E in a relationship type R
Specifies that each entity e in E participates in at least min and at most
max relationship instances in R
Default(no constraint): min=0, max=n
Must have minmax, min0, max 1
Derived from the knowledge of mini-world constraints
Examples:
A department has exactly one manager and an employee can manage at
most one department.
– Specify (0,1) for participation of EMPLOYEE in MANAGES
– Specify (1,1) for participation of DEPARTMENT in MANAGES
An employee can work for exactly one department but a department can
have any number of employees.
– Specify (1,1) for participation of EMPLOYEE in WORKS_FOR
– Specify (0,n) for participation of DEPARTMENT in WORKS_FOR
Chapter 3 83
The (min,max) notation relationship
constraints
(0,1) (1,1)
(1,1) (1,N)
Chapter 3 84
COMPANY ER Schema Diagram
using (min, max) notation
Chapter 3 85
Data Modeling Tools
A number of popular tools that cover conceptual
modeling and mapping into relational schema design.
Examples: ERWin, S- Designer (Enterprise
Application Suite), ER- Studio, etc.
POSITIVES: serves as documentation of application
requirements, easy user interface - mostly graphics editor
support
Chapter 3 86
Problems with Current Modeling Tools
DIAGRAMMING
Poor conceptual meaningful notation.
To avoid the problem of layout algorithms and aesthetics
of diagrams, they prefer boxes and lines and do nothing
more than represent (primary-foreign key) relationships
among resulting tables.(a few exceptions)
METHODOLGY
lack of built-in methodology support.
poor tradeoff analysis or user-driven design preferences.
poor design verification and suggestions for improvement.
Chapter 3 87
Some of the Currently Available Automated Database
Design Tools
Chapter 3 89
Extended Entity-Relationship (EER) Model
Incorporates Set-subset relationships
Incorporates Specialization/Generalization Hierarchies
Chapter 3 90
MORE PRATICE EXAMPLES
Chapter 3 91
Designing an ER Diagram
Consider the following set of requirements for a University database. Design an ER diagram
for this application:
The university keeps track of each student's name, student number, social security number,
current address and phone number, permanent address and phone number, birthdate, sex,
class (freshman, graduate), major department, minor department (if any), degree program
(B.A., B.S., ... Ph.D.). Some user applications need to refer to the city, state, and zip code
of the student's permanent address and to the student's last name. Both social security
number and student number are unique for each student. All students will have at least a
major department.
Each department is described by a name, department code, office number, office phone, and
college. Both the name and code have unique values for each department.
Each course has a course name, description, course number, number of credits, level and
offering department. The course number is unique for each course.
Each section has an instructor, semester, year, course, and section number. The section
number distinguishes sections of the same course that are taught during the same
semester/year; its value is an integer (1, 2, 3, ... up to the number of sections taught during
each semester).
A grade report must be generated for each student that lists the section, letter grade, and
numeric grade (0,1,2,3, or 4) for each student and calculates his or her average GPA.
Chapter 3 92
University ER Diagram
Address
CName
Grade_Report
CourseDesc
Letter Grade Instructor Year Course
CNumber
GPA
Credits
Numeric Grade Section Belong_To
SectionNumber Semester
Chapter
93 3
ER DIAGRAM FOR A BANK DATABASE
© The Benjamin/Cummings Publishing Company, Inc. 1994, Elmasri/Navathe, Fundamentals of Database Systems, Second Edition
Chapter
94 3
ER DIAGRAM FOR A BANK DATABASE
Consider the ER model shown in previous slide for
part of BANK database. Where each bank can have
multiple branches, and each branch can have multiple
accounts and loans.
List non weak, weak and identifying entity types
For weak entity what is the relationship that it has with other
entities and of what type.
List concisely the user requirements that led to this ER
schema diagram
Chapter 3 95
Figure 3-15a and 3-15b Multivalued attributes can be represented as relationships
simple
composite
96
Associative Entities
An entity –has attributes
A relationship –links entities together
The database relational model doesn't offer direct support to
many-to-many relationships, even though such relationships
happen frequently in normal usage.
The solution to this problem is the creation of another table to hold
the necessary information for this relationship. This new table is
called an associative entity.
An associative entity can be thought of as both an entity and a
relationship since it encapsulates properties from both. It is a
relationship since it is serving to join two or more entities together,
but it is also an entity since it may have its own properties.
The associative entity must have identifiers, Primary Keys, for
both adjoining tables, but may also contain its own unique
identifier and other information about the relationship.
Chapter 3 97
Associative Entities
When should a relationship with attributes instead be
an associative entity?
All relationships for the associative entity should be many to
many
The associative entity could have meaning independent of the
other entities
The associative entity preferably has a unique identifier, and
should also have other attributes
The associative entity may participate in other relationships other
than the entities of the associated relationship
Ternary relationships should be converted to associative entities
Chapter 3 98
Figure 3-11a A binary relationship with an attribute
99
Figure 3-11b An associative entity (CERTIFICATE)
101
Figure 3-18 Ternary relationship as an associative entity
102
Microsoft Visio
Notation for Pine
Valley Furniture
E-R diagram
Different modeling
software tools may have
different notation for the
same constructs
103
END
Chapter 3 104