You are on page 1of 49

The Relational Model

Chapter 3

1
Outline

• Introduction to Relational model.


• How data is represented.
• How to obtain the database design in the
relational model.

2
Introduction

• The relational model was first


introduced by Ted Codd of IBM
Research in 1970.
• Attracted immediate attention due to
its
– simplicity,
– elegance

3
Introduction to the Relational Model:

Basis of the Model

• The main construct for representing data in


the relational model is a relation, where each
relation is a table with rows and columns.
• A relation consists of 2 parts:
– a relation schema and
– a relation instance.
• The relation schema describes the column
heads for the table
• The relation instance is a table.

4
Introduction to the Relational Model:

Basis of the Model

Schema
• The schema specifies;
– the relation's name,
– the name of each field (or column, or attribute), and
– the domain of each field.
– A domain is referred to in a relation schema by the
domain name and has a set of associated values.
• E.g.
Students(sid: string, name: string, login: string, age: integer, gpa:
real).
• Field named sid has a domain named string.
– The set of values associated with domain string is the
set of all character strings. 5
Introduction to the Relational Model:

Basis of the Model

Instance
• An instance of a relation is a set of tuples,
also called records, in which each tuple has
the same number of fields as the relation
schema.
• A relation instance is a table
– in which each tuple is a row, and
– all rows have the same number of fields.

6
Introduction to the Relational Model:

Basis of the Model

• An instance of the Students relation

It has six tuples and five fields.


– Note that no two rows are identical.
– This is a requirement of the relational model - each relation
is defined to be a set of unique tuples or rows.
7
Introduction to the Relational Model:

Basis of the Model

• A relation schema specifies the domain of


each field or column in the relation instance.
• These domain constraints in the schema
specify an important condition on each
instance of the relation to satisfy:
– The values appear in a column must be drawn
from the domain associated with that column.
– The domain of a field is essentially the type of
that field, restricts the values that can appear in
the field.
8
Introduction to the Relational Model:

Basis of the Model

• Domain constraints are important in the relational


model.
– Consider only relation instance that satisfy them.
• Therefore,
– Relation instance means relation instance that
satisfies the domain constraints in the relation schema.
• The cardinality of a relation instance is the
number of tuples in it.
• The degree (arity) of a relation is the number of
fields.
9
Example Instance of
Students Relation

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

 Cardinality = 3, degree = 5, all rows distinct


 Do all columns in a relation instance have to
be distinct?
10
Characteristics of
Relations
• Tuples have no particular order.
• Ordering of attributes not important.
• No two rows (or tuples) are identical.
• All values belonging to a particular
attribute are from the same domain.
• attributes are atomic.
• attributes may have a null value.

11
Definition Summary

Informal Terms Formal Terms


• Table • Relation
• Column
• Row • Attribute/Field
• Values in a column
• Table Definition • Tuple
• Populated Table
• Domain

• Schema of Relation
(Intention)
• Extension (relation state)

12
Logical Database Design:
ER-to-Relational
Conceptual
Design

Conceptual Schema
(ER Model)

Logical Design

Logical Schema
(Relational Model)
13
ER-to-Relational Mapping
Minit
Fname Lname 1
Address Name Locations
N WORKS_FOR
Name Sex
NumberOfEmployees DEPARTMENT
Salary
EMPLOYEE

MANAGES 1 1
Hours
1 N 1
WORKS_FOR CONTROLS

supervisor supervisee
DEPENDENTS_OF
N
SPERVISION N PROJECT

DEPENDENTS Name
Name
Number Location
Sex BirthDate 14
ER-to-Relational Mapping

• Given an ER-diagram to translate it to an equivalent


relational schema
• STEP 1: Map Entity Types
– For each entity type Ei in the ER-diagram
• Create a relation schema Ri that includes the simple
attributes of Ei
• Include simple attributes of composite ones of Ei in Ri
• The primary key of Ei becomes the primary key of Ri
• If the primary key is composite in Ei then the set of
simple attributes in Ri forms a composite primary key
• Summary: Each entity type becomes a relation
15
ER-to-Relational Mapping

name
EmpNo
CREATE TABLE Employee
rank
(Eno CHAR(11),
Employees
name CHAR(20),
rank CHAR(20),
PRIMARY KEY (Eno))
Employee (EmpNo, name, rank)

Note the Primary Key of table is the


key of entity type.

16
ER-to-Relational Mapping
• STEP 2: Map Weak Entity Types Wk- draw identifier from parent
entity type into weak entity type
– For each weak entity Wk in the ER schema with owner entity type E,
• Identify relations R1,…,Rn translated from E1,…,En, respectively
• Create a relation Rk that include all simple attribute (or simple
components of composite attributes) of Wk
• Include the primary keys of R1,…,Rn as foreign keys in Rk that
reference R1,…,Rn, respectively
– This takes care of the identifying relationship type of Wk.
• i.e. Weak entity set and identifying relationship set are
translated into a single table.
– When the owner entity is deleted, all owned weak entities
must also be deleted.
• Summary: Each weak entity type becomes a relation with foreign
keys to its owners
17
ER-to-Relational Mapping
since
name dname
EmpNo rank relationship

