Chapter8. - DatabaseDesign
Chapter8. - DatabaseDesign
Database Design
1
Objectives: Database Design
Define the purpose of Database Design and
where in the lifecycle it is performed
Explain how persistent classes map to the
data model
Learn how to distribute class behavior to
the database
2
Database Design in Context
[Early
Elaboration [Inception
Iteration] Iteration (Optional)]
Analyze Behavior
(Optional)
Refine the
Architecture
Database Database
Design Design the Designer Design
Components Database
3
Database Design Overview
Database
Use-Case Realization Design
Data Model
Project Specific
Guidelines Design Model
4
Database Design Steps
Map persistent design classes to the data
model
Distribute class behavior to the database
5
Database Design Steps
Map persistent design classes to the data
model
Distribute class behavior to the database
6
Relational Databases and Object Orientation
RDBMS and Object Orientation are not entirely
compatible
RDBMS
• Focus is on data
• Better suited for ad-hoc relationships and reporting
application
• Expose data (column values)
Object Oriented system
• Focus is on behavior
• Better suited to handle state-specific behavior where
data is secondary
• Hide data (encapsulation)
7
The Relational Data Model
Relational model is composed of
Entities
Relations
Entity
Relation
products
ORDER LINE ITEM
Order_Id LineItem_Id
lineItems
Description
Price PRODUCT
Quantity
lineItem order
Product_Id Product_Id
Order_Id Name
Price
Columns
8
The Object Model
The Object Model is composed of
Classes (attributes)
Associations
LineItem
Order +lineItems - quantity : Integer
- number : Integer
+order - number : Integer
1..*
1
Product
- number : Integer
- description : String
- unitPrice : Double
9
Mapping Persistent Classes to Tables
In a relational database
Every row is regarded as an object
A column in a table is equivalent to a persistent
attribute of a class
Student
- name : String
- address : String
- studentID : Long
10
Mapping Associations Between Persistent Objects
Associations between two persistent
objects are realized as foreign keys to the
associated objects.
A foreign key is a column in one table that
contains the primary key value of associated
object Course Offering table
Number Course_ID
CourseOffering
- number : String 678 456789
Foreign Key
0..*
Course table Primary Key
Course
- name
1 - description
Name Description Number
- number Math 101 Algebra 456789
11
Mapping Aggregation to the Data Model
Aggregation is also modeled using foreign
key relationships
The use of composition implements a
cascading delete constraint
Student table
Student.
Student_ID
- studentID : int
123456
1
Primary Key
0..*
Schedule table Foreign Key
Schedule
- semester : Semester Student_ID Semester
123456 Spring 2001
12
Modeling Inheritance in the Data Model
A Data Model does not support modeling
inheritance in a direct way
Two options:
Use separate tables (normalized data)
Duplicate all inherited associations and
attributes (de-normalized data)
13
Database Design Steps
Map persistent design classes to the data
model
Distribute class behavior to the database
14
What Are Stored Procedures?
A stored procedure is executable code that
runs under the RDBMS
Two types of stored procedures:
Procedures: Executed explicitly by an
application
Triggers: Invoked implicitly when some
database event occurs
15
Map Class Behavior to Stored Procedures
Determine if any operations can be
implemented as a stored procedure
Candidates:
Operations that deal with persistent data
Operations in which a query is involved in a
computation
Operations that need to access the database to
validate data
16
Example: Map Class Behavior to Stored Procedures
17
Checkpoints: Database Design
Have all persistent classes been
mapped to database structures?
Have stored procedures and
triggers been defined?
Does the persistence mechanism
use stored procedures and
database triggers consistently?
18
19