Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban1 Mapping EER Conceptual Model and UML Class Diagrams to the Relational Data Model.

Similar presentations


Presentation on theme: "Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban1 Mapping EER Conceptual Model and UML Class Diagrams to the Relational Data Model."— Presentation transcript:

1 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban1 Mapping EER Conceptual Model and UML Class Diagrams to the Relational Data Model Suzanne W. Dietrich and Susan D. Urban Department of Computer Science and Engineering Arizona State University Tempe, AZ ADVANCED DATABASE CONCEPTS

2 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban2 OUTLINE Relationships and Associations Class Hierarchies –Creating a table for each class –Creating a table for each subclass –Flattening the hierarchy Shared Subclasses Categories Interface Classes

3 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban3 RELATIONAL DATA MODEL Extensional and Intensional Schema When mapping to the relational data model, some components of the conceptual model will be explicitly stored in tables while other semantics will be provided by views. Extensional Schema: –relations that are explicitly stored Intensional Schema: –relations defined as views

4 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban4 RELATIONAL SCHEMA NOTATION The specification of an extensional schema summarizes a table definition by –Listing the attribute names, –Underlining the candidate keys, and –Listing the additional table constraints tableName(keyAttribute, attr1, attr2, foreignKeyAttr) foreign key (foreignKeyAttr) reference primaryKeyTable(primaryKeyAttr) An explicit primary key table constraint is included only when there is more than one candidate key shown.

5 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban5 NAVIGATION OF ASSOCIATIONS By default, binary associations can be traversed in both directions in UML. 1:N and 1:1 associations can be unidirectional in UML, though (as is the default in EER), and have an alternative mapping approach that does not require creating a separate table for the association. M:N associations are inherently bidirectional, since a separate table must be created to represent the association.

6 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban6 NAVIGATION 1:N A(keyOfA, attrOfA, attrA1, attrA2, attrA3, keyOfB, attrOfBA) foreign key (keyOfB) references B(keyOfB), constraint totalParticipationAinBA not null (keyOfB) B(keyOfB, attrOfB ) Include the key from the 1 side of the association and any relationship attribute in the relation on the N side of the association. UML EER

7 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban7 NAVIGATION 1:1 B(keyOfB, attrOfB, keyOfC) primary key (keyOfB), foreign key (keyOfC) references C(keyOfC), constraint notNullCandidateKey not null (keyOfC), constraint candidateKeyConstraint unique (keyOfC) C(keyOfC, attrOfC) Include the key on the partial side of the association and any relationship attribute in the table on the total side of the association. EER UML

8 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban8 N-ARY ASSOCIATIONS Create a table with the primary key attributes of each class involved in the association and any descriptive attributes of the association itself. finance(inventoryId, ssn, bankId,loanAmount) foreign key (inventoryId) references car(inventoryId) foreign key (ssn) references person(ssn) foreign key (bankId) references bank(bankId)

9 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban9 WEAK OR QUALIFIED ASSOCIATION Create a table for the weak class with a composite key that includes the key of the owner class. A(keyOfA, attrOfA, attrA1, attrA2, attrA3) Weak(keyofA, partialKey, attrOfWk) UMLEER

10 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban10 RELATIONAL SCHEMA OF GENERIC EXAMPLE

11 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban11 HOLLYWOOD SCHEMA: EER

12 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban12 HOLLYWOOD SCHEMA: UML

13 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban13 CLASS HIERARCHIES Create a table for each class. Create a table for subclasses only, including the attributes inherited from the superclass. Flattening the hierarchy: create a single table to represent the hierarchy with type fields to indicate class membership. There are alternative strategies for mapping superclass/subclass hierarchies to relations:

14 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban14 MAPPING TERMINOLOGY Recall that a relational schema consists of the specification of the extensional schema (explicitly stored tables), the intensional schema (view definitions) and integrity constraints. The term relation refers to either a table or a view. Throughout the mapping of classes, the name of a class refers to a relation that describes the attributes of that class. The name of a table that is created to assist with the definition of a class will use a suffix to differentiate it from the relation for the class.

15 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban15 TABLE FOR EACH CLASS Create a separate table for each class in the hierarchy. Include the primary key of the root class in the table for each subclass (inclusion dependency). An equijoin must be performed to view all (specialized and inherited) attributes of a subclass.

