IMS 4212: Data Modeling—More Relationships 1 Dr. Lawrence West, Management Dept., University of Central Florida Data Modeling—Topics Foreign Keys Parent-Child Relationships Strong-Weak Entities Many to Many relationships Unary Relationships n-ary relationships Binary 1:1 relationships
IMS 4212: Data Modeling—More Relationships 2 Dr. Lawrence West, Management Dept., University of Central Florida Foreign Keys Relationships are established when the Primary Key attribute(s) of one entity is/are found in another entity These attributes are called Foreign Keys in the other entity Foreign Keys
IMS 4212: Data Modeling—More Relationships 3 Dr. Lawrence West, Management Dept., University of Central Florida Parent & Child Relationships (Terminology) The entity contributing the primary key is the parent The entity receiving the foreign key is the child Foreign Keys always, always, always go on the ‘many’ side of a 1:M relationship Identifier Attribute Shared Identifier Attribute (Foreign Key) ParentChild
IMS 4212: Data Modeling—More Relationships 4 Dr. Lawrence West, Management Dept., University of Central Florida Strong & Weak Entity Types Traditional definitions: –Strong entity type exists independently of any other entity –Weak entity type depends on some other entity type Strong Entity Type Weak Entity Type
IMS 4212: Data Modeling—More Relationships 5 Dr. Lawrence West, Management Dept., University of Central Florida Strong & Weak Entity Types (cont.) Identifying weak entities will often not happen until identifier attributes are specified When the Identifier Attribute of one entity appears as an attribute in another entity a relationship is established If the foreign key in the child table is not part of the child's PK the child is a strong entity Identifier Attribute Shared Identifier Attribute
IMS 4212: Data Modeling—More Relationships 6 Dr. Lawrence West, Management Dept., University of Central Florida Strong & Weak Entity Types (cont.) When the identifying attribute of one entity appears as part of a composite identifying attribute set in another entity the child entity is a weak entity Dept Code part of Course PK Indicates Weak Entity in some documentation styles (We will not use.) Parent (Strong) Child (Weak)
IMS 4212: Data Modeling—More Relationships 7 Dr. Lawrence West, Management Dept., University of Central Florida Strong vs. Weak Entity Types (Terminology) The identifying attribute that appears in the composite identifying attribute in a weak entity is sometimes called a cascading primary key. These primary key attributes can sometimes cascade through three or more entities Do not confuse mandatory relationships with weak entities
IMS 4212: Data Modeling—More Relationships 8 Dr. Lawrence West, Management Dept., University of Central Florida Many-to-Many Relationships What is the problem with implementing foreign keys for the following Many-to-Many relationships?
IMS 4212: Data Modeling—More Relationships 9 Dr. Lawrence West, Management Dept., University of Central Florida Many-to-Many Relationships (cont.) Many-to-Many (M:N) relationships must be decomposed into a new entity and two relationships Carefully examine the cardinality of the two new relationships If one is still M:N then decompose again
IMS 4212: Data Modeling—More Relationships 10 Dr. Lawrence West, Management Dept., University of Central Florida Many-to-Many Relationships (cont.) Always try the combination of the primary keys from the two original entities as the PK of the new entity –Sometimes not all attributes of a composite parent PK are needed –Sometimes an alternate PK suggests itself –Sometimes this proposed PK is not unique Add appropriate nonkey attributes to the new entity –Sometimes there won't be any—the new entity serves no role but to decompose the M:N relationship
IMS 4212: Data Modeling—More Relationships 11 Dr. Lawrence West, Management Dept., University of Central Florida Decomposing Many to Many Relationships Create a new entity Bring in PKs of original entities to the new entity Ask if the combination of these FKs can be the PK of the new entity –Defines the entity –Is enough to guarantee uniqueness –Is more than enough to guarantee uniqueness What is the new entity? Does a different PK suggest itself
IMS 4212: Data Modeling—More Relationships 12 Dr. Lawrence West, Management Dept., University of Central Florida Decomposing Many to Many Relationships (cont.) Determine the cardinality of the relationships from the original entities to the new entity If one (or both) are still many-to-many decompose again
IMS 4212: Data Modeling—More Relationships 13 Dr. Lawrence West, Management Dept., University of Central Florida Many-to-Many Relationships (cont.) Two kinds of entities created this way –A real ‘person, place, thing, event…’ that was overlooked in the original design –An ‘associative entity’ that has no purpose except to decompose the M:M relationship The distinction isn’t terribly important Both kinds can have non-key attributes
IMS 4212: Data Modeling—More Relationships 14 Dr. Lawrence West, Management Dept., University of Central Florida Many-to-Many Relationships (cont.) Decompose this relationship Sometimes the new entity has a natural meaning that should have been identified in the original data modeling step
IMS 4212: Data Modeling—More Relationships 15 Dr. Lawrence West, Management Dept., University of Central Florida Unary Relationships Unary relationships are relationships between an entity and itself –One employee supervises many other employees; each employee is supervised by, at most, one other employee –One part is a component of many other parts; One part (assembly) contains many other parts
IMS 4212: Data Modeling—More Relationships 16 Dr. Lawrence West, Management Dept., University of Central Florida EmployeeIDLastNameFirstName…ReportsTo 3JonesSally…9 5JeffersonMark…3 6WilsonJohn…3 8AdamskiJustin…9 9BossBig…9 13DowdRuss…6 19BrownLarry…9 Unary Relationships and Foreign Keys The foreign key in a unary relationship will be a different attribute in the entity
IMS 4212: Data Modeling—More Relationships 17 Dr. Lawrence West, Management Dept., University of Central Florida Decomposing Unary M:N relationships Unary Many-to-Many Relationships
IMS 4212: Data Modeling—More Relationships 18 Dr. Lawrence West, Management Dept., University of Central Florida Unary M:M Relationships (cont.) Fix this one
IMS 4212: Data Modeling—More Relationships 19 Dr. Lawrence West, Management Dept., University of Central Florida Ternary Relationships Ternary (or n-ary) relationships are relationships between three (or more) entities It will almost always be possible to identify a natural associative entity that reflects the relationship between the entities Create the weak associative entity and bring in foreign keys from the original entities
IMS 4212: Data Modeling—More Relationships 20 Dr. Lawrence West, Management Dept., University of Central Florida Ternary Relationships (cont.) In n-ary relationships there is a higher likelihood that the new entity will have its own 'natural' PK Examine the default PK carefully to see if it is appropriate Can you think of an alternate PK for the HouseSale entity? (What other entities are likely to be related to the HouseSale entity?)
IMS 4212: Data Modeling—More Relationships 21 Dr. Lawrence West, Management Dept., University of Central Florida Binary 1:1 Relationships In a 1:M relationship the parent entity will always be on the '1' side of the relaionship –Foreign key will be in the 'M' side Q: Where should the FK be in a 1:1 relationship?
IMS 4212: Data Modeling—More Relationships 22 Dr. Lawrence West, Management Dept., University of Central Florida Binary 1:1 Relationships (cont.) A: It doesn't matter (much) Some considerations –Use the simplest PK/FK available –If there is a 'natural' parent (e.g., employee 'owns' the office, not the other way around) make it the parent –If an occurrence of one member of the relationship may stand alone (not participate in a relationship) while the other will usually be in a relationship make it the parent