Database Systems Mapping from the (E)ER Model to the Relational Model CMPS 244 Conceptual Modeling Using the Entity-Relationship Model firstname lastname name hobby numofcourses id name STUDENT year credithours CID m n ENROLLED COURSE n grade TEACHES office m id PROFESSOR name Logical Modeling Using the Relational Model HOBBY STUDENT Id Firstname Student_id Lastname Hobby ENROLLMENT Student_id Course_name Course_year Grade COURSE Name Year Credit_hours TEACHES PROFESSOR Id Name Office Professor_id Course_name Course_year Mapping from the (E)ER Model to the Relational Model • Mapping of Entity Sets • Mapping of Relationship Sets • Mapping of Specializations and Generalizations • Mapping of Categorization Mapping from the (E)ER Model to the Relational Model • Mapping of Entity Sets • Mapping of Relationship Sets • Mapping of Specializations and Generalizations • Mapping of Categorization Mapping of Strong Entity Sets • For each strong entity set E, create a relation R that includes all the simple attributes of E • The primary key of R is the key of E Mapping of Non-Simple Attributes • Composite attributes • Include all the simple attributes that the composite attribute is composed of • Derived attributes • Do not include them • Multivalued attributes • Create a new relation R’ for each multivalued attribute A • Include a foreign key FK to R in R’ • Primary key of R’ is the combination of A and the PK of R Mapping Strong Entity Set Examples office PROFESSOR id PROFESSOR name year CID COURSE name Id Name Office credithours COURSE Name Year Credit_hours Mapping Non-Simple Attributes Example firstname hobby numofcourses STUDENT Id Firstname id lastname name STUDENT HOBBY Lastname Student_id Hobby Mapping of Weak Entity sets • For each weak entity set E’ with owner E whose corresponding relation is R • Create a relation R’ • Include a foreign key FK to the relation R in R’ • The primary key of R’ is the partial key of E’ and the key of E Mapping of Weak Entity Sets Example office PROFESSOR id PROFESSOR name Id Name Office 1 RELATIVE RELATED relationship m RELATIVE Professor_id name Name Relationship Mapping from the (E)ER Model to the Relational Model • Mapping of Entity Sets • Mapping of Relationship Sets • Mapping of Specializations and Generalizations • Mapping of Categorization Mapping of 1:1 Relationships • For each 1:1 relationship set P with participating entity sets Ei and Ej whose corresponding relations are Ri and Rj respectively • Include a foreign key FK to Ri in Rj and add the attributes of the relationship P to Rj, or • Include a foreign key FK to Rj in Ri and add the attributes of the relationship P to Ri, or • Create a new relation R with two foreign keys, FKi to Ri and FKj to Rj and add the attributes of the relationship P to R • Primary key of R is either FKi or FKj Mapping of 1:1 Relationships PKi A Ei 1 P PKj 1 Ri Ej … PKj … Ri PKi FKj … FKi PKj A PKj A … FKi FKj A FKi PKj … Rj R … … Rj R Ri PKi … Rj Ri PKi PKi Rj Ri PKi Rj FKj A PKj … … Mapping of 1:1 Relationship Example (Option 1) office PROFESSOR id PROFESSOR name Id Name Office Course_name Course_year 1 LEADS credithours 1 COURSE name year CID COURSE Name Year Credit_hours Mapping of a 1:1 Relationship Example (Option 2) office PROFESSOR id PROFESSOR name Id Name Office 1 COURSE LEADS credithours 1 name year CID COURSE Name Year Credit_hours Professor_id Mapping of a 1:1 Relationship Example (Option 3a) PROFESSOR office Id id PROFESSOR Name Office name LEADS 1 Professor_id Course_name Course_year LEADS COURSE credithours 1 name year CID COURSE Name Year Credit_hours Mapping of a 1:1 Relationship Example (Option 3b) PROFESSOR office Id id PROFESSOR Name Office name LEADS 1 Professor_id Course_name Course_year LEADS COURSE credithours 1 name year CID COURSE Name Year Credit_hours Which approach is better here? PROFESSOR COURSE Include a foreign key to PROFESSOR in COURSE since the foreign key in this case is smaller, i.e., Id of PROFESSOR is smaller than (name, year) of COURSE 19 Which approach is better here? PROFESSOR COURSE Include a foreign key to COURSE in PROFESSOR since majority of courses do not participate in the relationship, i.e., many null values the other way around 20 Which approach is better here? PROFESSOR COURSE Create a new relation since most of the entities from both sets do not participate in the relationship, i.e., many null values otherwise 21 Which approach is better here? office PROFESSOR id PROFESSOR name Id Name Office Course_name Course_year 1 LEADS credithours 1 COURSE name year CID COURSE Name Year Credit_hours Better approach because we know that every professor participates in the relationship so no null values Which approach is better here? office PROFESSOR id PROFESSOR name Id Name Office 1 COURSE LEADS credithours 1 name year CID COURSE Name Year Credit_hours Professor_id Better approach because Id of professor is smaller Mapping of 1:1 Relationships Summary • Try to minimize space consumed by • choosing the relation with the smaller primary key as the referenced relation • choosing the relation of the entity set with total participation as the referencing relation (no null values) • choosing the relation of the entity set whose most entities participate in the relationship as the referencing relation (fewer null values) • creating a new relation if most entities from both entity sets do not participate in the relationship Mapping of 1:N Relationships • For each 1:N relationship set P with participating entity sets Ei and Ej whose corresponding relations are Ri and Rj, respectively • If Ej is the entity set at the N-side, then include a foreign key FK to Ri in Rj and add the attributes of the relationship P to Rj, or • If Ei is the entity set at the N-side, then include a foreign key FK to Rj in Ri and add the attributes of the relationship P to Ri, or • Create a new relationship R with two foreign keys, FKi to Ri and FKj to Rj and add the attributes of the relationship P to R • Primary key of R is Fki if Fki is at the N-side or Fkj if Fkj is at the N-side Mapping of 1:N Relationships PKi A 1 Ei Ej Ei … PKj … FKi PKi A Rj … PKj R FKi A N PKj 1 P Ri Ri PKi PKi Rj Ri PKi N P PKj Rj … FKj PKj A Ri … PKi A … Rj … PKj R FKj Ej FKi FKj A … Mapping of 1:N Relationship Example (Approach 1) office PROFESSOR id PROFESSOR name Id Name Office 1 COURSE LEADS credithours m name year CID COURSE Name Year Credit_hours Professor_id Because COURSE is at the N-side, i.e., each course is related to at most one professor only Mapping of 1:N Relationship Example (Approach 2) PROFESSOR office Id id PROFESSOR Name Office name LEADS 1 Professor_id Course_name Course_year LEADS COURSE credithours m name year CID COURSE Name Year Credit_hours Which approach is better here? PROFESSOR COURSE Create a new relation since a lot of courses do not participate in the relationship, i.e., many null values otherwise 29 Mapping of N:M Relationships • For each M:N relationship set P with participating entity sets Ei and Ej whose corresponding relations are Ri and Rj, respectively • Create a new relation R with two foreign keys FKi to Ri and FKj to Rj and include all attributes of P in R • Primary key of R is FKi and FKj Mapping of M:N Relationship Example PROFESSOR office Id id PROFESSOR Name Office name LEADS n Professor_id Course_name Course_year LEADS COURSE credithours m name year CID COURSE Name Year Credit_hours Mapping of n-ary Relationships • For each n-ary relationship set P • Create a new relation S to represent P • Include primary keys of relations of participating entity sets as foreign keys in S • Include all attributes of P as attributes in S • Primary key of S is the combination of all foreign keys in general (unless one or more entity sets participating in P has a cardinality of 1) Mapping of n-ary Relationship Example firstname lastname name hobby numofcourses id name CID n m STUDENT year TEACHES COURSE grade 1 id PROFESSOR office name credithours Mapping of N-ary Relationship Example (2) HOBBY STUDENT Id Firstname Student_id Lastname Hobby TEACHES Student_id COURSE Name Year Credit_hours PROFESSOR Id Name Office Course_name Course_year Professor_id Grade Mapping from the (E)ER Model to the Relational Model • Mapping of Entity Sets • Mapping of Relationship Sets • Mapping of Specializations and Generalizations • Mapping of Categorization Mapping of Specializations and Generalizations • Given a superclass S and subclasses S1, S2, …, Sn • • • • • Map the superclass S into a relation R Map each subclass Si into a relation Ri Include a foreign key to R in each Ri Primary key of Ri is primary key of R Works for all specializations/generalizations (total or partial, overlapping or disjoint) Mapping of Specialization Example firstname hobby id lastname name STUDENT gpa thesis numofcourses UNDERGRAD POSTGRAD Mapping of Specialization Example (2) HOBBY STUDENT Id Firstname UNDERGRAD Id Gpa Student_id Lastname POSTGRAD Id Thesis Hobby Mapping of Specializations and Generalizations (2) • Given a superclass S and subclasses S1, S2, …, Sn Map each subclass Si into a relation Ri Include all attributes of S in each relation Ri Primary key of Ri is the key of S Works only for total specializations/generalizations (i.e., every entity in the superclass must belong to at least one of the subclasses) • Only recommended if • • • • • the specialization is disjoint, otherwise, we repeat the same entity with its superclass attributes multiple times, and • there are few specific attributes and relationships for the superclass Mapping of Specialization Example firstname hobby id lastname name STUDENT gpa thesis numofcourses UNDERGRAD POSTGRAD Mapping of Specialization Example (2) UNDERGRAD_HOBBY UNDERGRAD Id Firstname Lastname Gpa POSTGRAD Id Firstname Student_id Hobby POSTGRAD_HOBBY Lastname Thesis Student_id Hobby Mapping of Specializations and Generalizations (3) • Given a superclass S and subclasses S1, S2, …, Sn Map the superclass S into a relation R Include all attributes of each subclasses Si in R Primary key of R is the key of S Add a type attribute to R Works only for disjoint subclasses, otherwise type must be a multivalued attribute • Recommended only if very few specific attributes and relationships for the subclasses • • • • • Mapping of Specialization Example firstname hobby id lastname name STUDENT gpa thesis numofcourses UNDERGRAD POSTGRAD Mapping of Specialization Example (2) STUDENT Id Firstname HOBBY Lastname Gpa Thesis Type Student_id Hobby Mapping of Specializations and Generalizations (4) • Given a superclass S and subclasses S1, S2, …, Sn Map the superclass S into a relation R Include all attributes of each subclasses Si in R Primary key of R is the key of S Add n Boolean attributes ti to R indicating whether an entity belongs to subclass Si or not • Works for overlapping specialization/generalization • Recommended only if very few specific attributes and relationships for the subclasses • • • • Mapping of Generalization Example firstname id lastname name PERSON numofcourses hobby office Type=“staff ” Type=“student” STUDENT STAFF Mapping of Generalization Example (2) PERSON Id Firstname HOBBY Lastname Office Is_student Is_staff Person_id Hobby Mapping from the (E)ER Model to the Relational Model • Mapping of Entity Sets • Mapping of Relationship Sets • Mapping of Specializations and Generalizations • Mapping of Categorization Mapping of Categorization • Given a category S and superclasses S1, S2, …, Sn • Map the category S into a relation R • Include all attributes of S in R • Use key of the category as the primary key of R or create a surrogate key as the primary key of R • Create a relation Ri for each superclass Si • Add a foreign key FKi to R in each relation Ri Mapping Categorization Example account no balance OWNER id name name address job PERSON COMPANY Mapping of Specialization Example (2) OWNER Account_no PERSON Id Name Balance COMPANY Job Account_no Name Address Account_no Further Readings • Chapter 8 of the main textbook • Chapter 9 if you are using the online version