16 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban16 TABLE FOR EACH CLASS Example: Extensional Schema person (ssn, gender, name, isMarriedTo, phone, address) movieProfessionalEDB (ssn, company) foreign key (ssn) references person (ssn) celebrityEDB (ssn, agent, birthDate) foreign key (ssn) references person (ssn) The primary key and referential integrity constraints enforce the “isa” constraint. The EDB suffix on the movieProfessionalEDB and celebrityEDB tables shown above indicate that these tables, which are part of the Extensional DataBase, store the key and specialized attributes for the subclasses only. A view must be defined to include the inherited attributes from person.

17 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban17 TABLE FOR EACH CLASS Example: Intensional Schema A view must be provided for each subclass with its inherited attributes: create view movieProfessional as select p.ssn, p.gender, p.name, p.address, p.IsMarriedTo, p.phone, m.company from person p, movieProfessionalEDB m where p.ssn=m.ssn; A similar view must be defined for the celebrity subclass.

18 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban18 TABLE FOR EACH CLASS Disjoint Specialization Constraint // enforce constraint create assertion disjointSpecialization check (not exists (select * fromperson P where P.ssn in (select M.ssn frommovieProfessional M) and P.ssn in (select C.ssn fromcelebrity C) ) ) // find violations create view disjointViolation as select P.ssn fromperson P whereP.ssn in (select M.ssn from movieProfessional M) and P.ssn in (selectC.ssn fromcelebrity C)

19 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban19 TABLE FOR EACH CLASS Total Specialization Constraint // enforce constraint create assertion totalSpecialization check (not exists (select * frommovieProfessional M whereM.ssn not in (selectC.ssn fromcritic C union selectA.ssn fromagent A) ) ) // find violations create view totalViolation as select M.ssn frommovieProfessional M whereM.ssn not in (selectC.ssn fromcritic C) and M.ssn not in (selectA.ssn fromagent A)

20 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban20 TABLE FOR EACH CLASS Attribute-Defined Specialization // enforce constraint create assertion attrDefinedSpecialization check (not exists (select * fromproject P whereP.pid in (selectF.pid fromfilmProject F) and P.type <> ‘F’) or P.type = ‘F’ and P.pid not in (selectF.pid fromfilmProject F) ) ) // find violations create view attrDefinedViolation as select P.pid from project P where P.pid in (selectF.pid fromfilmProject F) and P.type <> ‘F’) or P.type = ‘F’ and P.pid not in (selectF.pid fromfilmProject F)

21 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban21 TABLE FOR EACH CLASS Specialization Constraints Summary

22 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban22 TABLE FOR EACH SUBCLASS Create a table for each subclass. Include inherited attributes from the superclass in the table for the subclass. Create a view to represent the superclass.

23 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban23 TABLE FOR EACH SUBCLASS Example Extensional Schema filmProject (pid, title, cost, type, location) modelingProject (pid, description, cost, type, location) Intensional Schema create view project as (select pid, cost, type, location from filmProject) union (select pid, cost, type, location from modelingProject)

24 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban24 TABLE FOR EACH SUBCLASS Constraints ISA: The isa constraint is enforced by the intensional schema since the superclass is defined as the union of its subclasses. Since each class is defined by a relation name in the schema, the specifications for the following specialization constraints do not require modification: Disjoint Specialization Total Specialization Attribute-defined Specialization

25 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban25 TABLE FOR EACH SUBCLASS Specialization Constraints Summary

26 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban26 FLATTENING THE HIERARCHY Create a single table to represent the hierarchy. Include all attributes of each superclass/subclass in the table. Include type fields to indicate the (sub)classes to which a tuple belongs. If a tuple does not belong to a subclass, then the corresponding specific attributes of that subclass will have null values. Not recommended if there are many specific attributes for the subclasses.

27 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban27 FLATTENING THE HIERARCHY Example: Single Type Indicator MovieProfessional Hierachy: Critic and Agent Subclasses Extensional Schema mpCriticAgentHierarchy (ssn, company, popularity, agentFee, subtype ) Intensional Schema (agent view not shown) create view movieProfessional as select H.ssn, H.company, P.gender, P.name, P.address, P.isMarriedTo, P.phone from person P natural join mpCriticAgentHierarchy H; create view critic as select M.ssn, M.gender, M.name, M.address, M.isMarriedTo, M.phone M. company, H. popularity from movieProfessional M natural join criticAgentHierarchy H where H.subtype = ‘c’;

