Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 53

COMP231

Relational Model

COMP231 1
Outline
 Introduction
 Terminology
 ER-to-Relational Mapping

COMP231 2
Introduction
 The relational model was first introduced by
Ted Codd of IBM Research in 1970
 and attracted due to its simplicity, elegance
and mathematical foundations
 The model uses the concept of a
mathematical relation – which looks like a
table of values

COMP231 3
Outline
 Introduction
 Terminology
 ER-to-Relational Mapping

COMP231 4
Terminology
 Relation  table; denoted by R(A1, A2, ..., An) where R is a
relation name and (A1, A2, ..., An) is the relation schema of R
 Attribute (column)  denoted by Ai
 Tuple (Record)  row
 Attribute value  value stored in a table cell
 Domain  legal type and range of values of an attribute
denoted by dom(Ai)
 Attribute: Age Domain: [0-100]
 Attribute: EmpName Domain: 50 alphabetic chars
 Attribute: Salary Domain: non-negative integer

COMP231 5
Terminology
Relation Name/Table Name Attributes/Columns (collectively as a
schema)

STUDENT
Name Student-id Age CGA
Chan Kin Ho 99223367 23 11.19

Tuples/Rows
Lam Wai Kin 96882145 17 10.89
Man Ko Yee 96452165 22 8.75
Lee Chin Cheung 96154292 16 10.98
Alvin Lam 96520934 15 9.65
COMP231 6
Schema
 The relation schema is

(Name, Student-id, Age, CGA)

OR

Name Student-id Age CGA

 The primary key is underlined in the above


COMP231 7
Foreign Key
 A foreign key is a set of attributes in
one relation r that is used to refer to a
tuple in another relation s.
(it must correspond to the primary key
of the second relation)

COMP231 8
Foreign Key
 Student
 (Student-id, Student-Name)
 Take
 (Student-id, Course-id, semesterNo)

 Student-id in relation Student is a primary key


 Student-id in relation Take is a foreign key

COMP231 9
Foreign Key
 Student
 (Student-id, Student-Name)
 Take
 (Student-id, Course-id, semesterNo)
 Course
 (Course-id, Course-Name)

 Course-id in relation Course is a primary key


 Course-id in relation Take is a foreign key

COMP231 10
Outline
 Introduction
 Terminology
 ER-to-Relational Mapping

COMP231 11
ER-to-Relational Mapping
 Typically, database designers begin with the
ER model, which is very expressive and
user-friendly to human
 Then, the ER model is mapped to the
relational model for DBMS manipulations
 Database queries and updates will be written
according to the relational model

COMP231 12
ER-to-Relational Mapping
 Translating traditional ER diagrams
 Translating Class Hierarchy

COMP231 13
name sex addr works_for dname number
eid
Employee start_date Department
bdate
Manages
salary
supervisor supervisee hours Controls

supervision works_on
Project
Dependent_of pname
pnumber plocation

name bdate
Dependent
sex relationship

COMP231 14
Steps
 Step 1 (Strong Entity Set)
 Step 2 (Weak Entity Set)
 Step 3 (1-to-1 Relationship)
 Step 4 (1-to-many Relationship)
 Step 5 (Many-to-many Relationship)
 Step 6 (Non-binary Relationship)

COMP231 15
Step 1 (Strong Entity)
 For each strong entity set E in the ER
schema,
 create a relation schema R that includes all
the attributes of E.
 choose one set of key attributes of E as a
primary key for R.

COMP231 16
name sex addr works_for dname number
eid
Employee start_date Department
bdate
Manages
salary
supervisor supervisee hours Controls

supervision works_on
Project
Dependent_of pname
pnumber plocation

name bdate
Dependent
sex relationship

COMP231 17
name sex addr dname number
eid
Employee Department
bdate
salary

Project
pname
pnumber plocation

COMP231 18
Step 1 (Strong Entity)
 Example
 We create the relation schemas EMPLOYEE,
DEPARTMENT and PROJECT.

EMPLOYEE name eid bdate addr sex salary

DEPARTMENT dname dnumber

PROJECT pname pnumber plocation

COMP231 19
 If there is a derived attribute, what
should we do?

We have two choices.


Choice 1: Include this derived attribute
Adv: We can directly obtain the value of the derived attribute
Disadv: We may encounter some data inconsistencies
Choice 2: NOT include this derived attribute
Adv: We can avoid data inconsistency
Disadv: We need to perform some operations to obtain the
value of the derived attribute