Employees Has Dependents

Employee (EmpNo, name, Dependents (EmpNo, dname,


rank) relationship)

CREATE TABLE Employee CREATE TABLE Dependents


(Empno CHAR(11), (dname CHAR(20),
name CHAR(20), Empno CHAR(11) NOT NULL,
rank CHAR(10), relationship CHAR(20),
PRIMARY KEY (EmpNo)) PRIMARY KEY (dname, EmpNo),
FOREIGN KEY (EmpNo) REFERENCES
Employee ON DELETE 18
ER-to-Relational Mapping

(STEPS 3 to 5): Map Relationship Types


• 1:1 - options for setting up one, two or
three relations
• 1:N - the many side provides the key
• M:N - need to set up a separate relation for
the relationship

19
Mapping
1-1 Relationship Types
• STEP 3
– For each binary 1:1 relationship between Ei and Ej
• Identify relations Ri and Rj translated from Ei and
Ej, respectively
• Choose one relation (say Ri) and include the
primary key of Rj as a foreign key in Ri that
references Rj (this forces the cardinality 1 constraint
on the one edge)
• Include the simple attributes of the 1:1 relationship
as attributes in the chosen relation (above it would
be Ri)
• Summary: Create a foreign key and move relationship
attributes to one side of the 1:1 relationship
20
Mapping
1-1 Relationship Types
since
name dname
EmpNo rank did budget

Employees Manage Departments


1 1

Option 1:
Employee (EmpNo, name, rank) Dept (did, EmpNo, dname, budget, since)

Option 2:
Employee (EmpNo, did, name, rank, since) Dept (did, dname, budget)

21
Mapping
1-1 Relationship Types
since
name dname
EmpNo rank did budget

Employees Manage Departments


1 1

Total participation on one side:


Move attributes to the total
participation side
Employee (EmpNo, name, rank) Dept (did, EmpNo, dname, budget, since)

22
Mapping
1-1 Relationship Types
since
name dname
EmpNo rank did budget

Manager Manage Departments


1 1

Option 1: Setting up ONE relation


ManageDept (EmpNo, did, name, rank, dname, budget, since)

Note: An alternative mapping of 1:1 relationship type is


possible by merging the two entity types and the
relationship type into a single Relation.
This is appropriate when BOTH PARTICAPTIONS ARE
TOTAL. 23
Mapping Recursive
Relationship
name
EmpNo rank
supervisor

Employees Reports_to

subordinate

CREATE TABLE Reports-To (


supervisor -EmpNo CHAR ( 11) ,
subordinate- EmpNo CHAR(11),
PRIMARY KEY (supervisor- EmpNo, subordinate- EmpNo),
FOREIGN KEY (supervisor- EmpNo) REFERENCES Employees(EmpNo),
FOREIGN KEY (subordinate- EmpNo) REFERENCES Employees(EmpNo) );
24
Mapping
1-N Relationship Types

 STEP 4:
– For each binary 1:N relationship between Ei and Ej
• Identify relations Ri and Rj translated from Ei and Ej,
respectively
• Choose the relation on the N side of the relationship
(say Ri) and include the primary key of Rj as a
foreign key in Ri that references Rj
• Include the simple attributes of the 1:N relationship
as attributes in the relation on the N side (above, it
would be Ri)
 Summary: Create a foreign key and move relationship
attributes to the N side of the 1:N relationship
25
Mapping
1-N Relationship Types
since
name dname
EmpNo rank did budget

Employees works Departments


N 1

Employee (EmpNo, did, name, rank , since) Dept (did, dname, budget)
CREATE TABLE Employee(
EmpNo INTEGER,
did INTEGER,
name CHAR(10),
rank INTEGER,
since DATETIME,
PRIMARY KEY (EmpNo),
FOREIGN KEY (did) REFERENCES Employee (did)) 26
Mapping
1-N Relationship Types
• Does every department have an employee(s)?
– If so, this is a participation constraint: the participation of
Departments in Works is said to be total (vs. partial).
• Every EmpNo value in Employees table must have a
value for did field. (with a non-null did value!)
since
CREATE TABLE Employee( name dname
EmpNo INTEGER,
EmpNo rank did budget
rank INTEGER,
name CHAR(11),
since datetime,
works
did INTEGER NOT NULL, Employees Departments
N 1
PRIMARY KEY (EmpNo),
FOREIGN KEY (did)
REFERENCES Departments
ON DELETE NO ACTION)
27
Mapping
M-N Relationship Types
• STEP 5 :
– For each binary M:N relationship between Ei and Ej
• Identify relations Ri and Rj translated from Ei and Ej,
respectively
• Create a new relation scheme Rk representing the
relationship
• Include the primary key of Ri and Rj as foreign keys in Rk
that reference Ri and Rj, respectively
• The composition of foreign keys in Rk will form the primary
key of Rk
• Include the simple attributes of the M:N relationship as
attributes in Rk
• Summary: Each M:N relationship becomes a relation with
foreign keys to the participants
28
Mapping
M-N Relationship Types
responsibilties
name pname
EmpNo rank ProjNo budget

Employees works Project


N M

Employee (EmpNo, name, rank) Project (ProjNo, pname, budget)

Works (EmpNo, ProjNo, responsibilties)

CREATE TABLE Works (


Empno CHAR(10),
Projno CHAR(10),
responsibilties CHAR(15),
PRIMARY KEY (Empno, Projno),
FOREIGN KEY (Projno) REFERENCE Proj (Projno),
FOREIGN KEY (Empno) REFERENCE Emp (Empno)); 29
Exercise: Map the following ER
diagram to relational schema
phone custNo cname
dname
Customer
Department
1 phone
1

Owns
DoneBy
RegNo

endDate 1 N Manufacturer
startDate WorkOn
N Aircraft
Maintenance
N model
Replaced M
Parts cost
jobNo labour

partCode pname 30
ER-to-Relational
Mapping
• STEP 6: Map multi-valued attributes – set up a new
relation
– For each multi-valued attribute Ak of entity type Ei
• Identify relation Ri translated from Ei
• Create a new relation scheme Rk and include Ak as a
simple attribute of Rk
• Include the primary key of Ri as a foreign key in Rk
that references Ri
• The composition of Ak with the foreign key in Rk will
form the primary key of Rk
• Summary: Each multi-valued attribute becomes a
relation with a foreign key to its entity type

31
ER-to-Relational
Mapping
name
EmpNo rank
Employee (EmpNo, name, rank)

Employees Qualification (EmpNo, qualification)

qualification
CREATE TABLE Qualification (
EmpNo INTEGER,
qualification CHAR(20),
PRIMARY KEY (Empno,
qualification),
FOREIGN KEY (Empno)
REFERENCES Employee (EmpNo)
ON DELETE CASCADE)

32
ER-to-Relational Mapping
• STEP 7: Map higher order relationships (ternary, 4- way etc.) –
they become separate relations.
– For each n-ary relationship (n > 2) between E1,…,En
• Identify relations R1,…,Rn translated from E1,…,En,
respectively
• Create a new relation scheme Rk representing the
relationship
• Include the primary keys of R1,…,Rn as foreign keys in Rk
that reference R1,…,Rn, respectively
• The composition of foreign keys in Rk will form the primary
key of Rk
• Include the simple attributes of the n-ary relationship as
attributes in Rk
• Summary: Each n-ary relationship becomes a relation with
foreign keys to the participants
33
ER-to-Relational Mapping
Name pname

SupplierNo location ProjNo budget

Supplier (SNo, Name, location)


Supplier Project

Project (projNo, pName, budget)


supplies
Supplies (SNo, partNo, ProjNo)
CREATE TABLE Supplies ( Part (partNo, pName)
SNo INTETER,
PartNo INTERGER, Part
partName
ProjNo INTERGER,
PRIMARY KEY (SNo, partNo, ProjNo),
FOREIGN KEY (SNo) REFERENCES Supplier (SNo), partNo
FOREIGN KEY (partNo) REFERENCES Project (partNo),
FOREIGN KEY (ProjNo) REFERENCES Part (ProjNo))
34
ER-to-Relational Mapping -
Super class / Subclass Relationship

• STEP 8: Mapping of generalization hierarchies


and set-subset relationships

• Several alternative strategies

35
ER-to-Relational Mapping -
Super class / Subclass Relationship

• Option A:
– Create a table for each class.
• Retain the table for the super class created in
• Create a table for each subclass with columns for
the attributes in the subclass
• Add the primary key of the super class. This is a
foreign key and (possibly part of) the primary key.

36
Superclass / Subclass
Option A
name Employee EmpNo

HourlyRate contractid
ISA

HourlyEmp ContractEmp period


NoHours

Employee (EmpNo, name)


HourlyEmp (EmpNo, name, hourlyRate, noHours)
ContractEmp (EmpNo, contractid, name, period)
CREATE TABLE HourlyEmp ( CREATE TABLE ContractEmp (
EmpNo INTEGER, EmpNo INTEGER,
Name VARCHAR(20), Contractid INTEGER,
hourlyRate REAL, Name VARCHAR(20),
noHours INTERGER, Period DATETIME,
PRIMARY KEY (EmpNo), PRIMARY KEY (Contractid),
FORIEGN KEY (EmpNo) REFERENCES FORIEGN KEY (EmpNo) REFERENCES
37
Employee (EmpNo)) Employee (EmpNo))
Superclass / Subclass
Option B

• Option B:
– Create a table for each subclass
• A table has as columns for all the simple
attributes in the subclass and
superclass(es)
• The primary key is the primary key of the
superclass.
– This requires the covering constraint to hold.

38
Superclass / Subclass
Option B
name Student SNo

GPA researchTitle
ISA

Undergraduate Postgraduate
Major

Undergraduate (SNo, name, major, gpa)


Postgradua te (SNo, name, research)
No table for Student
CREATE TABLE Undergraduate ( CREATE TABLE Postgraduate (
SNo INTEGER, SNo INTEGER,
Name VARCHAR(20), Name VARCHAR(20),
major VARCHAR(20), researchTitle VARCHAR(20),
GPA REAL, PRIMARY KEY (SNo))
PRIMARY KEY (SNo))
39
Comparison of Options A & B
• Option A
– This is more general.
– The disadvantage is that an extra relation is needed.
– More operation may be necessary when need to get the
employee information (looking up two relations).
• Option B
– It is not always possible.
– The advantage of this method that 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.
40
ER-to-Relational Mapping -
Aggregation
• Translating aggregation into the relational model is
easy because…
– there is no real distinction between entities and
relationships in the relational model.
name
ssn salary
Employees

Monitors until

pid since
did
N M
Projects Sponso Departments
rs
Started_on pbudget dname
41
ER-to-Relational Mapping -
Aggregation

• Suppose that we have an entity set called


PROJECT and that each PROJECTs entity is
sponsored by one or more DEPARTMENTs.
• The Sponsors relationship set captures this
information.
• Therefore, the key attributes of SPONSORS are
(did, pid) – the sponsorship is determined by the
department id and the project id.
• For the Monitors relationship create a relation:
MONITORS (ssn, did, pid, until)

42
ER-to-Relational Mapping -
Aggregation
• There is a special case in which this translation can be
refined by dropping the Sponsors relation.
– if Sponsors has no descriptive attributes and has total
participation in Monitors, every possible instance of the
Sponsors relation can be obtained by looking at the (pid, did)
columns of the Monitors relation.
• Consider the Sponsors relation.
– It has attributes:
• SPONSORS (pid, did, since) and in general we need it (in addition to
Monitors) for two reasons:
– We have to record the descriptive attributes (in our example, since)
of the Sponsors relationship.
– Not every sponsorship has a monitor, and thus some (pid, did)
pairs in the Sponsors relation may not appear in the Monitors
relation.
43
Considerations in the
Mapping

• Reduce data redundancy (same


information stored more than once)
• Reduce the number of NULL values.
• Reduce cost of looking up the information
(cost is less if it is from one relation
versus from more relations)

44
Summary of Mapping for
Model Constructs & Constraints

ER Model Relational Model

Entity type Relation


1:1 or 1:N relationship Foreignkey or realtionship
type relation
M:N relationship type Relationship relation and 2 FK
N-ary relationship type Relationship relation and n FK
Simple attributes Attribute
Composite attributes Set of simple attributes
Multi-values attributes Relation and FK
Value set Domain
Key attribute Primary key
45
Number
Minit
Fname Lname
Address (1,N) Name Locations
(1,1)
WORKS_FOR
Name Sex Salary employee department

NumberOfEmployees DEPARTMENT
Ssn Salary
EMPLOYEE (1,1)
(0,1)
Bdate department-
manager MANAGES managed (0,N)
Hours Controlling-
(0,1) department
worker
(0,N) WORKS_FOR CONTROLS
(0,1)
(1,N)
supervisor supervisee (0,N) Controlled-
employee project project
(1,1)

SPERVISION DEPENDENTS_OF PROJECT

Name
ER diagram for the Dependent
COMPANY schema, with (1,1) Number Location
all role names included and DEPENDENTS
with structural constraints
on relationship specified
using the alternate notation
Name
Relationship
(min, max) Sex BirthDate 46
Exercise: Map the following ER
diagram into relational schema
name Person ssn tradename formula

specialty phone ISA address Drugs

Doctor Patients price


sickness N
N M

Treat Sell
qty
1
M SoldBy
N
Prescribe
Primary date name Pharmacy
Physician
M
startdate
N 1 N
Contract Pharmaceutical
Supervisor supervise
ssn Company
enddate
name id phone 47
Exercise: Map the following ER
diagram into relational schema

• Assume the following constraints


– Patients and Doctors cover Person.

48
Thank you!

49

You might also like