You are on page 1of 64

Database

Design 1
Database Design
• The main objectives of database design are to produce
- logical model and
- physical model
according to a database model.
• The logical model concentrates on the required data to
be stored independently with how the data will be stored
or where it will be stored physically.
• The physical model involves translating the logical model
of the database onto physical media using hardware
resources and software systems such as database 2
management systems (DBMS).
Database Design Process
DBMS independent

Data
Requirements

Entity Relationship (E-R) Diagrams


Conceptual Database Design UML Class Diagrams

Logical Database Design Logical data model


relatated of the chosen DBMS
choose
DBMS dependent

DBMS File organization and access path,


Physical Database Design based on the options offered by the
chosen DBMS

Database Implementation 3
What is a Database?
• General definition
A database (DB) is any collection of related data.

• Restrictive definition
A DB is a logically coherent collection of data with some inherent
meaning relevant to some aspects of the real world.

• Most of us encounter several activities every day that involve


some interactions with a database, For example,
‐ if we go to the bank to deposit or withdraw funds,
‐ if we make a hotel or airline reservation,
‐ if we access a computerized library catalog to search for a
bibliographic item, 4
‐ if we purchase something online…
What is a DBMS?

• DBMS : Database Management System is a collection of


programs that enables users to create and maintain a database.
• DBMS facilitates the processes of
 Defining a database involves specifying the data types, structures,
and constraints of the data to be stored in the database.
 Constructing a database is the process of storing the data on
some storage medium that is controlled by the DBMS.
 Manipulating a database includes functions such as querying the
database to retrieve specific data, updating the database,
generating reports from data.
5
 Sharing a database allows multiple users and programs to access
the database simultaneously.
Advantages of DBMS
• Controlling Redundancy

• Restricting Unauthorized Access

• Providing Storage Structures and Search Techniques for


Efficient Query Processing

• Providing Backup and Recovery

• Providing Multiple User Interfaces

• Representing Complex Relationships among Data

• Enforcing Integrity Constraints (ensures that data conforms to


6
guidelines specified by the database administrator)
7
Types of Database/DBMS (1/5)

• Relational DBMS (RDMBS)


 More than 80% of DB market
 Only RDBMS are considered in the context of this course

• Other DBMS types

 Hierarchical DBMS

 Network DBMS
8
 Object-oriented DBMS
Relational DBMS (RDBMS)
• In a relational database, all data are organized in the form
of tables (rows, columns).
• The RDBMS model emerged in the 1970s and has become by
far the most widely used type of DBMS due to its simplicity.

 Great technology to store large volumes of structured data

 Consistency of data

9
Examples of RDBMS
• Oracle : Developed by Oracle corporation. It is a relational
DBMS (object relational for its latest versions). Oracle is
regarded to be one of the safe DBMS and is largely used by
professionals.

• PostgreSQL : is a relational DBMS and powerful object-


relational offers an open source alternative to commercial
solutions like Oracle or IBM.

• MySQL : is an open source relational DBMS, one of the most


popular on the web. It is reliable, fast and also flexible.

• Microsoft Access : is a relational DBMS developed by


Microsoft. It provides a graphical interface to quickly develop 10
small-scale applications and to make prototypes.
Other types of DBs/DBMS (1/4)
 Hierarchical DBMS : In the hierarchical database one data is
subordinate of another data which represents relationships
between parent and child.

 Limitation: only supports 1 to n relationships


 It makes data modeling difficult

• A parent can have multiple children

• A child can only have a single parent


11
Other types of DBs/DBMS (2/4)
 Network DBMS : Network database represents relationships
between many to many data of database. It is structured in a
form of graph.
• Represents one to one as many to many relationships.
 Very complex system

 Lack of structural independence

 Structural changes to the database


are very difficult.

12
Other types of DBs/DBMS (3/4)
 Object-oriented DBMS : supports the modeling and creation of
