Part2 - Ch3 - Data Modeling Using The Entity Relationship Model
Part2 - Ch3 - Data Modeling Using The Entity Relationship Model
Outline:
3.1 Design Phases.
Phase 1: Requirements collection and analysis.
Phase 2: Conceptual Design.
Phase 3: Logical design (data model mapping).
Phase 4: Physical design.
1
3.1 Design Phases
Phase 1: Requirements collection and analysis
Interview prospective database users to understand and document their data requirements
The result of this phase is concisely written set of users’ requirements and functional
requirements of the application; consist of the user defined operations (or transactions) that will
be applied to the database, and they include both retrievals and updates).
Conceptual schema for the database, using High-Level Conceptual Data Model (entity types,
relationships, and constraints).Because these concepts do not include implementation details,
they are usually easier to understand and can used to communicate with no technical users. The
high level conceptual schema can also be used as a reference to ensure that all users' data
requirements are met and that the requirements do not include conflicts.
The result is Conceptual Schema in high level model
The internal storage structures, access paths, and file organizations for the database files are
specified.
2
Mini-World
Database Requirements
DBMS-independent
Conceptual Data Model
DBMS-specific
Phase3: Logical Design
3
3.2 The Entity-Relationship (ER) Model
Used for conceptual database design
ER Model describes data as:
o Entities
o Relationships
o Attributes
Entity: An entity is a "thing" (a being, an object, an event) in the real world and is
distinguishable from other objects (things).
Entity types represent set (or collection) of entities that have the same attributes (for example,
employee entity type and company entity type). An entity is an instance of an entity type. The
Entity type is pictured by rectangular nodes
Entity Type
Figure 3: Two entity types, EMPLOYEE and COMPANY, and some member entities of each.
4
An entity set is the collection of all entities of a particular entity type in the database at
any point in time
Characteristic of Entity:
Existence: It may have a physical existence (also called concrete) (for example, car,
house, or an employee) in the real world or it may be an object with a conceptual
existence (abstract). (for example, a job, or a university course).
Described by its attributes (set of properties); for example an EMPLOYEE entity may
have a Name, SSN, Address, Sex, BirthDate.
Each attribute is drawn from some domain (such as integers) possibly augmented by
NULL (more about NULLs later).
Example: a specific book entity will have title= “Database System Concepts”,
pages=821, ISBN= 0-07-044756-X
Example: A person name= “Mohammed”, age=21, add=salt
Entity sets do not need to be disjoint. For example, it is possible to define the entity
set of all employees of a bank (employee) and the entity set of all customers of the bank
(customer). A person entity may be an employee entity, a customer entity, both, or
neither.
5
Types of Attributes
Several types of attributes occur in the ER model (simple Vs. composite, single-valued Vs.
multivalued, Stored Vs. Derived Attribute, complex Attribute, and key Attribute)
o Each entity has a single atomic value for the attribute; for example SSN or Gender.
Simple Attribute
A Composite
Attribute
6
o Composition may form a hierarchy where some components are themselves
composite; for example StreetAddress can be subdivided into three simple attributes,
Number, Street ,and ApartmentNumber, as shown in the following figure :
Multivalued Attribute
o Some attribute values can be derived from related attributes or entities; for example
an attribute Age of employee entity can be determined from the current date and the
value of stored attribute (BirthDate).
Derived Simple
Attribute
5. Complex Attribute
7
o example: {AddressPhone ( { Phone ( AreaCode, PhoneNumber ) }, Address (
StreetAddress ( Number, Name ), City, State, Zip ) ) }
Number Name
StreetAddress City
PhoneNumber AreaCode
State
Zip
Phone Address
AddressPhone
8
6. Key Attribute (identifier)
o An attribute whose values are distinct for each individual entity in the collection. In
other words, 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.
Key Attribute
o 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.
o The following ER diagram for car:
OR
9
The following figure contains all of the above types of attributes:
Null Values
o In some cases a particular entity may not have an applicable value for an attribute.
Example, the ApartmentNumber attribute of an address applies only to addresses that
are in apartment buildings and not to single-family homes.
o If we do not know the value of an attribute for a particular entity. The unknown
category of null can be further classified into two cases:
The first case arises when it is known that the attribute value is applicable, but
it is missing—for example, if the height attribute of a person is listed as null.
The second case arises when it is not known whether the attribute value
applicable —for example, if the HomePhone attribute of a person is null.
Example: Appartment-Number attribute:
o Not applicable: if the address does not include a apartment-number.
o Missing: an apartment-number exists but we do not know what it is.
o Unknown: we do not know whether or not an apartment-number is part of the
customer’s address
10
Relationship
A relationship is an association among at least two entities belonging to one or more entity
sets. For example, EMPLOYEE Sami works for IT DEPARTMENT.
Relationship
Type
Example:
11
Department
Employee
A relationship set is the collection of instances (i.e., relationships between objects) represented
by a relationship type.
Relationship types may associate an entity type with itself. In such a case, the roles of the entity
types in the relationship type are listed on the edges, and the relationship is said to be recursive.
12
Pre-
requisite
Course
More than one relationship type can exist with the same participating entity types; for example
the WORKS_FOR relationship type in which EMPLOYEEs and DEPARTMENT participate,
or the MANAGES relationship type in which EMPLOYEEs and DEPARTMENTs participate.
Works
for
Employee Department
Manage
Example: We can have two binary relationship types associating the student and team types,
TeamMemberOf and LeaderOf. In the former case, a student entity is a member of a team
entity; in the later case, a student can be a leader of a team.
Member
of
Student Team
Leader
of
13
Hours/week
o Example:
Since
Price
Figure (1)
Price
Figure (2)
14
The degree of a relationship type
1. Unary: related to another of the same entity type. Also called recursive relationships.
Supervise
Supervisor Supervisee
Employee
15
2. Binary: entities of two different types related to each other. (Two participating entities).
A Department
Employee has Works for Many Employee
Department
3. Ternary: entities of three different types related to each other. (three participating
entities).
PartNo#
Part
Suppid Pname
Quantity
16
Figure 17: Ternary Relationship set example.
Example:
Drug
Doctor Prescribe
Patient
4. n-ary: entities of more than three different types related to each other.
17
Entities Vs Attributes
o We should have
Example2:
o If we want to model something about a person, such as the date of birth of a person or
whom a person likes.
o These require a person to have an attribute (date of birth) and enter into a relationship
(with other persons).
We cannot model this situation if person is an attribute of Buy.
o Similarly, for product and vendor.
18
3.3 Structural Constraints on relationship
Cardinality ratio (of a binary relationship)
Mapping cardinalities, or cardinality ratio, express the number of entities to which another
entity can be associated via a relationship set. (specifies maximum participation).
There are four types of cardinality: 1:1, 1:N, N:1, or M:N.
1. One-to-One (1:1) cardinality ratio, an entity in one set is associated with at most one entity
in another.
19
For Example:
Department
Employee
(Manager)
Abed Hossam
20
The ER Diagram is:
2. One-to-many (1: N) cardinality ratio, an entity in the first set is associated with 0 or more
entities in the second set. However, those entities in the second set can be associated with
at most one entity in the first.
For Example:
21
The ER Diagram is:
3. Many-to-one (N: 1) cardinality ratio is just the reverse of the 1:N cardinality ratio. You
can think of whichever entity set you like as being the first set. Just specify the direction
that makes sense for your application, and then be consistent.
4. Many-to-many (N:M) cardinality ratio, entities of either set may be associated with any
number of entities in the other.
22
For Example:
Projects
Jamal Sami
Employee
23
The ER Diagram is:
2.
24
If a loan can belong to only one customer, and a customer can have several
loans, then the relationship type from customer to loan is one-to-many.
If a loan can belong to several customers, the relationship type is many-to-many.
We can slide the date to the entity, if no two squares end up in the same entity as
follows:
25
Example2: Consider relationship Buys among Person, Vendor, and Product
We want to specify that a person Buys a product from a vendor at a specific
price
Price is not
A property of a vendor, because different products may be sold by the
same vendor at different prices
A property of a product, because different vendors may sell the same
product at different prices
A property of a person, because different products may be bought by the
same person at different prices
So Price is really an attribute of the relationship Buys
For each tuple (person, product, vendor) there is a value of price
26
Shown by double line.
Partial Total
ف
Figure 28: Total/Partial ER diagram.
(min1..max1) (min2..max2)
E1 R E2
27
An employee must work for exactly one department but a department can have any number of
employees.
o Specify (1..1) for participation of EMPLOYEE in WORKS_FOR
o Specify (0..*) for participation of DEPARTMENT in WORKS_FOR
(1..1) (0..*)
Employee Works for Department
A weak entity type must participate in an identifying relationship type with an owner or
identifying entity type.
A weak entity always has a total participation constraint.
Identifying relationship type is pictured by diamond with double lines.
Identifying
Relationship
Type
Partial Key
o The particular entity they are related to in the identifying entity type
A weak entity type may have more than one identifying entity type and an identifying
relationship type of degree higher than two.
28
For example:
SSN Name
o A DEPENDENT entity is identified by the dependent’s first name, and the specific
EMPLOYEE with whom the dependent is related.
Name of DEPENDENT is the partial key
DEPENDENT is a weak entity type
EMPLOYEE is its identifying entity type via the identifying relationship type
DEPENDENT OF.
Example 2:
Example 3:
A:
29
B:
We need to specify for a weak entity through which relationship it is identified; this done by
using double lines as the following example
o A woman can have several sons, each of whom can have several horses
Represent a weak entity as a regular entity type by introducing an artificial or surrogate key.
30
Some database design tools permit only binary relationships
o Ternary relationship must be represented as a weak entity type.
Consider the following example:
Figure 33: Ternary relationship types. (a) The SUPPLY relationship. (b) Three binary relationships not
equivalent to SUPPLY. (c) SUPPLY represented as a weak entity type.
31
3.5 An Example of Database Application (COMPANY Database)
The database designers stated the following description of the “miniworld”—the part of the
company to be represented in the database:
1. The company is organized into departments. Each department has a unique name, a unique
number, and a particular employee who manages the department. We keep track of the start
date when that employee began managing the department. A department may have several
locations.
2. A department controls a number of projects, each of which has a unique name, a unique
number, and a single location.
3. We store each employee’s name, social security number, address, salary, sex, and birth
date. An employee is assigned to one department but may work on several projects, which
are not necessarily controlled by the same department. We keep track of the number of
hours per week that an employee works on each project. We also keep track of the direct
supervisor of each employee.
4. We want to keep track of the dependents of each employee for insurance purposes. We
keep each dependent’s first name, sex, birth date, and relationship to the employee.
32
Figure 34: The ER Diagram for the company DB.
33
Consider the following information about a university database:
Design and draw an ER diagram that captures the information about the University.
34