You are on page 1of 30

Advanced Data Modeling

Chapter 3
Many-to-Many Relationships
Relationship Strength
• Relationships between two entities may be classified as:
– Identifying
– non-identifying
• Determined by the child entity (crow’s foot) in a relationship
Non-identifying Relationship
• Exists if the PK of the child entity (crow's foot) does not contain
a PK component from the parent entity
• Both entities are independent and can stand on their own
• Relationship is represented by a FK
• Most relationships are non-identifying
• Identified by a dashed-line
Non-identifying Relationship
• DEPARTMENT – EMPLOYEE relationship
– work_dept_id (department_id) is a foreign key in the EMPLOYEE entity
– department_id is not part of the UID of the EMPLOYEE entity
Identifying Relationship
• Occurs when an entity cannot stand on its own
• Dependent on another entity
• Exists when the primary key of the parent entity is part of the
primary key of the child entity
• The primary key identifier of the parent entity is both a PK and
FK in the child entity
Identifying Relationship
• Mandatory foreign key in the intersection (child) entity
• The relationship line is a solid line
• Rounded corners
Many-to-Many (M:M) Relationship
• A relationship between two entities where an instance in entity
1 may connect (match) with many instances in entity 2 and one
instance in entity 2 may connect (match) with many instances in
entity 1

7
Blackwater Consulting
Business is expanding and Blackwater Consulting has determined that it
is now necessary to assign employees to more than one project at a
time to balance workloads. As a result, additional analysis has been
conducted and the following was determined:
• Each employee may be assigned to many projects. Sometimes,
however, an employee may be off work and is not assigned any
projects.
• Each project may be assigned to several employees. However, when a
project is first initiated, there are no employees assigned to it.
.
Business Rules
• The following business rules have been determined:
– Each employee may be assigned to one or more projects
– Some employees may not be assigned to a project
– Each project may have several employees assigned to it
– Some projects may not have any employees assigned to it
– The dates when employees start and end working on a project are
stored
– The hours an employee works on a project are stored
Many-to-Many (M:M) Relationship
• EMPLOYEE – PROJECT relationship:
– An employee may work on many projects

– A project may be assigned to many employees

10
Many-to-Many (M:M) Relationship
• EMPLOYEE – PROJECT relationship:
– Each EMPLOYEE may work on zero, one or more PROJECTs
– Each PROJECT may be assigned to zero, one or more EMPLOYEEs

– Which entity has the foreign key?


– Where do we store the number of hours each employee works on a 11
project?
Many-to-Many (M:M) Relationship
• Cannot add the primary key containing a single value of one
table as a foreign key to the other table

• This results in a single relationship, and there are many 12


Many-to-Many (M:M) Relationship
– Each EMPLOYEE may work on zero, one and only one PROJECT

– Each PROJECT may be assigned zero, one and only one EMPLOYEE

13
Many-to-Many (M:M) Relationship
• Cannot add the primary key containing multiple values of one
table as a foreign key to the other table

• This results in one column storing multiple values 14


Many-to-Many (M:M) Relationship
• Cannot add multiple columns for the foreign key

• This limits the number of relationships


15
• Violates normalization rules discussed in chapter 5
Business Rules
– Keep track of the date when employees start and end working on a
project
– Keep track how many hours an employee has worked on a project
– Where does this data go?
Many-to-Many (M:M) Relationships
• Many-to-many relationships are common in the first version of
an ER data model
• In later stages of data modeling, M:M relationships will
disappear
• Many-to-many relationships are typically hiding another entity

17
Resolving Many-to-Many Relationships
Intersection Entity
• Create a new entity between the two tables of a many-to-many
relationship
• This new entity is called an intersection or association entity
because it is created from the intersection or association
between two entities
Resolving Many-to-Many Relationships
Intersection (Association) Entities
• The name of the intersection entity is either:
– The combined name of the two parent entities (EMPLOYEE_PROJECT)
– A new name that reflects the purpose of the entity (ASSIGNMENT)
Resolving Many-to-Many Relationships
Intersection (Association) Entity
• Intersection entity is the combined name of the two parent
entities
Resolving Many-to-Many Relationships
Intersection (Association) Entity
• The primary key of the intersection entity is the combination of
the primary keys from its parent entities, this is called a
concatenated key
Resolving Many-to-Many Relationships
Intersection (Association) Entity
• The concatenated key of the intersection entity is:
– Primary keys to uniquely identify each instance of the intersection
entity
– Identifying relationship
– Foreign keys to connect back to the parent entities
– FKs in intersection entity are mandatory
Resolving Many-to-Many Relationships
Intersection (Association) Entities
• The name of the intersection entity can be a new name that
reflects the purpose of the entity (ASSIGNMENT)
Another Example
• A large distribution company has many warehouses across the county that
provide faster shipping to customers. The company handles many
products that may be stored in any of the company’s warehouses. Of
course, there will be times when a product is out of stock and is not stored
in a particular warehouse. The company wants to know the inventory
count for each product at each warehouse. For products, store the
product id, name, and unit cost. For warehouse, store the warehouse id
and city.
• Draw the ERD for the many-to-many relationship
• Draw a second ERD resolving the many-to-many relationship
25
Distribution Center
Many-to-Many Relationship

26
Distribution Center
Many-to-Many Relationship
• Each warehouse may store all products and all products may be
stored in any warehouse
• But there is a problem here. Where is the quantity on hand
stored?
• We need an intersection entity

27
Intersection Entity
• A third entity called an intersection
entity is needed to resolve a M:M
relationship.
• The original M:M relationship
becomes two 1:M relationships
• What would be the UID of the
Intersection entity?

28
Distribution Center
Solution

29
30

You might also like