data as objects.
• There is currently no widely agreed-upon standard for what
constitutes an ODBMS and no standard query language to
ODBMS.
• They still need to be refined to achieve greater
standardization.

13
Other types of DBs/DBMS(4/4)
• Non-Relational DBMS (No-SQL DBMS): Next Generation
Databases increasingly used in big data and real-time
web applications.

• “NewSQL”: a mixing of the various data models into what


amounts to a Relational + NoSQL Store. They are designed for
Web-scale applications. They are an effort to make the data
scalable and still provide many of the traditional SQL
operations.

14
Relational Database Design Process

15
Relational Database Design Process
DBMS independent

Data
Requirements

Entity Relationship (E-R) Diagrams


Conceptual Database Design UML Class Diagrams

Mapping E-R diagrams / class diagrams


Logical Database Design Relational Data Model
Relational
DBMS dependent

Relational schemas
DBMS File organization and access path,
Physical Database Design based on the options offered by the
RDBMS

Relational Database 16
Implementation
Objective of logical design (1/2)
• Translate the conceptual model (E-R or Class diagram)
into a logical model that can be implemented on a chosen
DBMS.

 As conceptual model, we consider the Class diagram

 As logical model, we consider the relational model

Input: conceptual model (Class diagram)

Output: relational schema


17
Objective of logical design (2/2)

Mapping

Class diagram

18
Relational Data Model
Database Design Process Example

Conceptual model

Logical model

19

Physical model
Data Models
• A data model is a collection of concepts for describing data.
• Each DBMS implements a certain data model.
• A schema is a description of a particular collection of data, using a
given data model.
• The Relational model of data is the most widely used model today.
 Main concept: relation, basically a table with rows and
columns.

 Every relation has a schema, which describes the columns


