fen 2014-03-231 mapping from class diagram (domain model) to relational model table design

Download FEN 2014-03-231 Mapping from Class Diagram (Domain Model) to Relational Model Table Design

If you can't read please download the document

Upload: lauren-york

Post on 18-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

Mapping: Identity, State (and Class) Each class is mapped onto a table (relation): Class name is used as table name All simple-value attributes are transferred to the table (a column) A primary key (id) is added to the table For each attribute consider: Domain (type) NULLs Uniqueness For composite attributes only the components are added For list attributes (multi value) a new table holding the primary key and the value of the list attribute is created FEN

TRANSCRIPT

FEN Mapping from Class Diagram (Domain Model) to Relational Model Table Design Implementation Strategy How are objects stored in a relational database? We need to store: Identity State Class Structures: generalisation association aggregation 2FEN prgVar1 prgVar2 Mapping: Identity, State (and Class) Each class is mapped onto a table (relation): Class name is used as table name All simple-value attributes are transferred to the table (a column) A primary key (id) is added to the table For each attribute consider: Domain (type) NULLs Uniqueness For composite attributes only the components are added For list attributes (multi value) a new table holding the primary key and the value of the list attribute is created FEN The structures can cause new attributes Semantic of the class diagram must be preserved There are more than one possible mapping for a structure Mapping object-relationships: Associations and aggregations: One-to-one One-to-many Many-to-many Class structure: Inheritance: Three different solutions 4FEN Mapping: Structure Structure: many-to-many (*..* or n:m) CustomerAccount Account Customer CustomerIDCPR Name Address Jens Andersen Sndergade Oda Nielsen Algade Pia Schrder Bispensgade 27 AccountIdAccountnoBalancetype checkkonto ln checkkonto Account AccountNo Balance Type Customer CPR Name Address 0:m 1:m 5FEN New table with the two primary keys as foreign keys CustomerIdAccountID Account AccountIDAccountNoBalanceType CustomerId checkkonto ln checkkonto 5 Two alternatives 1.As many to many. 2.The key from the one-side is added as an attribute to the many-side as a foreign key Account AccountNo Balance Date Customer CPR Name Address 1 1:m 6FEN Structure: one-to-many (1..* or 1:n) Account AccountIDAccountNoBalanceType CustomerId checkkonto ln checkkonto 5 Account AccountNo Balance Date Customer CPR Name Address 1 1 7FEN Structure: one-to-one (1..1 or 1:1) Two alternatives 1.As many to many. 2.The key from one of the sides is added as an attribute to the other side as a foreign key. The key from one of the sides is added as an attribute to the other side as a foreign key. But at which side? Account AccountNo Balance Date Customer CPR Name Address 1 1 8FEN Structure: one-to-one (1..1 or 1:1) Customer CusIDCPRnameaddresse accountId Jens AndersenSndergade Oda NielsenAlgade Pia SchrderBispensgade On which side should the foreign key be included? Choose the side that minimises NULL values Account AccountNo Balance Date Customer CPR Name Address 1 1 9FEN Structure: one-to-one (1..1 or 1:1) Person Car 11 On which side should the foreign key be included? Minimise NULL values: - are most cars assigned to a specific person? - do most persons have car to their disposal? What if the company is a car rental company? 10FEN Structure: one-to-one (1..1 or 1:1) Person Car 11 On which side should the foreign key be included? 11 Three kinds multiplicities: One-to-one (1-1): One person is associated with one car One-to-many (1-n): One person may be associated with several cars, but one car is only associated with one person Many-to-many (n-m): One person may be associated with several cars, and one car may be associated with many persons Person Car 11 PersonCar 1* Person Car ** Mapping: Structure FEN 12 Three kinds multiplicities: One-to-one (1-1): Include the primary key from one of the sides on the other side (minimise NULLs). One-to-many (1-n): Include the primary key from the one- side on the many-side. Many-to-many (n-m): Create a new table with the primary keys from both sides as foreign keys. The combination of the to foreign keys becomes primary key in the new table. Person Car 11 PersonCar 1* Person Car ** Mapping: Object Structure FEN Three Approaches for Transformation 1.Create a table for the super-class and a table for each subclass (The Nice Way). 2.Create tables only for the subclasses (Pull-Down). 3.Create a table only for the super-class (Pull-Up). 13FEN Mapping: Class Structure Inheritance 14 accIDaccNodateaccType checkAcc loan checkAcc accIDintRatenextBook 10, , Account accIDamountpaymentdateForPay CheckAccount Loan Conceptually attractive. Clear and simple, traceability is good, and the design may always be used. Easy to maintain. Accessing objects requires joins. Mapping: Class Structure Inheritance (1: The Nice Way) FEN CheckAccount accDaccNodate intRate nextBook , , FEN Mapping: Class Structure Inheritance (2: Pull-Down)) No table for the super-class. Easy to retrieve all information if the type is known. Works best, if there are few attributes in the super-class. Does not work, if objects may belong to more than one subclass, or if there exists objects of the superclass Code duplication No tables for subclasses. Easy access. Works best if there are few attributes in the subclasses. Problems with null values 16FEN Mapping: Class Structure Inheritance (3: Pull-Up)) 17 Summary: Domain Model Mapping to RDB Each domain class is mapped to a table: Attributes, keys, data types, NULLs? Primary key is added (or chosen). Associations and aggregations are represented by foreign key references: 1-1: Include the primary key of the one-side as foreign key on the other side (minimise NULLs). 1-n: Include the primary key of the one-side as foreign key on the n-side. n-m: Create a new table with both sides primary key as foreign keys. Generalisation: choose one of these designs: 1.Each class (both super- and subclasses) is mapped to a table. The general and special parts of an object are connected by the same primary key. The superclass may have added a type flag (preferable, but may be expensive in joins). 2.Only subclasses are mapped to tables that include the attributes from the superclass (no overlap allowed!). 3.Only the superclass is mapped to a table that includes a type flag and all attributes from the subclasses. All attributes from irrelevant subclasses are NULL (may cause many NULLs). FEN Exercise Design a database for a library with this domain model. I.e. Define relational table schemas. Add some relevant attributes, consider if nulls are allowed. On the association Reservation there are some attributes (date, the state, etc.). FEN