Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

CSC371 Database Systems - 1

Course Instructor: Mr. Amjad Usman


Email: amjadusman@ciitwah.edu.pk

Lecture 29
Logical Database Design
(Relational Model)
Objectives of logical design...

Translate the conceptual design into a logical database design that


can be implemented on a chosen DBMS
Input:
conceptual model (ERD)
Output:
relational schema,
normalized relations

COMSATS UNiversity, Wah Campus 10-Dec-19 2


Relational database components

Data structure
Data organized into tables with rows and columns
Data manipulation
Add, delete, modify, and retrieve using SQL
Data integrity
Maintained using business rules

COMSATS UNiversity, Wah Campus 10-Dec-19 3


Relation
 A relation is a named, two-dimensional table of data
 Table consists of rows (records) and columns (attribute or field)
Requirements for a table to qualify as a relation:
Every table has a unique name.
Attributes in tables have unique names.
Every attribute value is atomic.
 Multi-valued and composite attributes?

Every row is unique.


The order of the columns is irrelevant.
The order of the rows is irrelevant.

COMSATS UNiversity, Wah Campus 10-Dec-19 4


Key Fields
 Keys are special fields that serve two main purposes:
 Primary keys are unique identifiers of the relation in question. Examples include employee numbers,
social security numbers, etc. This is how we can guarantee that all rows are unique
 Foreign keys are identifiers that enable a dependent relation (on the many side of a relationship) to
refer to its parent relation (on the one side of the relationship)
 Keys can be simple (a single field) or
composite (more than one field)
Weak entities
Associative entities

COMSATS UNiversity, Wah Campus 10-Dec-19 5


Schema for four relations (Pine Valley Furniture Company)

Primary Key
Foreign Key
(implements 1:N relationship
between customer and order)

Combined, these are a composite


primary key (uniquely identifies the
order line)…individually they are
foreign keys (implement M:N
relationship between order and product)

COMSATS UNiversity, Wah Campus 10-Dec-19 6


Integrity Constraints

Domain Constraints
Allowable values for an attribute.
Entity Integrity
No primary key attribute may be null. All primary key fields MUST have data
Referential Integrity
Either each foreign key value must match a primary key value in another relation or the
foreign key value must be null.

COMSATS UNiversity, Wah Campus 10-Dec-19 7


Domain definitions

Domain definitions enforce domain integrity constraints

COMSATS UNiversity, Wah Campus 10-Dec-19 8


Integrity Constraints
 Referential Integrity–rule states that any foreign key value (on the relation of the many
side) MUST match a primary key value in the relation of the one side. (Or the foreign key
can be null)
For example: Delete Rules
Restrict–don’t allow delete of “parent” side if related rows exist in “dependent” side
Cascade–automatically delete “dependent” side rows that correspond with the “parent” side row to
be deleted
Set-to-Null–set the foreign key in the dependent side to null if deleting from the parent side → not
allowed for weak entities

COMSATS UNiversity, Wah Campus 10-Dec-19 9


Referential integrity constraints (Pine Valley Furniture)

Referential
integrity
constraints are
drawn via arrows
from dependent to
parent table

COMSATS UNiversity, Wah Campus 10-Dec-19 10


Transformation Steps

Step 1: Map Regular Entities


Step 2: Map Weak Entities
Step 3: Map Binary Relationships
Step 4: Map Associative Entities
Step 5: Map Unary Relationships
Step 6: Map Ternary (n-ary) Relationships
Step 7: Map Supertype/Subtype Relationships

COMSATS UNiversity, Wah Campus 10-Dec-19 11


Mapping Summary

COMSATS UNiversity, Wah Campus 10-Dec-19 12


Step1: Mapping Regular Entities
Simple attributes: E-R attributes map directly onto the relation
Composite attributes: Use only their simple, component attributes
Multi-valued Attribute – Becomes a separate relation with a foreign key taken from
the superior entity

COMSATS UNiversity, Wah Campus 10-Dec-19 13


Mapping a Simple Attribute

(a) CUSTOMER entity type with simple attributes

(b) CUSTOMER relation

COMSATS UNiversity, Wah Campus 10-Dec-19 14


Mapping a Composite Attribute

(a) CUSTOMER entity type with composite attribute

(b) CUSTOMER relation with address detail

COMSATS UNiversity, Wah Campus 10-Dec-19 15


Mapping Multivalued Attribute
(a)
Multivalued attribute becomes a separate
relation with foreign key

(b)

One–to–many relationship between original entity and new relation

COMSATS UNiversity, Wah Campus 10-Dec-19 16


Step2: Mapping Weak Entities

Becomes a separate relation with a foreign key taken from


the identifying owner
Primary key composed of:
Partial identifier of weak entity
Primary key of identifying relation (strong entity)

COMSATS UNiversity, Wah Campus 10-Dec-19 17


