0% found this document useful (0 votes)
10 views43 pages

IT1090 - Lecture 06

Uploaded by

jscricinfo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views43 pages

IT1090 - Lecture 06

Uploaded by

jscricinfo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

IT1090 - Information Systems and Data Modeling

Logical Database Design &


Relational Model

Lecture - 06

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Learning Outcome
LO3: Model data requirements using data models

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

In this lecture you will learn


What is logical database design?

How to derive a logical model from the information


represented in the ER model (conceptual model)

We focus on one type of logical model which is


relational model

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Relational Model
•Data model on which most DBMS implementations
are based. CODD 1970

•Simple & elegant mod:


• Everything is a relation (= table)
Every relation is a table with rows &
columns

•There are standard ways to convert from


ER model (Conceptual Model )

Relational model (Logical Model )

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Relational Model (contd.)


The relational model represents the database as a
collection of relations.

Relation consists of
Relation schema
Relation instance (table)

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Relation
RELATION:
Schema
Instance

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Relation Schema
Describes the column heads (attributes) of the
relation

name of the relation,


name of each filed,
domain of each field

Domain : is described by domain name and set


of associated values.

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Relation Schema
Schema Name(relation name)

•Students
(sid:string,
name:string,
login:string, domain
Field name
age :integer,
gpa :real )

• Domain GPA : real (0-4)

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Relation Instance
Set of tuples or records or rows :
Each tuple has the same number of fields as the
relation schema
Example : Relation Instance

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Degree of a relation
The degree of R is the number of attributes in R
(ID,Name,Address,Phone)=4

ID Name Address Phone


100 Sampath Moratuwa 01992883
110 Amali Colombo -3 01983733
120 Sanath Negombo null

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Example
Make a list of students in the class, keeping their ID,
name and phone number.
ID Name Phone
S01 Mike 111
S02 Elisa 222

This is the basic structure of the relation model, a


table or relation.

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Formalizing : Relations
Definition: A relation is a named table of data
Table is made up of rows (records or tuples), and columns
(attributes or fields)
Not all tables qualify as relations. Requirements:
1. Every relation has a unique name
2. Every attribute value is atomic (not multivalued, not
composite)
3. Every row is unique (can’t have two rows with exactly the same
values for all their fields)
4. Attributes (columns) in tables have unique names
5. The order of the columns is irrelevant
6. The order of the rows is irrelevant

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Formalizing Key Fields

Primary key (PK)


Minimal set of attributes that uniquely identifies a
row
This is how we can guarantee that all rows are unique

Foreign key (FK)


Set of attributes in a table that serves as a reference
to the primary key of another table

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Foreign Key
A constraint involving two relations
referencing relation
referenced relation.
Tuples in the referencing relation have attributes FK
(called foreign key attributes) that reference the
primary key attributes PK of the referenced relation
referencing Enrolled (cid ,grade, studid) FK

referenced Students (sid, name ,login, age, gpa)


PK
Display the foreign keys by drawing an arrow from the
foreign key to the primary key

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Integrity Constraints - IC
DBMS must prevent entry of incorrect information.
To prevent : Constraints / conditions are specified on
a relational schema = ICs
Database which satisfies all constraints specified on
a database schema is a legal instance.
DBMS enforces constraints - permits only legal
instances to be stored.
When the application is run the DBMS checks for the
violation and disallows the changes to the data that
violates the specified IC.
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

Integrity Constraints
Specified and enforced at different times.

Specified : When the DBA /end user defines


the data base schema

Enforced : When database application is run


DBMS checks for violations
Disallow violating entries

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Integrity Constraints
Many kinds of ICs:

Domain constraints
Key constraints
Entity integrity constraints
Referential integrity constraints

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Domain Constraints
•Domain constraints: value in the Column must be
drawn from the domain associated with that
column

•Restricts the :
• Type
• Values that can appear in the field
Eg.
• Name Char (25)
• GPA (real >=0, =<4)

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Key constraints
Is a statement that;
A certain minimal subset of the fields of a relation
is a unique identifier for a tuple.