COMP231 20
street city
city
street

Employee country
country
Employee
address
address

 If there is a composite attribute, what


should we do?

We have two choices.


Choice 1: Include the high-level attribute only (e.g., address)
Choice 2: Include all low-level attributes (e.g., street, city, country)

COMP231 21
eid
eid

Employee phone
phone
Employee

 If there is a multi-valued attribute, what


should we do?

We have two choices.


Choice 1: Include one attribute only (e.g., phone)
Choice 2: Create another table containing the primary key of the entity set
and the multi-valued attribute
e.g., create a schema
PhoneTable (eid, phone)

COMP231 22
Step 2 (Weak Entity)
 For each weak entity set W in the ER
model,
 create a relation schema R, and include all
attributes.
 In addition, include the primary key(s) of the
owner(s).
 The primary key of R is the combination of the
primary key(s) of the owner(s) and the
discriminator of the weak entity set W.

COMP231 23
name sex addr works_for dname number
eid
Employee start_date Department
bdate
Manages
salary
supervisor supervisee hours Controls

supervision works_on
Project
Dependent_of pname
pnumber plocation

name bdate
Dependent
sex relationship

COMP231 24
name sex addr
eid
Employee
bdate
salary

Dependent_of

name bdate
Dependent
sex relationship

COMP231 25
Dependent
eid dependent-name sex bdate relationship

COMP231 26
Step 3 (1-to-1 Relationship)
 For each binary one-to-one (1:1)
relationship set R
T ---- S

 Choose one of the 2 relation schemas, say S,


 get primary key of T,
 include it as foreign keys in S.
 Better if S has total participation in R
 Include the attributes of the relationship set R as
attributes of S.

COMP231 27
name sex addr works_for dname number
eid
Employee start_date Department
bdate
Manages
salary
supervisor supervisee hours Controls

supervision works_on
Project
Dependent_of pname
pnumber plocation

name bdate
Dependent
sex relationship

COMP231 28
name sex addr dname number
eid
Employee start_date Department
bdate
Manages
salary

COMP231 29
We include the primary key of EMPLOYEE as foreign
key in DEPARTMENT and rename it mgr_id.
We include the attribute startdate of MANAGES and
rename it mgr_start_date.
DEPARTMENT dname dnumber mgr_id mgr_start_date

COMP231 30
Compare the following two choices EMPLOYEE DEPARTMENT
MANAGES
to include MANAGES:

Add information to EMPLOYEE

Add to DEPARTMENT
COMP231 31
 In the above, the NULL value is a special value
meaning that the value is either unknown or not
applicable.
 Notice that an alternative mapping of a one-to-one
relationship set is possible by merging the two
entity sets and the relationship into a single relation.

 This is appropriate when both participations


Step 3: are
total. Advantage:
The total number of relations
remain unchanged
Disadvantage:
It may store NULL values
if there is no total participation
COMP231 32
name sex addr dname number
eid
Employee start_date Department
bdate
Manages
salary

Can we create a new relation

Manages (eid, number, start_date) It can be used if there are only


a few relationship instances
Or Advantage:
It can avoid storing NULL values
Manages (eid, number, start_date) if there is no total participation
Disadvantage:
for this relationship? There is one additional
Yes.
relation
COMP231 33
Step 4 (1-to-many
Relationship)
 For each binary one-to-many
relationship set
T ---- S
 Include as foreign key in S the primary key
that represents the other entity set T
participating in R.
 Include any attributes of the one-to-many
relationship set as attributes of S.

COMP231 34
name sex addr works_for dname number
eid
Employee start_date Department
bdate
Manages
salary
supervisor supervisee hours Controls

supervision works_on
Project
Dependent_of pname
pnumber plocation

name bdate
Dependent
sex relationship

COMP231 35
name sex addr works_for dname number
eid
Employee Department
bdate
salary
supervisor supervisee Controls

supervision
Project
pname
pnumber plocation

COMP231 36
 The primary key dnumber of the DEPARTMENT relation schema is
included as foreign key in the EMPLOYEE relation schema.

 We rename it as dno. (The renaming is not necessary but makes the


name more meaningful.)

EMPLOYEE name eid bdate addr sex salary dno

EMPLOYEE WORKS_FOR DEPARTMENT

COMP231 37
Add employees to department
Compare the following 2 choices:

DEPARTMENT

EMPLOYEE

COMP231 Add department to employee 38


 For SUPERVISON,
