Entity-Relationship Model A Company Database

advertisement
Entity-Relationship Model
Kuang-hua Chen
Department of Library and Information Science
National Taiwan University
A Company Database
• Keeps track of a company’s employees,
departments and projects
• After the requirements collection and analysis
phase
– designers stated some description of the “miniworld”
– the part of the company to be represented
Language & Information Processing System, LIS, NTU
ER-2
Description of COMPANY Database
• 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.
• A department controls a number of projects, each of
which has a unique name, a unique number, and a
single location.
• We store each employee’s name, social security
number, address, salary, sex and birthdate.
Language & Information Processing System, LIS, NTU
ER-3
Description of COMPANY Database
(Continued)
• 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.
• We want to keep track of the dependents of each
employee for insurance purposes.
• We keep each dependent’s name, sex, birthdate, and
relationship to the employee.
Language & Information Processing System, LIS, NTU
ER-4
ER schema diagram for the COMPANY database
Minit
Fname
Lname
Number
N
Name
SSN
Address
Sex
Salary
1
1
Locations
Name
NumberOfEmployees
StartDate
EMPLOYEE
Bdate
WORKS_FOR
DEPARTMENT
MANAGES 1
1
CONTROLS
Hours
M
N
N
supervisor
1
WORKS_ON
supervisee
SUPERVISION
N
1
PROJECT
Name
DEPENDENTS_OF
Location
Number
N
DEPENDENT
Name
Sex
BirthDate
Relationship
Language & Information Processing System, LIS, NTU
ER-5
Entities and Attributes
• Entity, which is a “thing” in the real world with an
independent existence.
– an object with a physical existence -- a particular person, car,
house, or employee
– an object with a conceptual existence -- a company, a job, or
a university course.
• Each entity has particular properties, called attributes
– employee entity may be described by the employee’s name,
age, address...
Language & Information Processing System, LIS, NTU
ER-6
Entities and Attributes (Continued)
• A particular entity will have a value for each of its
attributes
• The attribute values that describe each entity become a
major part of the data stored in the database.
Language & Information Processing System, LIS, NTU
ER-7
Types of Attribute
• Composite attributes can be divided into smaller
subparts, which represent more basic attributes with
independent meanings of their own.
• Attributes that are not divisible are called simple or
atomic attributes.
• Most attributes have a single value for a particular entity;
such attributes are called single-valued.
• In some cases an attribute can have a set of values of the
same entity, such attributes are called multivalued.
Language & Information Processing System, LIS, NTU
ER-8
Types of Attribute (Continued)
• In some cases two (or more) attribute values are related
-- for example, the Age attribute is hence called a
derived attribute and is said to be derivable from the
BirthDate attribute, which is called a stored attribute.
• A null value for an attribute:
– A particular entity may not have any applicable value for an
attribute.
– Null can also be used if we do not know the value of an
attribute for a particular entity.
Language & Information Processing System, LIS, NTU
ER-9
Sample Entities with Attribute Values
Name=John Smith
e1
.
Address=2311 Kirby,
Houston, Texas 77001
Name=Sunco Oil
.
c1
Headquarters=Houston
Age=55
HomePhone=713-749-2630
Language & Information Processing System, LIS, NTU
President=John Smith
ER-10
A Hierarchy of Composite
Attributes
Address
StreetAddress
Number
Street
City
State
Zip
ApartmentNumber
Language & Information Processing System, LIS, NTU
ER-11
Entity Types
• An entity type defines a set of entities with the same
attributes.
– EMPLOYEE and COMPANY
ENTITY TYPE
KEY ATTRIBUTE
ATTRIBUTE
MULTIVALUED
ATTRIBUTE
• An entity type describes the schema or intension
• The individual entities of a particular entity type are
grouped into a collection or entity set (extension)
Language & Information Processing System, LIS, NTU
ER-12
Two Entity Types and Member Entities
ENTITY TYPE NAME:
ATTRIBUTES:
EMPLOYEE
Name, Age, Salary
.
.
.
e1
(John Smith, 55, 80k)
e2
ENTITY SET:
(EXTENSION)
(Fred Brown, 40, 30k)
COMPANY
Name, Headquarters, President
c
.
.
1
(Sunco Oil, Houston, John Smith)
c
2
(Fast Computer, Kallas, Bob King)
e3
...
Language & Information Processing System, LIS, NTU
...
(Judy Clark, 25, 20k)
ER-13
Key Attributes
• An entity type has an attribute whose values are distinct
for each individual entity
• Its value can be used to identify each entity uniquely
• e.g. Name attribute for COMPANY,
SocialSecurityNumber for PERSON
• Sometimes, several attributes together form a key
• Key attribute must hold for all extensions
• Some entity types have more than one key attribute
Language & Information Processing System, LIS, NTU
ER-14
The CAR Entity Type
CAR
Registration(RegistrationNumber, State), VehicleID, Make, Model, Year, {Color}
car1
.
.
.
((ABC 123,TEXAS), TK629, Ford Mustang, convertible, 1989,{red, black})
car2
((ABC 123,NEW YORK), WP9872, Nissan Sentra, 2-door, 1992, {blue})
car3
...
((VSY 720, TEXAS), TD729, Chrysler LeBaron, 4-door, 1993, {white, blue})
Language & Information Processing System, LIS, NTU
ER-15
Value Sets
• A value set (or domain) specifies the set of values that
may be assigned to that attribute
– the value set for Age attribute of EMPLOYEE is 16
to 70
• Value sets are not displayed in ER diagram
• The value set V for an attribute A of entity type
E can be defined as a function from E to the
power set of V A : E → P ( V )
Language & Information Processing System, LIS, NTU
ER-16
Initial Conceptual Design for
COMPANY Database
• An entity type DEPARTMENT with attributes Name,
Number, Locations, Manager, and ManagerStartDate.
Locations is the only multivalued attribute.
• We can specify that each of Name and Number is a key
attribute, because each was specified to be unique.
• An entity type DEPENDENT with attributes Employee,
DependentName, Sex, BirthDate, and Relationship (for
the employee).
Language & Information Processing System, LIS, NTU
ER-17
Initial Conceptual Design for
COMPANY Database (Continued)
• An entity type PROJECT with attributes Name, Number,
Location, and ControllingDepartment. Each of Name
and Number is a key attribute.
• An entity type EMPLOYEE with attributes Name, SSN,
Sex, Address, Salary, BirthDate, Department, and
Supervisor. Both Name and Address may be composite
attributes
• We must go back to the users to see if any of them will
refer to the individual components of Name -FirstName, MiddleInitial, LastName -- or of Address.
Language & Information Processing System, LIS, NTU
ER-18
Preliminary Design of Entity Types
DEPARTMENT
Name, Number, {Locations}, Manager, ManagerStartDate
PROJECT
Name, Number, Location, ControllingDepartment
EMPLOYEE
Name(Fname, Minit, Lname), SSN, Sex, Address, Salary,
BirthDate, Department, Supervisor, {WorksOn(Project, Hours)}
DEPENDENT
Employee, DependentName,Sex, BirthDate, Relationship
Language & Information Processing System, LIS, NTU
ER-19
Relationships
Relationship Types and Relationship Instances:
• A relationship type R among n entity types E1,E2,...,En
defines a set of associations among entities from these
types.
• R is a set of relationship instances ri, where
each ri associates n entities (e1,e2,...,en), and each
entity ej in ri is a member of entity type Ej, 1≦ j
≦ n.
ER Model:
Language & Information Processing System, LIS, NTU
RELATIONSHIP
TYPE
ER-20
Instances of the WORKS_FOR
Relationship
EMPLOYEE
e1
e2
e3
e4
e5
e6
r1
ß
ß
ß
ß
ß
ß
ß
r2
r3
r4
r5
r6
...
r7
DEPARTMENT
„
d1
„
ß
d2
„
ß
d3
„
„
ß
...
e7
WORKS_FOR
„
„
...
Language & Information Processing System, LIS, NTU
ER-21
Degree of a Relationship Type
• Each of the entity types E1,E2,...En is said to
participate in the relationship type R, and
similarly each of the individual entities e1,
e2,...,en is said to participate in the relationship
instance ri=(e1, e2,..., en)
• The degree of a relationship type is the number
of participating entity types.
• A relationship type of degree two is called
binary, and one of degree three is called ternary.
Language & Information Processing System, LIS, NTU
ER-22
The Ternary Relationship SUPPLY
SUPPLIER
SUPPLY
PROJECT
s1
ß
s2
ß
„
r1
„
...
r2
„
r3
„
r4
p1ß
r5
p2ß
p3ß
r6
„
j2
j3
...
PART
j1
„
...
... ...
„
r7
Language & Information Processing System, LIS, NTU
ER-23
Role Names and Recursive
Relationships
• The role name signifies the role that a
participating entity plays in each relationship
instance
• When all entity types are distinct, role name is
not needed
• Sometimes, the same entity type participates
more than once in a relationship type is different
roles
Language & Information Processing System, LIS, NTU
ER-24
Recursive Relationship SUPERVISION
SUPERVISION
EMPLOYEE
e .
e .
e .
e .
e .
e .
e .
1
1
2
2
1
2
3
1
2
4
5
„
2
1
1
2
1
6
2
...
...
7
r1
„ r
2
„ r
3
„ r
4
„ r
5
„ r6
supervisor (1) and supervisee (2)
Language & Information Processing System, LIS, NTU
ER-25
Constraints on Relationship Types
• Cardinality ratio
– specifies the number of relationship instances that an
entity can participate in
• Participation constraint
– specifies whether the existence of an entity depends
on its being related to another entity via the
relationship type
• Structural constraints
– cardinality ratio + participation constraint
Language & Information Processing System, LIS, NTU
ER-26
Cardinality Ratio
• DEPARTMENT:EMPLOYEE 1:N
• EMPLOYEE:PROJECT M:N (WORK_ON)
• EMPLOYEE:DEPARTMENT 1:1 (MANAGE)
Language & Information Processing System, LIS, NTU
ER-27
The 1:1 Relationship Manages
EMPLOYEE
MANAGES
.
e.
e.
e.
e.
e.
e.
DEPARTMENT
e1
r
2
„1
3
„r2
4
5
6
.
.
d
.
d
d1
2
„r
3
3
7
EMPLOYEE: partial participation; DEPARTMENT: total participation
Language & Information Processing System, LIS, NTU
ER-28
The M:N relationship WORKS_ON
WORKS_ON
EMPLOYEE
r
„1
.
e.
e.
e.
PROJECT
e1
„r
2
2
r3
3
„
r
4
„
P2
4
P3
r
„5
„r
.
.
.
.
P1
„
P4
6
...
r
7
Language & Information Processing System, LIS, NTU
ER-29
Participation Constraint
• Total
– if every employee must work for a department, the
entity EMPLOYEE in WORKS_FOR is called total
– sometimes called existence dependency
• Partial
– some or “part” of the set of employee entities are
related ot a department entity via MANAGES, but
not necessarily all
Language & Information Processing System, LIS, NTU
ER-30
Attributes of Relationship
• The number of hours per week that an employee
works on a project
• Attributes of 1:1 or 1:N relationship types can be
migrated ot one of the participating entity types
– StartDate of MANAGES may be migrated to
EMPLOYEE or DEPARTMENT
• For M:N relationship, the attribute should be
determined by the combination of participating
entities
Language & Information Processing System, LIS, NTU
ER-31
Weak Entity Types
• Entity type without any key attribute is called
weak entity type
• Entities of a week entity type is identified by
specific entities from another entity type via
identifying relationship
• A weak entity type always has a total
participation constraint
Language & Information Processing System, LIS, NTU
ER-32
Structure Constraints
• Specifying structural constraints involves
associating a pair of integer numbers (min, max)
with each participation of an entity type E in a
relationship R
• 0<=min<=max, max>=1
• Each entity e in E must participation in at least
min and at most max relationship instances in R
• min=0 means partial participation
• min>0 means total participation
Language & Information Processing System, LIS, NTU
ER-33
ER Diagram for the COMPANY Schema
Fname
Minit
Lname
Name
Ssn
Address
Sex
Bdate
Number
WORKS_FOR
(1,1)
employee
Salary
StartDate
(0,1)
manager
EMPLOYEE
MANAGES
supervisee
SUPERVISION
DEPARTMENT
NumberOfEmployees
(1,1)
departmentmanaged
controllingdepartment
Hours
WORKS_ON
(0,1)
supervisor
Locations
Name
(0,N)
CONTROLS
(1,N)
worker
(0,N)
(4,N)
department
(0,N)
employee
controlled- (1,1)
project
(1,N)
project
PROJECT
Name
DEPENDENTS_OF
Location
Number
dependent
(1,1)
DEPENDENT
Name
Sex
Language & Information Processing System, LIS, NTU
BirthDate
Relationship
ER-34
Summary of ER Diagram Notation
Symbol
Meaning
Symbol
Meaning
ENTITY TYPE
COMPOSITE
ATTRIBUTE
WEAK ENTITY TYPE
DERIVED
ATTRIBUTE
RELATIONSHIP TYPE
IDENTIFYING
RELATIONSHIP TYPE
ATTRIBUTE
R
E1
1
E1
N
R
E2
TOTAL PARTICIPATION
OF E2 IN R
E2
CARDINALITY RATIO 1:N
FOR E1:E2 IN R
KEY ATTRIBUTE
(min,max)
R
MULTIVALUED
ATTRIBUTE
STRUCTURAL CONSTRAINT
(min, max) ON PARTICIPATION
OF E IN R
E
Language & Information Processing System, LIS, NTU
ER-35
Ternary Relationship Types
(a)
SName
ProjName
Quantity
PROJECT
SUPPLY
SUPPLIER
PartNo
The ternary relationship type SUPPLY
PART
(b)
SName
ProjName
M
SUPPLIER
SUPPLIES
N
PROJECT
M
M
N
PART
N
(c)
Quantity
SName
SUPPLIER
USES
PartNo
CAN_SUPPLY
1
SS
N
SUPPLY
ProjName
N
SPJ
1
PROJECT
N
PartNo
SP
1
SUPPLY represented as a weak entity type
PART
Language & Information Processing System, LIS, NTU
ER-36
TAUGHT_DURING
Semester
Year
Sem_Year
IName
INSTRUCTOR
OFFERS
CAN_TEACH
SEMESTER
OFFERED_DURING
CNumber
COURSE
Another example of ternary
versus binary relationship types.
Name
CName
CCI
CANDIDATE
Department
COMPANY
A weak entity type INTERVIEW,
with a ternary identifying relationship type.
Date
Dept/Date
INTERVIEW
Language & Information Processing System, LIS, NTU
RESULTS_IN
JOB_OFFER
ER-37
Download