Example of mapping a weak entity

a) Weak entity DEPENDENT

COMSATS UNiversity, Wah Campus 10-Dec-19 18


Example of mapping a weak entity

b) Relations resulting from weak entity


NOTE: the domain constraint
for the foreign key should
NOT allow null value if
DEPENDENT is a weak entity

Foreign key

Composite primary key

COMSATS UNiversity, Wah Campus 10-Dec-19 19


Step3: Mapping Binary Relationships

One-to-Many
Primary key on the one side becomes a foreign key on the many side
Many-to-Many
Create a new relation with the primary keys of the two entities as its primary key
One-to-One
Primary key on the mandatory side becomes a foreign key on the optional side

COMSATS UNiversity, Wah Campus 10-Dec-19 20


Example of mapping a 1:M relationship
a) Relationship between customers and orders

Note the mandatory one

b) Mapping the relationship

Again, no null value in the


foreign key…this is because
of the mandatory minimum
cardinality
Foreign key
COMSATS UNiversity, Wah Campus 10-Dec-19 21
Example of data for 1:M relationship

Customer Customer ID Customer_Name Customer_Address


101 Ali Blue Area
102 Omer Jinnah Super
103 Usman Aabpara

Order ID Order Date Customer ID


Order 1001 20-Jun-2009 101
1002 21-Jun-2009 102
1003 21-Jun-2009 101
1004 22-Jun-2009 103

COMSATS UNiversity, Wah Campus 10-Dec-19 22


Example of mapping an M:N relationship

The Completes relationship will need to become a separate relation

COMSATS UNiversity, Wah Campus 10-Dec-19 23


Example of mapping an M:N relationship

Composite primary key

Foreign key New


Foreign key
intersection
relation

COMSATS UNiversity, Wah Campus 10-Dec-19 24


Example of mapping a binary 1:1 relationship

Often in 1:1 relationships, one direction is optional.

Foreign key goes in the


relation on the optional
side,
Matching the primary key
on the mandatory side.
COMSATS UNiversity, Wah Campus 10-Dec-19 25
Step4: Mapping Associative Entities

Identifier Not Assigned


Default primary key for the association relation is composed of the primary keys of the
two entities (as in M:N relationship)
Identifier Assigned
It is natural and familiar to end-users
Default identifier may not be unique

COMSATS UNiversity, Wah Campus 10-Dec-19 26


Example of mapping an associative entity

Primary key differs from foreign keys

COMSATS UNiversity, Wah Campus 10-Dec-19 27


Associative Entity without Identifier

Composite primary key formed from the two foreign keys

COMSATS UNiversity, Wah Campus 10-Dec-19 28


Step5: Mapping Unary Relationships

One-to-Many – Recursive foreign key in the same relation


Many-to-Many – Two relations:
One for the entity type
One for an associative relation in which the primary key has two attributes, both taken
from the primary key of the entity

COMSATS UNiversity, Wah Campus 10-Dec-19 29


Mapping a Unary 1:N Relationship

(a) EMPLOYEE entity


with unary relationship

(b) EMPLOYEE
relation with
recursive foreign key

COMSATS UNiversity, Wah Campus 10-Dec-19 30


Example Data for Unary 1:N Relationship

Employee and Manager


Employee ID Employee_Name Date_of_Birth Manager ID
101 Ali 01-Jan-1977 NULL
102 Omer 01-Jan-1978 101
103 Usman 01-Jan-1979 101
104 Fatima 01-Jan-1980 101
105 Hasan 01-Jan-1981 104
106 Abdullah 01-Jan-1982 102

COMSATS UNiversity, Wah Campus 10-Dec-19 31


Mapping a Unary M:N Relationship
(a) Bill-of-materials
relationships (M:N)

(b) ITEM and


COMPONENT
relations

COMSATS UNiversity, Wah Campus 10-Dec-19 32


Bill-of-Material Sample Data
Item No Description Unit_Cost
Item
101 Glass Dining Table 20,000
102 Glass 12mm 3x5 5,000
103 Wooden Pillar 1x1x2 4,000
104 Office Table 15,000

Item No Component No Quantity


101 102 1
Components
101 103 2
104 102 1

COMSATS UNiversity, Wah Campus 10-Dec-19 33


Step6: Mapping Ternary Relationships
One relation for
each entity and one
for the associative
entity

Associative entity
has foreign keys to
each entity in the
relationship

COMSATS UNiversity, Wah Campus 10-Dec-19 34


Ternary Relationship Mapping Example

Remember This is why But this makes a It would be


that the treatment date very better to create
primary key and time are cumbersome a surrogate key
MUST be included in the key… like Treatment#
unique composite
primary key

COMSATS UNiversity, Wah Campus 10-Dec-19 35


Thank You So Much

COMSATS UNiversity, Wah Campus 10-Dec-19 36

You might also like