28 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban28 FLATTENING THE HIERARCHY Example: Multiple Type Indicators Celebrity Hierachy: MovieStar and Model Subclasses Extensional Schema celebrityHierarchy (ssn, birthDate, agentSsn, movieStar, movieType, model, preferences) where the boolean attributes movieStar and model indicate membership of the celebrity in the corresponding subclass. Intensional Schema (model view not shown) create view movieStar as select C.ssn, C.gender, C.name, C.address, C.isMarriedTo, C.phone, C.birthdate, C.agentSsn, E.movieType from celebrity C natural join celebrityEDB E where C.movieStar=‘TRUE’

29 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban29 FLATTENING THE HIERARCHY Example: Attribute-Defined Subclasses For attribute-defined subclasses, there is no need for an additional type field since there already exists an attribute that defines the type of the subclass. Extensional Schema projectHierarchy (pid, location, cost, type, title, description) where type can be either "f" for filmProject or "m" for modelingProject Intensional Schema (modelingProject not shown) create view project as select P.pid, P.location, P.cost, P.type from projectHierarchy P; create view filmProject select P.pid, P.location, P.cost, P.type, P.title from projectHierarchy P where P.type = ‘f’;

30 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban30 FLATTENING THE HIERARCHY Constraints ISA: The isa constraint is enforced by the intensional schema. Since each class is defined (either extensionally or intensionally) in the schema, the following constraint specifications already presented can be reused: Disjoint Specialization Total Specialization Attribute-defined Specialization

31 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban31 FLATTENING THE HIERARCHY Specialization Constraints Summary

32 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban32 SHARED SUBCLASSES If the superclasses of the shared subclass all have the same key attribute, any of the previous options for superclass/subclass mapping can be used.

33 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban33 SHARED SUBCLASSES starModel Example: Table for each class Extensional Schema celebrityEDB (ssn, birthDate) movieStarEDB (ssn, movieType) modelEDB (ssn, preferences) starModelEDB (ssn) Intensional Schema (assuming views for model, movieStar & celebrity ) create view starModel as select S.ssn, C.birthdate, MS.movieType, M.preferences from (((starModelEDB S natural join model M) natural join movieStar MS) natural join celebrity C)

34 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban34 SHARED SUBCLASSES starModel Example: Flattening the hierarchy Extensional Schema celebrityHierarchy (ssn, birthDate, movieStar, movieType, model, preferences) Intensional Schema create view starModel as select C.ssn, C.birthDate, C.movieType, C.preferences fromcelebrityHierarchy C wheremovieStar = ‘TRUE’ and model = ‘TRUE’

35 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban35 CATEGORIES Define a surrogate key attribute. Define a relation for the category class that includes the surrogate key. Each superclass relation must contain the surrogate key.

36 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban36 CATEGORIES Example: EER

37 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban37 CATEGORIES Example: UML In UML, the category mapping also applies to XOR Constraints

38 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban38 CATEGORIES Example: Sponsor Mapping person (ssn, name, phone, gender, address, sponsorCode) company (cid, cname, sponsorCode) modelingProject (pid, description, sponsorCode) sponsor (sponsorCode) sponsorCode is used as a surrogate key.

39 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban39 CATEGORY CONSTRAINT // enforce constraint create assertion category check (not exists (select * fromsponsor S whereS.sponsorCode not in (selectP.sponsorCode fromperson P union selectC.sponsorCode fromcompany C) ) ) // find violations create view categoryViolation as select S.sponsorCode fromsponsor S whereS.sponsorCode not in (selectP.sponsorCode fromperson P union selectC.sponsorCode fromcompany C)

40 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban40 RELATION FOR EACH CLASS Total Category Constraint // enforce constraint create assertion totalSponsorCategory check (not exists (select * fromperson P whereP.sponsorCode not in (selectS.sponsorCode fromsponsor S) or (select * fromcompany C whereC.sponsorCode not in (selectS.sponsorCode fromsponsor S))); // find violations create view totalSponsorPersonViolation as select * fromperson P whereP.sponsorCode not in (selectS.sponsorCode fromsponsor S) ; create view totalSponsorCompanyViolation as select * fromcompany C whereC.sponsorCode not in (selectS.sponsorCode fromsponsor S) ;

41 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban41 INTERFACE CLASSES Since an interface class defines behavior inheritance, a relation is not required for an interface class. Appropriate procedures that implement the operations specified in an interface class should be implemented in the concrete subclasses of an interface class.

42 Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban42 ONLINE SHOPPING EER SCHEMA


Download ppt "Mapping Object-Oriented Conceptual Models © 2002 by Dietrich and Urban1 Mapping EER Conceptual Model and UML Class Diagrams to the Relational Data Model."

Similar presentations


Ads by Google