ch02 er hand
TRANSCRIPT
Chapter 2Conceptual ModelingConceptual Modeling
Entity Relationship DiagramsEntity Relationship Diagrams
11
ObjectivesObjectives• Definition of terms• Importance of data modeling• Write good names and definitions for entities,
l ti hi d tt ib trelationships, and attributes• Distinguish unary, binary, and ternary relationships• Model different types of attributes entities• Model different types of attributes, entities,
relationships, and cardinalities• Draw E-R diagrams for common business situationsDraw E R diagrams for common business situations• Convert many-to-many relationships to associative
entities
22
• Model time-dependent data using time stamps
Business RulesBusiness Rules
• Statements that define or constrain someStatements that define or constrain some aspect of the business
• Assert business structure• Assert business structure
• Control/influence business behavior
• Are expressed in terms familiar to end users
• Govern how data are stored and handled.
• Our DB App will (hopefully) automate business rules
33
business rules
E/R ModelingE/R Modeling
• The E/R model is used to construct a conceptual pdata model –
a representation of thea representation of the structure and constraintsof a database and is theof a database and is the technology independent.
44
E-R ModelingE R Modeling
• An E-R model is based on:
– Entities
– Relationships between entities
– Attributes of entities and relationships
• E-R diagram
– a graphical representation of an E-R modelg p p
55
Sample E-R Diagram (Figure 2-1)
66
Basic E-R notation (Figure 2-2)
Entity symbols
Attribute symbols
A special entity that is
symbols
yalso a relationship Relationship
symbols
Relationship degreesspecify number of number of entity types involved
Relationship cardinalitiesspecify how many of
h
77
each entity type is allowed
Modeling Business Rules• Business rules
– Statements that define the business.– Expressed in natural language for end users– Expressed in data models for system developers
A G d B i R l i• A Good Business Rule is: (Table 2-1)
– Declarative – what, not how– Precise – clear, agreed-upon meaning, g p g– Atomic – one statement– Consistent – internally and externally
Expressible structured natural language– Expressible – structured, natural language– Distinct – non-redundant– Business-oriented – understood by business people
88
A Good Data Name is:
R l t d t b i t t h i l• Related to business, not technical
• Meaningful and self-documenting
• Unique
• ReadableReadable
• Composed of words from an approved list
R t bl• Repeatable
• Written in standard syntax
99
Modeling Entities and Attributes
1010
EntitiesEntities• Things in the real world, physical or not: person,
place object event conceptplace, object, event, concept
• Entity Type– collection of entities that share properties or
characteristicsEntity type is always SINGULAR– Entity type is always SINGULAR
– Represented by a rectangleStudent
• Entity Instance– each of the instances of an entity type
1111
What Should an Entity Be?What Should an Entity Be?• SHOULD BE:
– An object that we are trying to model
– An object that will have many instances in the database
• SHOULD NOT BE:– A user of the database system
– An output of the database system (e.g. a report)
1212
Figure 2-4 Example of inappropriate entities
Inappropriate System user
System outputpp p
entities
Appropriate entities
1313
Attributes
• Attribute - property or characteristic of an entity p p y ytype that is of interest to the organization.
• Classifications of attributes:Classifications of attributes:– Required versus Optional Attributes
– Simple versus Composite AttributeSimple versus Composite Attribute
– Single-Valued versus Multi-valued Attribute
Stored versus Derived Attributes– Stored versus Derived Attributes
– Identifier Attributes
1414
Figure 2-7 A composite attribute
An attribute broken into component parts
Figure 2-8 Entity with multivalued attribute (Skill) and derived attribute (Years Employed)and derived attribute (Years Employed)
MultivaluedMultivaluedan employee can have more than one skill
Derivedfrom date employed
1515
more than one skilland current date
Identifiers (Keys)( y )
• Identifier (Key) - An attribute (or combination of ib ) h i l id ifi i di id l iattributes) that uniquely identifies individual instances
of an entity type• Simple Key versus Composite Key• Simple Key versus Composite Key• Candidate Key – an attribute that could be a
key…satisfies the requirements for being a keyy q g y
1616
Criteria for Selecting Identifiersg
• Will not change in value over the life of geach instance of the entity type.
• Will not be NULL.Will not be NULL.
• No intelligent identifiers (containing e.g. locations or people that might change)locations or people that might change)
• Substitute new, simple (e.g., surrogatett ib t ) k f l it kattribute) keys for long, composite keys
(e.g., entity type of Game: Game# instead f H T d Vi it T )
1717
of Home_Team and Visitor_Team)
Figure 2-9 Simple and composite identifier attributes
The id ifi i identifier is boldfaced and underlined
1818
Practice: Person
• Produce an E-R diagram for the following situation:
We have one entity, called Person, with the following tt ib t ID (th id tifi ) N hi h i dattributes: ID (the identifier); Name, which is composed
of one or more given names and one or more family names; one or more aliases; an address (composed of ; ; ( pstreet, city, state, zip); date of birth; and age, which can be calculated from the date of birth.
1919
Relationshipsp
• Association between two or more entities• Represented by connecting lines
2020
Figure 2-10 Relationship types and instances
a) Relationship type (Completes)
b) Relationship instances
2121
Relationshipsp
• Degree• Cardinality constraints
2222
Degree of relationships – from Figure 2-2
One entity related to
th f
Entities of two Entities of
h d ffanother of the same entity type
two different types related to
three different types related to each other
2323
y yprelated to each other
Fig. 2-12: (b) Binary relationships
2424
Interpreting Cardinalities
2525
Interpreting Cardinalities
2626
Cardinality ConstraintsCardinality Constraints
• Cardinality Constraints - the ynumber of instances of one entity that can or must be associated with each instance of
th titanother entity.
• Minimum CardinalityIf th ti l– If zero, then optional
– If one or more, then mandatory
• Maximum CardinalityMaximum Cardinality– The maximum number
2727
Quick CheckQuick Check
• So now you try it Add cardinality constraints to the• So now you try it. Add cardinality constraints to the following diagram– A person is national of zero or more countriesA person is national of zero or more countries
– A country has one or more people.
2828
Fig. 2-16: Introducing cardinality constraint(a) Basic relationship(a) Basic relationship
(a) Relationship with cardinality constraints
2929
Figure 2-17 Examples of cardinality constraints
a) Mandatory cardinalities
A patient must have recorded at least one history and can
A patient history is recorded for one and
3030
at least one history, and can have many
recorded for one and only one patient
Figure 2-17 Examples of cardinality constraints (cont.)
b) One optional, one mandatory
3131
Maximum Cardinalities
• Looking at the MAXIMUM cardinality on BOTH sides, we classify relationships as: , y p
1:1
1:M
M:N
3232
Practice: Products
• We have two kinds of entities: Products and Categories. For each product we keep its id g p p(identifier), name, price, wholesale price, and profit margin, which is calculated from the price and the
h l l i F h t k it idwholesale price. For each category we keep its id (identifier) and its name. Each product belongs to zero or more categories and each category can have zero or g g ymore products.
3333
Practice : CD
• Let us model a CD with three entities: CD, PERSON and SONGand SONG.– A CD has a number, which is its identifier, and a title– A person has an ID and a name, divided into first, last– A song has an ID, a title and a length– We keep track of which person is a song’s author. A person
can author many songs and a song has exactly one authorcan author many songs and a song has exactly one author.– We keep track of which people perform on a CD. Zero or
more people can perform on a CD, and people can perform on CDzero or more CDs.
– We keep track of which songs are included on a CD. One or more songs are included on a CD, and a song is included in
3434
zero or more CDs.
Strong vs.Weak Entitiesg
• Strong entity– Exists independently of other types of entities– Has its own unique identifier– Identifier underlined with single-line– Identifier underlined with single-line
• Weak entity– Dependent on a strong entity …cannot exist on its ownp g y– Does not have a unique identifier (only a partial identifier)– Partial identifier underlined with double-line
E i b h d bl li– Entity box has double line
• Identifying relationshiplinks strong entities to weak entities
3535
– links strong entities to weak entities
Figure 2-5 Example of a weak identity and its identifying relationshiprelationship
3636
Strong entity Weak entity
Weak Entity Strong Entity
N Strong entity
Now a strong entity(unique ID)
3737
Practice: Course Sections
• Model the courses using two entities: COURSE and SECTION Each course has a number and a title and maySECTION. Each course has a number and a title, and may have zero or more sections. Each section has a number and the term offered, which is composed of semester and year.
3838
Practice: Weak Entities
• Give another example of a weak entity type.
3939
Relationships with AttributesRelationships with Attributes
4040
Figure 2-11a A binary relationship with an attributeAttribute on a relationship (Link Attribute/Associative)
Here, the date completed attribute pertains specifically to the employee’s completion of a course it is an
4141
to the employee s completion of a course…it is an attribute of the relationship
Practice: Relationships with Attributes• #8, page 103. The figure below shows a grade report that is
mailed to students at the end of each semester. Prepare an ERD reflecting the data contained in the gradePrepare an ERD reflecting the data contained in the grade report. Assume that each course is taught by one instructor.
4242
Practice: Relationships with Attributes
4343
Homework Assignmentg• Exercise #15(a,g), page #104• Exercise #17 page #106• Exercise #17, page #106• Exercise #25, page #107
4444
Associative EntitiesAssociative Entities
4545
Associative Entities• One of the hardest concepts in E-R modeling• An associative entity is a relationship transformed• An associative entity is a relationship transformed
into an entity• Each instance of an associative entity represents an y p
instance of the relationship• Needed to represent ternary relationships, and for
h d t t l ti hi i tcases when we need to convert a relationship into an entity, to relate it to other entities.
4646
Associative Entities - Example• Previous model: a database of courses taken by employees.
For each employee we keep its SSN (identifier), name and birth date and for each course we keep its id and title Webirth date, and for each course we keep its id and title. We also keep the date the employee completed that course.
oo
S h l d i f i b h
4747
• Suppose that we also want to record information about the institutions issuing the certificates. HOW???
Figure 2-11b An associative entity (CERTIFICATE)
oo
Associative entity is like a relationship with an attribute, but it is also considered to be an entity in its own righty g
Note that the many-to-many cardinality between entities in Figure 2 11a has been replaced by two one to many
4848
Figure 2-11a has been replaced by two one-to-many relationships with the associative entity
oo
IssuesFill in the missing di li i
INSTITUTION
cardinalities.
Institution IDInstitution Address (City,
4949
( yState, Zip)
Fig. 2-11: (b) An associative entity (CERTIFICATE)
oo
Employee ID
oo
p y _Course_ID
What is an alternative to assign the PK?
5050
Associative Entities• An entity - has attributes• A relationship - links entities together• A relationship - links entities together• When should a relationship with attributes instead be
an associative entity? y– All relationships for the associative entity should be many– The associative entity could have meaning independent of
the other entitiesthe 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
5151
Homework Assignmentg• Exercise #15(f), page #104
5252
Ternary RelationshipsTernary Relationships
5353
Ternary RelationshipsTernary Relationships
• Relationships of degree 3• Relationships of degree 3
• Associates three entities at the same time
• Can't we just live with binary relationships?
5454
Ternary Relationship - ExampleTernary Relationship Example
• Say we wanted to keep track of which Person got which Degrees from which University.
tPERSON UNIVERSITY
got
DEGREEDEGREE
5555
• Can this be modeled with binary relationships?
Ternary Relationship - ExampleTernary Relationship Example
• Yes, with the help of an associative entity, p y
PERSON UNIVERSITYDIPLOMAPERSON UNIVERSITYDIPLOMA
DEGREE
• Ternary relationship “got” is now an associative
5656
entity, DIPLOMA. Cardinality constraints?
Figure 2-12 c) Example of a ternary relationship
5757
Note: a relationship can have attributes of its own
Fig. 2-14: Ternary relationships as an associative entity
5858
As a general rule, ternary relationships should be converted to associative entities.
Fig. 2-18: Cardinality constraints in a ternary relationship
Wh t th b i l ?
5959
What are the business rules?
Practice: Ternary RelationshipsPractice: Ternary Relationships
• Give another example of a ternary relationship• Give another example of a ternary relationship.
6060
Homework AssignmentHomework Assignment
• Exercise #15(b) page #104• Exercise #15(b), page #104
• Exercise #18, page 106
6161
Unary RelationshipsUnary Relationships
6262
Unary RelationshipsUnary Relationships
• Relationships of degree 1• Relationships of degree 1
• Also known as recursive relationships
• Two or more entities in the relationship are of the same type
• Example: we want to represent when an employee supervises another employee.p y p p y
6363
Example: SupervisesExample: Supervises
• Example: we want to represent when an• Example: we want to represent when an employee supervises another employee.
W ld i h hi lik hi• We could start with something like this:
SUPERVISORSupervises
SUPERVISEE
6464
Example: SupervisesExample: Supervises
• But supervisors can have their own supervisors• But supervisors can have their own supervisors
• Both supervisor and supervisee are employees, so we need a recursive relationship with roleswe need a recursive relationship, with roles
Supervises
EMPLOYEE
Supervisee
EMPLOYEESupervisor
6565
Example: SupervisesExample: Supervises
• And of course add cardinalities• And, of course, add cardinalities
• Should always define roles – even more important when cardinalities are differentwhen cardinalities are different
Supervises
Supervisee V
EMPLOYEESupervisor
EMPLOYEE
6666
What are the business rules?
Fig. 2-12 (a) Unary relationships
Practice: Add missing cardinalities.
6767
g
Figure 2-17 Examples of cardinality constraints (cont.)
c) Optional cardinalities
A person is pmarried to at most one other person, or may not be married at all
6868
Example: Bill Of MaterialsExample: Bill Of Materials
Has ComponentsWhole PartHas Components
• Part-Whole is also a recursive relationship:
Has ComponentsPart V
Q iITEM
Whole>
Quantity
6969
Fig. 2-13: (b) Two ITEM bill-of-materials instancesFig. 2 13: (b) Two ITEM bill of materials instances
7070
Fig. 2-13 (c ) : An associative entity - bill of materials structure
This could just be a relationship with
7171
This could just be a relationship with attributes…it’s a judgment call
Practice: Unary RelationshipsPractice: Unary Relationships
• Give another example of a unary relationship.p y p
7272
Homework AssignmentHomework Assignment
• Exercise #10, page #103., p g
7373
Attributes or Entity?Attributes or Entity?
• Sometimes you will wonder whether to represent y pdata as an attribute or an entity. This is a common dilemma.
• Let us look at a few situations.
7474
Figure 2-15a and 2-15b Multivalued attributes can be represented as relationships/Associative Entity
simple
composite
7575
Attributes or Entity?Attributes or Entity?
• So when SHOULD an attribute be linked to an entity type via a relationship?– Attribute refers to a concept in the data model
– Multiple entity instances share the same attribute
• Example on next page
7676
Fig. 2-15: Using relationships and entities to link related attribute(c) Composite attribute of data shared with other entity types( ) p y yp
7777
M lti l R l ti hiMultiple Relationships
M th l ti hi b t• More than one relationship between the same entity types (Fig. 2-21)
7878
Fig. 2-21: Examples of multiple relationships
( ) E l d d t t(a) Employees and departments
7979
Entities can be related to one another in more than one way.
Fig. 2-21: (b) Professors and courses (fixed upon constraint)
A N B i R lA New Business Rule:An instructor who isscheduled to teach a course must be qualifiedcourse must be qualifiedto teach that course??
????
Here, minimum cardinality constraint is 2, what’s for?
8080
Homework AssignmentHomework Assignment
• Exercise #11, page #103, p g
• Exercise #15(d,i), page #105
• Exercise #18, page #106Exercise #18, page #106
• Exercise #20, page #106
• Exercise #23 page #107Exercise #23, page #107
• Exercise #26, page #108
8181
Modeling Time-Dependent DataModeling Time Dependent Data
Figure 2-19. Simple example of time-stampingg p p p g
This attribute is both
8282
This attribute is both multivalued and composite.
Fig. 2-20: (a) E-R diagram not recognizing product reassignment
Fig. 2-20: (b) E-R diagram recognizing product reassignment
8383
Fig. 2-20: (a) E-R diagram not recognizing product reassignment
H t fl t d t li
In the middle of year, due to a reorganization of the sales function some products are reassigned to different product lines
PRODUCTLINE
How to reflect product line changed over time?
Sales Product Product-Line
$50,000 P B$ ,
$40,000 P A(out of $50,000)
Assigned
8484
PRODUCT ORDERPlaced
Fig. 2-20: Pine Valley Furniture product databaseSolution: adding a new relationship of “Sales_for_product_line”
(b) E-R diagram recognizing product reassignment
8585
Fig. 2-20: Pine Valley Furniture product database(c) E-R diagram with associative entity for product assignment to product line ( ) g y p g p
over time
8686
Th E i R l i hi (E R) M d lThe Entity Relationship (E-R) Model
Congratulation !!
You have just learnedYou have just learned one of the most important modelingimportant modeling concept (E-R) for developing the datadeveloping the data base systems.
8787
Recognizing Different ERDRecognizing Different ERD Notations
8888
Fig. 2-22: Microsoft Visio
Notation for Pine Valley
Furniture E-R diagram
Different modelingDifferent modeling software tools may have different notation f thfor the same constructs
8989
Fig. 2-22: E-R diagram for Pine Valley Furniture Company
9090