Artifacts > Analysis & Design Artifact Set > Data Model > Guidelines > Reverse-engineering Relational Databases
Guidelines:
|
Column Name | Data Type |
Customer_ID | Number |
Name | Varchar |
Street | Varchar |
City | Varchar |
State/Province | Char(2) |
Zip/Postal Code | Varchar |
Country | Varchar |
Table definition for Customer table
Starting from this point, we create a class, Customer, with the structure shown in the following figure:
Initial Customer class
In this initial Customer class, there is an attribute for each column in the Customer table. Each attribute has public visibility, since any of the columns in the originating table may be queried.
Note, the icon listed to the left of the attribute indicates that the attribute is 'public'; by default, all attributes derived from RDBMS tables should be public, since the RDBMS generally allows any column to be queried without restriction.
The class that results from the direct table-class mapping will often contain attributes that can be separated into a separate class, especially in cases where the attributes appear in a number of translated classes. These 'repeated attributes' may have resulted from denormalization of tables for performance reasons, or may have been the result of an oversimplified data model. In these cases, split the corresponding class into two or more classes to represent a normalized view of the tables.
Example
Nearly immediately after defining the Customer class above, however, we can define an Address class which contains all address information (assuming that there will be other things with addresses in our system), leaving us with the following classes:
revised Customer class, with extracted Address class
The association drawn between these two is an aggregation, since the customer's address can be thought of as being part-of the customer.
For each foreign-key relationship in the table, create an association between the associated classes, removing the attribute from the class which mapped to the foreign-key column. If the foreign-key column was represented initially as an attribute, remove it from the class.
Example
Assume the structure for the Order table listed below:
Column Name | Data Type |
Number | Number |
Customer_ID | Varchar |
Structure for the Order table
In the Order table listed above, the Customer_ID column is a foreign-key reference; this column contains the primary key value of the Customer associated with the Order. We would represent this in the Design Model as shown below:
Representation of foreign-key Relationships in the Design Model
The foreign-key is represented as an association between the classes Order and Item.
RDBMS data models represent many-to-many relationships with what has been called a join table, or an association table. These tables enable many-to-many relationships to be represented using an intermediate table which contains the primary keys of two different tables which may be joined together. The reason join tables are needed is because a foreign key reference can only contain a reference to a single foreign key value; when a single row may relate to many other rows in another table, a join table is needed to associate them.
Example
Consider the case of Products, which may be provided by any one of a number of Suppliers, and any Supplier may provide any number of Products. The Product and Supplier tables have the structure defined below:
Product Table |
|
|
Supplier Table |
|
|
Column Name | Data Type |
|
Column Name | Data Type |
|
Product_ID | Number |
|
Supplier_ID | Number |
|
Name | Varchar |
|
Name | Varchar |
|
Description | Varchar |
|
Street | Varchar |
|
Price | Number |
|
City | Varchar |
|
|
|
|
State/Province | Char(2) |
|
|
|
|
Zip/Postal Code | Varchar |
|
|
|
|
Country | Varchar |
|
Product and Supplier Table Definitions
In order to link these two tables together to find the products offered by a particular supplier, we need a Product-Supplier table, which is defined in the table below.
Product-Supplier Table | |
Column Name | Data Type |
Product_ID | Number |
Supplier_ID | Number |
Product-Supplier Table Definition
This join table contains the primary keys of products and suppliers, linking them together. An row in the table would indicate that a particular supplier offers a particular product. All rows whose Supplier_ID column matches a particular supplier ID would provide a listing of all products offered by that supplier.
In the Design Model, this intermediate table is redundant, since an object model can represent many-to-many associations directly. The Supplier and Product classes and their relationships is shown in Figure 8, along with the Address class, which is extracted from the Supplier, according to the previous discussion.
Product and Supplier Class Representation
Often, you will find tables which have some similar structure. In the Relational Data Model, there is no concept of generalization, so there is not a way to represent that two or more tables have some structure in common. Sometimes common structure results from denormalization for performance, such as was the case above with the 'implicit' Address table which we extracted into a separate class. In other cases, tables share more fundamental characteristics which we can extract into a generalized parent class with two or more sub-classes. To find generalization opportunities, look for repeated columns in several tables, where the tales are more similar than they are different.
Example
Consider the following tables, SoftwareProduct and HardwareProduct, as shown below:
SoftwareProduct Table |
|
|
HardwareProduct Table |
|
Column Name | Data Type |
|
Column Name | Data Type |
Product_ID | Number |
|
Product_ID | Number |
Name | Varchar |
|
Name | Varchar |
Description | Varchar |
|
Description | Varchar |
Price | Number |
|
Price | Number |
Version | Number |
|
Assembly | Number |
SoftwareProduct and HardwareProduct Tables
Notice that the columns highlighted in blue are identical; these two tables share most of their definition in common, and only differ slightly. We can represent this by extracting a common Product class, with SoftwareProduct and HardwareProduct as sub-classes of the Product, as shown in the following figure:
SoftwareProduct and HardwareProduct Classes, showing generalization to the Product class
Putting all of the class definitions together, figure below shows a consolidated class diagram for the Order Entry system (major classes only)
Consolidated Class diagram for the Order Entry System
Replicating behavior is more difficult, since typically relational databases are not object-oriented and do not appear to have anything analogous to operations on a class in the object model. The following steps can help re-construct the behavior of the classes identified above:
Rational Unified Process |