Transforming UML Class Diagrams to the Relational Model
by K. Yue
1. Transforming OO Model to the Relational Model
Model Transformation
The problem is: Source Model --> Target Model.
2. Transformation Rules
2.1 Classes
C1. A class C is mapped or transformed to a relation RC.
Rationale:
2.2 Attributes
Basic:
ATT1. Include all single-valued attributes (with simple data types) of a class C as attributes of RC, the relation for the class C.
ATT2. For each multi-valued attribute A of the class C, create a new relation RCA containing the attribute A and the primary key, RCId, of the relation RC (which implements the class C).
Secondary:
ATT3. If the multiplicity of an attribute is specified, to handle the case of 0:
ATT4. The default value of an attribute may be directly implemented in SQL DDL.
ATT5. Data type mapping should be handled effectively and consistently.
Example:
UML for toyu.student:
Relational schema (in HW assignment format):
Relation |
Student(StuId, fname, lname, major, minor, ach, advisor) |
Candidate Keys |
[1] StuId |
Foreign Keys |
[1] major references department(deptCode), [2] minor references department(deptCode), [3] advisor references faculty(facId) |
Nullable Attributes |
major, minor, advisor, ach |
Non-nullable Attributes |
stuId, fname, lname |
Notes |
Relational schema in SQL (with more implementation details):
CREATE TABLE IF NOT EXISTS Student (
stuId INT NOT NULL,
fname VARCHAR(30) NOT NULL,
lname VARCHAR(30) NOT NULL,
major CHAR(4) NULL,
minor CHAR(4) NULL,
-- ach: accumulated credit hours, including transferred credits.
ach INTEGER(3) NULL DEFAULT 0,
advisor INT NULL,
CONSTRAINT Student_stuId_pk PRIMARY KEY(stuId),
-- an artificial example of a CHECK constraint.
CONSTRAINT Student_ach_cc CHECK ((ach>=0) AND (ach < 250)),
CONSTRAINT Student_major_fk FOREIGN KEY (major)
REFERENCES Department(deptCode) ON DELETE CASCADE,
CONSTRAINT Student_minor_fk FOREIGN KEY (minor)
REFERENCES Department(deptCode) ON DELETE CASCADE,
CONSTRAINT Student_advisor_fk FOREIGN KEY (advisor)
REFERENCES Faculty(facId)
);
Example:
Consider the class Member with the following attributes:
Reasonable relation schema: three relations used
All columns in the table above are not nullable.
ATT6. A single-valued attribute of a composite data type (such as set, list, array) can be mapped in various ways.
ATT7. For a derived attribute A:
Example:
A class Rectangle has three attributes:
What may the relational schema look like?
One solution:
Rectangle(RectangleId, Length, Width)
with a view Rect define as
select RectangleId, Length, Width, Length * Width as Area
from Rectangle;
Alternatively, using virtual column: a column that is computed in real-time by an expression and not stored.
CREATE or replace TABLE rectangle (
width DOUBLE,
height DOUBLE,
area DOUBLE AS (width * height) virtual
);
2.3 Keys and Constraints
KC1. If a relation R implements a class C or an association (class) A, and C or A has candidate keys K's, set all K's as candidate keys of R.
KC2. If a relation R implements a class C or an association (class) A, and C or A has no candidate key, create a surrogate primary key SK for R.
KC3. All candidate keys can be implemented by using the 'unique' and non-null constraint in SQL.
KC4. If many CK but no primary key are specified, select a candidate key as the primary key and set it accordingly in the relation.
Example:
for the class Department in toyu:
Relational schema (in HW assignment format):
3 |
Department(deptCode, deptName, schoolCode, numStaff) |
Candidate Keys |
[1] deptCode, [2] deptName |
Foreign Keys |
[1] schoolCode references School(schoolCode) |
Nullable Attributes |
schoolCode, numStaff |
Non-nullable Attributes |
deptCode, deptName, |
Notes |
|
Relational schema in SQL (with more implementation details):
CREATE TABLE IF NOT EXISTS Department (
deptCode CHAR(4) NOT NULL,
deptName VARCHAR(30) NOT NULL,
schoolCode CHAR(3) NULL,
numStaff TINYINT NULL,
CONSTRAINT Department_deptCode_pk PRIMARY KEY (deptCode),
-- alternate keys: [1] deptName
CONSTRAINT Department_name_ck UNIQUE (deptName),
CONSTRAINT Department_schoolCode_fk FOREIGN KEY (schoolCode)
REFERENCES School(schoolCode)
);
KC5. For a stereotype:
2.4 Associations
A1. For a many to one association between C1 (the class with the one multiplicity) and Cm, add a column R1_Id into the relation Rm (which implements Cm).
Example:
For:
We have the three numbered associations implemented by the three foreign keys below.
4 |
Faculty(facId, fname, lname, deptCode, rank) |
Candidate Keys |
[1] facId |
Foreign Keys |
[1] deptCode references Department(deptCode) |
Nullable Attributes |
rank, deptCode |
Non-nullable Attributes |
facId, fname, lname |
Notes |
|
6 |
Class(classId, courseId, semester, year, facId, room) |
Candidate Keys |
[1] classId |
Foreign Keys |
[1] courseId references Course(courseId), [2] facId references Faculty(facId) |
Nullable Attributes |
room |
Non-nullable Attributes |
classId, courseId, semester, year, facId |
Notes |
|
A2. For a many to many association (including association classes) between classes CA and CB, create a new relation RAB(RA_Id, RB_Id).
Example:
For:
We have:
8 |
Enroll(stuId, classId, grade, n_alerts) |
Candidate Keys |
[1] stuId, classId |
Foreign Keys |
[1] stuId references Student(stuId), [2] classId references Class(classId), [3] grade references Grade(grade) |
Nullable Attributes |
grade, n_alerts |
Non-nullable Attributes |
stuId, classId |
A3. For a one to one association between classes CA and CB, there are several options:
A4. For any n-ary association (n>2), a new relation is needed.
Example:
Consider the ternary association between the classes Supplier, Part, and Warehouse with an association attribute quantity.
It can be replaced by a new class and three binary association.
Supply(SupplyId, SupplierId, PartId, WarehouseId, Quantity):
Example:
Toyu UML diagram:
toyu schema: toyu_schema.pdf
toyu SQL: createtoyu.sql.txt