(fields, attributes) and keys. 20
Relation (att1, att2, …, attn)
Examlpe of Relational Database
• University Database stores student and course information.
It is defined by the following relational schema:
STUDENT (Student_number, Name, Class, Major)
COURSE (Course_number, Course_name, Credit_hours, Department)
SECTION (Section_identifier #Course_number, Semester, Year, Instructor)

21
How about relationships?
• Relational modeling uses primary keys and foreign keys to maintain
relationships defined in conceptual diagrams (E-A / class diagrams).
• Primary keys (PK) are typically used to identify uniquely each relation.
R1 (PKR1, att1, att2, attn)
• Foreign keys (FK) are the primary key of another relation to which a class
has a relationship.
R2 (PKR2, att1, att2, attn, #FKR1)
 We say that the foreign key FKR1 references the primary key PKR1 of
R1
• Keys may be :
‐ Simple : made of one attribute (PK= att1). 22

‐ Composite : made of more than one attribute (PK= att1, att2, …).
Simple vs composite key (1/2)
• Table Employee

cardID lastname firstname dateofbirth


08245167 henry john 03/05/1960
04125489 henry adam 06/08/1974
01234567 kidman adam 04/01/1955
07584230 bailey harry 05/05/1980
06215348 morgan alex 09/09/1975

• Assume that : cardID primary key

 cardID is a simple primary key (1 attribute).


23
Simple vs composite key (2/2)
• Table Employee
lastname firstname dateofbirth
henry john 03/05/1960
henry adam 06/08/1974
kidman adam 04/01/1955
bailey harry 05/05/1980
morgan alex 09/09/1975

• Assume that : lastname primary key


firstname primary key

 lastname, firstname is a composite primary key (2 attributes).

24
• lastname, firstname can be considered as primary key only if we
are sure that we never have the same values over all tuples.
Relations : Some rules...
• Every relation has a unique name.

• Attributes in relations have unique names.

• Every attribute value is atomic.

• Every relation must have a primary key.

• Every row is unique.

• The order of the columns is irrelevant.

• The order of the rows is irrelevant.


25
Mapping UML Class diagrams
into Relational schemas

Mapping rules

26
Rule 1 : Mapping Classes
• Each class is represented by a relation

CUSTOMER : a class

CUSTOMER : a relation

PK

• Relational schema of CUSTOMER


CUSTOMER (Customer_ID, Customer_Name, Customer_Adress, Postal_Code) 27

PK
Rule 2 : Mapping One-to-Many relationships

• primary key on the one side becomes a foreign key on the many side.

CUSTOMER
Customer ID PK
Customer Name
Customer Address
1 Mapping
submits

0..*
ORDER
FK
Order ID
ORDER date

Relational schema 28
CUSTOMER (Customer_ID, Customer_Name, Customer_Address)
ORDER (Order_ID, Order_date, #Customer_ID)
Exercise
• Determine the relational schema of the Car Rental database
from the following class diagram.

29
Exercise (solution)
• Determine the relational schema of the Car Rental database
from the following class diagram.

• Customer (Cust_ID, Lastname, Firstname, Address, Postal_Code)


• Rent (Rent_ID, Rent_Date, Retutn_Date, #Cust_ID, #Reg_No)
• Car (Reg_NO, Rate, #Model_ID)
30
• CarType (Model_ID, Mark, Model, Year)
Rule 3 : Mapping Many-to-Many relationships
• create a new relation (associative relation) with the primary keys of the two
relations as foreign keys.
• its primary key is composed by the two foreign keys.

Employee
Employee ID
Employee Name
Employee Address Mapping
*
assigned

1..*
Task
Task ID Relational schema
Description
Task (Task_ID, Description) 31
Employee (Employee_ID, Employee_Name, Employee_Adress)
Assigned_Task (#Task_ID, #Employee_ID)
Exercise
• Determine the relational schema for teachers and courses
database from the following class diagram

32
Exercise (solution)
• Determine the relational schema for teachers and courses
database from the following class diagram

• Teacher (TeacherID, Firstname, Lastname, Office)


• Course (CourseID, CourseName, Credits, Description)
• Teachers_Courses ( #TeacherID, #CourseID)
33
Rule 4 : Mapping Association Class relationships
1. Association Class : One-to-Many
• The attributes of the association class are added to the relation on the
many side. The association class is not mapped.

Company Employee
0..1 Employes 1..*
Company ID Employee ID Mapping
Company Name Employee Name
Employee Address

WorksFor
Salary

Relational schema 34
Employee (Employee_ID, Employee_Name, Employee_Adress, #Company_ID, Salary)
Company (Company_ID, Company Name)
Rule 4 : Mapping Association Class relationships
2. Association Class : Many-to-Many
• We create a relation for the association class with the primary keys of the
two relations as foreign keys and its own attributes.
• its primary key is composed by the two foreign keys.

PRODUCT * Requests ORDER


* Mapping
Product ID Order ID
Unit Price ORDER date

ORDER_Line
Quantity

Relational schema
ORDER (Order_ID, Order_date) 35
PRODUCT (Product_ID, Unit_Price)
ORDER_LINE (#Order_ID, #Product_ID, Quantity)
Exercise
• Determine the relational schema for teachers and courses
database from the following class diagram

36
Exercise (solution)
• Determine the relational schema for teachers and courses
database from the following class diagram

• Teacher (TeacherID, Firstname, Lastname, Office)


• Course (CourseID, CourseName, Credits, Description)
37
• Teaches_Courses ( #TeacherID, #CourseID, Year, Semester)
Rule 5 : Mapping Aggregation relationships
• The aggregation relationships are mapped in the same way as mapping
associations.
Team
Team
Team ID
Team ID Company Name
Team Name
*
1

1..* *
Employee
Employee
Employee ID
Employee ID Employee Name
Employee Name Employee Address
Employee Address
Rule 2 Rule 3
Relational schema
Relational schema 38
Team (Team_ID, Team Name)
Team (Team_ID, Team Name)
Employee (Employee_ID, …)
Employee (Employee_ID, … , #Team_ID)
Team_Employee(#Team_ID, #Employee_ID)
Rule 6 : Mapping Composition relationships
• The composition relationships are mapped as One-to-many relationships.
• The primary key of the composite relation is then added as a foreign key to
the part relation.
• The primary key of the part relation is thus a composite key.

Company Department

*
Company ID Dept ID
Company Name Dept Name

Relational schema
Company (Company_ID, Company Name)
Department (Dept_ID, #Company_ID, Dept Name) 39
Rule 7 : Mapping One-to-One relationships (1/4)

There are two solutions to


transform an association 1:1

• With two relations: we treat


the association 1:1 as
association 1:N  add foreign
key in one of the relations.

• With one relationship: the two


are merged into one relation.

40
Rule 7 : Mapping One-to-One relationships (2/4)

1st solution : two relations


Relation1 (a,b) or Relation1 (a,b,#c)
Relation2 (c,d,#a) Relation2 (c,d)

2nd solution : one relation


Relation12 (a,b,c,d) or Relation21 (c,d,a,b)

41
Exercise
• Determine the relational schema of the following relationship

Employee Position
1..1 holds 1..1
Employee ID Position ID
Employee Name Position title
Employee Address

42
Exercise (Solution)
• Determine the relational schema of the following relationship

Employee Position
1..1 holds 1..1
Employee ID Position ID
Employee Name Position title
Employee Address

1st solution : two relations


Employee (Employee ID, Employee Name, Employee Address, #PostionID)
Position (PositionID, Position title)
or
Employee (Employee ID, Employee Name, Employee Address)
Position (PositionID, Position title, # Employee ID)

2nd solution : one relation


EmployeePosition (Employee ID, Employee Name, Employee Address, PositionID, Position title)
43
or
EmployeePosition (PositionID, Position title, Employee ID, Employee Name, Employee Address)
Rule 7 : Mapping One-to-One relationships (3/4)

1st solution : two relations (is most adapted)


we will always choose the relation on the side 0..1 to be referencing.
Relation1 (a,b,#c)
Relation2 (c,d)

2nd solution : one relation


Relation21 (c,d,a,b) 44
We can’t consider Relation12 (a,b,c,d) because attributes of Relation1
may be Null (0..1)
Exercise
• Determine the relational schema of the following relationship

Employee Department
1..1 Manages 0..1
Employee ID Dept ID
Employee Name Dept Name
Employee Address

45
Exercise (Solution)
• Determine the relational schema of the following relationship

Employee Department
1..1 Manages 0..1
Employee ID Dept ID
Employee Name Dept Name
Employee Address

1st solution : two relations


Employee (Employee ID, Employee Name, Employee Address)
Department (DeptID, Dept Name, # Employee ID)

2nd solution : one relation


Employee Department (Employee ID, Employee Name, Employee Address, DeptID, 46
Dept Name)
Rule 7 : Mapping One-to-One relationships (4/4)

1st solution : two relations


One side or the other
Relation1(a,b,#c)
Relation2(c,d)
or
Relation1(a,b)
Relation2(c,d,#a)
47
2nd
solution : one relation (not adapted)
We can’t merge in one relation because the attributes may be Null (0..1)
Exercise
• Determine the relational schema of the following relationship

Customer Car
0..1 rents 0..1
Customer ID Car ID
Customer Name Car Description
Customer Address

48
Exercise (Solution)
• Determine the relational schema of the following relationship

Customer Car
0..1 rents 0..1
Customer ID Car ID
Customer Name Car Description
Customer Address

Only One solution : two relations


Customer (Customer ID, Customer Name, Customer Address, #CarID)
Car (CarID, Car Description)
Or
Customer (Customer ID, Customer Name, Customer Address)
Car (CarID, Car Description, #Customer ID) 49
Rule 8 : Mapping Reflexive relationships
1. One-to-Many

• We add a recursive foreign key in the same relation.


 Employee (Employee ID, firstName, LastName, #EmployeeID)

• We may rename the foreign key for more clarity:


 Employee (Employee ID, firstName, LastName, #ManagerID)
50
Rule 8 : Mapping Reflexive relationships
2. Many-to-Many

• We create two relations:


‐ One for the class type
 Employee (Employee ID, firstName, LastName)
‐ One for an associative relation in which the primary key has
two attributes, both taken from the primary key of the class.
51
 EmployeeManager (#Employee ID, #ManagerID)
Exercise
• Consider the following class diagram
• Derive a relational schema from this class diagram

52
Exercise (Solution)

Relational schema

53
Relational schema : graphical representation

54
Mapping Inheritance relationship
• Consider a superclass Person having two subclasses
(child/concrete) classes Customer and Employee.
• Common attributes Person ID and name are represented in the
superclass Person.
• The subclass Customer has one specific attribute preferences
• The subclass Employee has one specific attribute salary

How to map the


inheritance
relashionship?

• There are three solutions for mapping inheritance into a relational 55


database.
1. Map Each Subclass To Its Own Relation
• With this approach a relation is created for each subclass.
• Each relation include both the attributes of the subclass and its
inherited attributes from the superclass Person.
• The superclass Person is not mapped.

Mapping

2 relations
56
• Customer(Customer ID, Name, Preferences)
• Employee(Employee ID, Name, Salary)
2. Map Each Class To Its Own Relation
• With this approach a relation is created for each class
(superclass and subclasses).
• Each relation includes both the attributes of the subclass and
the inherited key attribute from the superclass Person.

Mapping

3 relations
• Person (Person ID, Name) 57
• Customer(#Person ID, Preferences)
• Employee(#Person ID, Salary)
3. Map Hierarchy To A Single Relation
• With this approach only the superclass Person is represented
by a relation.
• The specific attributes of each subclass are represented in the
relation mapping the super class Person.
• A new attribute PersonType is added in the relation Person
indicating whether the Person is a customer, an employee, or
perhaps other type.

Mapping
C : Customer
E : Employee
O : Other
58
1 relation
• Person (Person ID, PersonType:{C,E,O}, Name, Preferences, Salary)
Mapping Inheritance relationship
• As there are three solutions for mapping inheritance into a
relational database,

How to decide
which solution
consider?

59
Elements of choice (1/3)
1st solution : Map Each Subclass To Its Own Relation
• In particular it works if the superclass is defined as abstract
or all the instances of the superclass are represented by
the subclasses.
 An abstract class is a non-instantiable class. It expresses
an abstract generalization, which does not match any
existing object in the world. its name is shown in italics

60
Elements of choice (2/3)
2nd solution : Map Each Class To Its Own Relation
• This solution is appropriate when some instances of the
superclass may exist and are not represented by any of the
subclasses. Or also if some other instances may belong to
different subclasses in the same time.

 Persons that are not Customer


nor Employee may exist.

 Persons that are customer and


Employee in the same time may also exist.
61
Elements of choice (3/3)
3rd solution : Map Hierarchy To A Single Relation
• This solution is appropriate when there is a significant
distinction in the structure of subclasses and/or when the
semantics of their identification is not strong.
• This is a good strategy for simple and/or shallow class
hierarchies where there is little or no overlap between
subclasses.

 A Person may not be Customers


and Employee in the same time.

 Persons that are not Customer 62


nor Employee may exist.
Exercise (Solution)

Relational schema
• COURSE (CourseID, Course_Name)
• SUBJECT (Subject_Code, Subject_Name,#Tcode)
• COURSE_SUBJECT (#CourseID, #Subject_Code, Subject_Type)
63
• Teacher (T_Code, Fname, Lname, Ext)
• OFFERING(OfferingID, StartDate, EndDate,#Subject_Code)
Relational schema : graphical representation

64

You might also like