include the primary key of the EMPLOYEE as
foreign key in the EMPLOYEE, and call it super_id.

EMPLOYEE name eid bdate addr sex salary dno super_id

 For CONTROLS relationship,


include dnum as foreign key in PROJECT,
which references the primary key dnumber of DEPARTMENT.

PROJECT pname pnumber plocation dnum


COMP231 39
name sex addr works_for dname number
eid
Employee Department
bdate
salary
supervisor supervisee Controls

supervision
Project
Can we create a new relation pname
pnumber plocation
works_for (eid, number)

for this relationship? Yes.

COMP231 40
Step 5 (Many-to-many
Relationship)
 For each binary many-to-many
relationship set R,
 create a new relation schema S to represent R.
 Include as foreign key attributes in S the primary
keys of the relation schemas for the participating
entity sets in R
 their combination will form the primary key of S.
 Also include attributes of the many-to-many
relationship set as attributes of S.

COMP231 41
name sex addr works_for dname number
eid
Employee start_date Department
bdate
Manages
salary
supervisor supervisee hours Controls

supervision works_on
Project
Dependent_of pname
pnumber plocation

name bdate
Dependent
sex relationship

COMP231 42
name sex addr
eid
Employee
bdate
salary
hours

works_on
Project
pname
pnumber plocation

COMP231 43
WORKS_ON eid pnumber hours

Map the many-to-many relationship sets


WORKS_ON by creating the
relation schema WORKS_ON.
Include the primary keys of PROJECT and EMPLOYEE as
foreign keys.

COMP231 44
Compare the following three choices to include works_on
Add to EMPLOYEE

name id bdate addr sex salary dno pnumber hours


Yeung 7 080370 … F 20K 3 Null Null
WORKS_ON
EMPLOYEE PROJECT Chan 3 031060 … M 30K 4 C77 89
Chan 3 031060 … M 30K 4 A01 10
Wong 4 010280 … F 10K 7 A01 101
Cheung 8 220985 … M 24K 1 A01 22
Cheung 8 220985 … M 24K 1 C77 57

Add to PROJECT New relation WORKS_ON

COMP231 45
Step 6 (Non-binary
Relationship)
 For each non-binary relationship
set,
 create a new relation schema S to
represent R.
 Include as foreign key attributes in S the
primary keys of the participating entity
sets.
 Also include any attributes of the non-
binary relationship set as attributes of S.

COMP231 46
 For non-binary relationships,
 The primary key of S is usually a combination
of all the foreign keys that reference the
relations representing the participating entity
sets.
 However if any entity set has a key constraint
on the relationship (e.g., 1-to-many
relationship), the primary key of the entity set
can be used as a key for the relationship.

COMP231 47
Resulting relation schemas:
DEPARTMENT dname dnumber mgr_id mgr_start_date

EMPLOYEE name eid bdate addr sex salary dno super_id

PROJECT pname pnumber plocation dnum

DEPENDENT eid dependent-name sex bdate relationship

WORKS_ON eid pnumber hours

COMP231 48
ER-to-Relational Mapping
 Translating traditional ER diagrams
 Translating Class Hierarchy

COMP231 49
Translating Class Hierarchy
 Consider the class hierarchy example.

name
name
ID
ID address
address

Employees
Employees

Hour_worked
Hour_worked Contract_ID
Contract_ID
Hour_wages
Hour_wages ISA
ISA

Hourly_Emps
Hourly_Emps Contract_Emps
Contract_Emps
COMP231 50
Translating Class Hierarchy
 Two possible ways:

1. Map each of the entity sets Employees, Hourly-Emps,


and Contract-Emps to a distinct relation.

EMPLOYEE name id address

HOURLY-EMP id hours_worked hours_wages

CONTRACT-EMP id contract-id

COMP231 51
2. Create only 2 relations:

HOURLY-EMP id name address hours_worked hours_wages

CONTRACT-EMP id name address contract-id

This requires the covering constraint to hold.


(i.e. Hourly_Emp and Contract_Emp
COVER Employee)
COMP231 52
 The first method is more general.
 Disadvantage: an extra relation is needed.
 More operation may be necessary when we need to
get the employee information
 (e.g. looking up two relations).

 The second method is not always possible.


 Advantage : information of each employee is more
easily accessible (usually only one relation look up).
 However, if an employee can be both hourly and
contract based, then information of the employee will
be stored twice.

COMP231 53

You might also like