Which Means
Two tuples in a legal instance cannot have
identical values in all the fields of a key.

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Constraints…
•Entity Integrity Constraints: states that primary key
values cannot be null.
• This is because primary key values are used to identify
the individual tuples.
•Referential Integrity Constraints
• Sometimes information stored in one relation is linked
to information stored in another relation.
• If one is modified the other must be modified to keep
the data consistent.
• An IC involving both relations must be specified.
• IC involving 2 relations is a foreign key constraint.
• Foreign keys enforce referential integrity constraints.

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Referential Integrity
The value in the foreign key column ( can be either:
a value of an existing primary key in the referenced relati
or a null.

FK
cid grade stuid PK Students
Carnatic101 C 53666 sid name login age gpa
B 53666 Jones jones@cs 18 3.4
Reggae203 53666
53688 Smith smith@eecs 18 3.2
Topology112 A 53650
53650 Smith smith@math 19 3.8
History105 B 53666

Enrolled
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

DB operations & Constraints


IC are specified when a relation is created and enforced
(checked) when a relation is modified.

3 types of modifications to the relation :


Insert : inserts a new tuple(s) into a relation.

Delete : delete tuple(s) in a relation.

Update : changes the values of some attributes in


existing tuples .

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Insert Operation
The insert operation can violate the following
constraints:
Domain constraints (invalid value)
Key constraints (duplicate key values)
Entity integrity constraints (null primary key value)
Referential integrity constraint (non-existing
primary key value)

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Examples
Students sid name login age gpa
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

Domain constraints (invalid value)


Insert <‘abc’,’Tom’,’tom@lk’,’17’,3.2>into Students
sid value is not in the domain

Enrolled cid Grade sid


IT A 53666
IS B 53650

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Examples
Students sid name login age gpa
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

Key constraints (duplicate key values)


Insert <‘53666’,’Tom’,’tom@lk’,’17’,3.2>into Students

Enrolled cid Grade sid


IT A 53666
IS B 53650

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Examples
sid name login age gpa
Students
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

Entity integrity constraints (null primary key value)


Insert <null,’Tom’,’tom@lk’,’17’,3.2>into Students

Enrolled cid Grade sid


IT A 53666
IS B 53650

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Examples Students sid name login age gpa


53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

Referential integrity constraint (non-existing primary key


value)
Insert <‘IT’,’A’,’53900’>into Enrolled

Enrolled cid Grade sid


IT A 53666
IS B 53650

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Delete Operation
•Delete operation can violate referential integrity.
Enrolled Students
cid grade stuid sid name login age gpa
Carnatic101 C 53666 53666 Jones jones@cs 18 3.4
Reggae203 B 53666 53688 Smith smith@eecs 18 3.2
Topology112 A 53650 53650 Smith smith@math 19 3.8
•Two
History105
options:B 53666
• Reject the deletion
• Cascade the delete

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Update Operation
Update operation can be considered as a deleting a
tuple and re-inserting the tuple with new values.
All constraints discussed in Insert & Delete need to
be considered.
Domain constraints (invalid value)
Key constraints (duplicate key values)
Entity integrity constraints (null primary key
value)
Referential integrity constraint (non-existing
primary key value)
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

ER to Relational Mapping…
In the Database Design process, we firstly derive a
conceptual model (ER Diagram)
This model needs to be mapped to the relational model
in order to be implemented using a relational DBMS
(RDBMS). Moving from Conceptual (ER) to lower level
Logical Model (Relational)
ER is independent of the details of the implementation
(relational, network or OO)_
This section discusses the rules that can be used for this
process

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Mapping: Regular Entity


ER Model Relational Model

Entity(strong)  Relation

For example,
name ARTIST

Primary key Artist ID name

ID
Primary key
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

Mapping: Composite Attribute


ER Model Relational
Model
Composite attributes  Set of simple atomic
attributes
EMPLOYEE

Employee surname firstname

fullname
firstname

surname
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

Mapping: Multivalued Attributes


ER Model Relational Model
Multivalued attribute  Relation & Foreign Key

Employee
Employee
name id name

id phone
Employee_contact

phone id

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Mapping:1:1 with Total & Partial


participation
If there is a 1:1 relationship R from entity A to B
and if B is in total participation with A on R then
the foreign key is placed in B.
Employee

eid

Employee manage
Dept Dept

eid dno mgreno


dno
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

Mapping:1:1 with total participation


If there is a 1:1 relationship R from entity A to B and if A
and B are both in total participation with R then A & B
can be collapsed as 1 table

A has B AB

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Mapping :1:N Conversion


1:N relationships, post the identifier (Primary Key) from
the ‘one’ side as an attribute into the ‘many’ side.

C-TYPE
C-MAX C-NAME

Course C-ID
TEL
1 DIRECTOR
FK
Is registered

N S-DEGREE

Student
S-DOB

S-ID S-NAME
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

Mapping : M:N Conversion

FK

FK

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Mapping ..N-array Relationships


ER Model Relational Model
N-ary relationship “Relationship” relation
and n foreign keys
B
A
pkA pkB

A B
R pkA pkB pkC pkD
r
C D
C D pkC pkD

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Mapping Weak Entities … (contd.)


ER Model Relational Model
Weak Entity Relations and combination of
partial and primary keys

1
Employee EMPLOYEE

Policy id
id
N
name Dependents DEPENDENTS

id name age
age
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

Your Turn – Map to a Relational


Schema

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

Lecture Outline
• ER-to-Relational Mapping Algorithm

– Step 1: Mapping of Regular Entity Types


• Multivalued attributes.
• Composite attributes

– Step 2: Mapping of Weak Entity Types


– Step 3: Mapping of Binary 1:1 Relation Types
– Step 4: Mapping of Binary 1:N Relationship Types.
– Step 5: Mapping of Binary M:N Relationship Types.
– Step 6: Mapping of N-array Relationship Types.
SLIIT - Faculty of Computing
IT1090 - Information Systems and Data Modeling

Summary of Mapping constructs and constraints

Correspondence between ER and Relational Models


ER Model Relational Model
Entity type “Entity” relation
1:1 or 1:N relationship type Foreign key (or “relationship” relation)
M:N relationship type “Relationship” relation and two foreign keys
N-array relationship type “Relationship” relation and n foreign keys
Simple attribute Attribute
Composite attribute Set of simple component attributes
Multivalued attribute Relation and foreign key
Value set Domain
Key attribute Primary (or secondary) key

SLIIT - Faculty of Computing


IT1090 - Information Systems and Data Modeling

End of Lecture 06

Questions ?

43
SLIIT - Faculty of Computing

You might also like