Professional Documents
Culture Documents
Chapter 3
1
Outline
2
Introduction
3
Introduction to the Relational Model:
4
Introduction to the Relational 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:
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:
11
Definition Summary
• 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
name
EmpNo
CREATE TABLE Employee
rank
(Eno CHAR(11),
Employees
name CHAR(20),
rank CHAR(20),
PRIMARY KEY (Eno))
Employee (EmpNo, name, rank)
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
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
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
22
Mapping
1-1 Relationship Types
since
name dname
EmpNo rank did budget
Employees Reports_to
subordinate
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
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
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)
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
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
• 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
Monitors until
pid since
did
N M
Projects Sponso Departments
rs
Started_on pbudget dname
41
ER-to-Relational Mapping -
Aggregation
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
44
Summary of Mapping for
Model Constructs & Constraints
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)
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
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
48
Thank you!
49