Professional Documents
Culture Documents
Relational Model
Relational Model
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
OR
COMP231 8
Foreign Key
Student
(Student-id, Student-Name)
Take
(Student-id, Course-id, semesterNo)
COMP231 9
Foreign Key
Student
(Student-id, Student-Name)
Take
(Student-id, Course-id, semesterNo)
Course
(Course-id, Course-Name)
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.
COMP231 19
If there is a derived attribute, what
should we do?
COMP231 20
street city
city
street
Employee country
country
Employee
address
address
COMP231 21
eid
eid
Employee phone
phone
Employee
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
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 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.
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.
COMP231 37
Add employees to department
Compare the following 2 choices:
DEPARTMENT
EMPLOYEE
supervision
Project
Can we create a new relation pname
pnumber plocation
works_for (eid, number)
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
COMP231 44
Compare the following three choices to include works_on
Add to EMPLOYEE
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
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:
CONTRACT-EMP id contract-id
COMP231 51
2. Create only 2 relations:
COMP231 53