Beruflich Dokumente
Kultur Dokumente
Relationship Mapping
Relationship Mapping
Objectives
This lesson covers the following objectives:
Apply the rule of relationship mapping to correctly
transform 1:M and barred relationships
Apply the rule of relationship mapping to correctly
transform M:M relationships
Transform 1:1 relationships
Apply the rule of relationship mapping to correctly
transform relationships in an arc
Relationship Mapping
Purpose
Suppose that you are building a house for someone. You
have all of the materials wood, paint, doors, windows,
nails, screws, etc. and the skills, but you do not have a
design.
As you start, you dont know how many rooms should be
included, where the windows should be placed, how the
doors should be oriented, or what color each room should
be painted.
Relationship Mapping
Purpose (cont.)
You could build a house in such a manner and make
these decisions as you go, but if you do not start with a
blueprint of the structural design, the final product may not
be the house that the customer has in mind.
Relationship Mapping
Purpose (cont.)
Relationships are mapped between primary keys and
foreign keys to allow one table to reference another. If we
dont map relationships, we just have a lot of standalone
tables containing information that does not connect to
anything else in the database.
Mapping relationships between entities serves as a critical
first-step to facilitate discussion between the customer,
designer, developer, and administrator of the database
product.
Relationship Mapping
Relationship Mapping
Relationship Mapping
the manager
of
EMPLOYEE
# id
* name
* address
* birth date
EMPLOYEES (EPE)
belong to
composed of
DEPARTMENTS (DPT)
Key Type
Optionality
Column
Name
Key Type
Optionality
Column
Name
pk
id
pk
id
name
uk
name
address
birth_date
fk1
dpt_id
fk2
epe_id
DEPARTMENT
# id
* name
Relationship Mapping
Relationship Mapping
Enforcing Optionality
MUSICIAN
# id
* name
o instrument
composed of
MUSICIANS (MSN)
10
BANDS (BAD)
Key type
Optionality
Column
name
pk
id
name
instrument
bad_id
fk
BAND
# id
* name
a member of
Foreign
key
refers
to
Key type
Optionality
Column
name
pk
id
name
Relationship Mapping
11
Relationship Mapping
PAY CHECK
# id
* pay period
EMPLOYEE
# id
* name
issued to
the recipient of
PAYCHECKS (PCK)
Key Type
Optionality
Column Name
pk
id
pay_period
epe_id
fk
12
Relationship Mapping
13
Relationship Mapping
located in
BANK
# number
* name
the location of
ACCOUNTS (ACT)
Key Type
Optionality
Column Name
pk
act_nbr
balance
date_opened
bak_nbr
pk,fk
refers
to
BANKS (BAK)
14
Key Type
Optionality
Column Name
pk
bank_number
name
Relationship Mapping
15
Relationship Mapping
16
Relationship Mapping
the location of
ROOMS (ROM)
17
located within
located within
located within
ROOM
# number
BUILDING
# id
* address
FLOOR
# number
the location of
the location of
SUITES (SUE)
pk
rom_nbr
pk
sue_nbr
pk, fk
sue_nbr
pk, fk
flr_nbr
pk, fk
sue_flr_nbr
pk, fk
flr_bdg_id
pk, fk
sue_bdg_id
tenant
FLOORS (FLR)
BUILDINGS (BDG)
pk
flr_nbr
pk
pk, fk
bdg_id
id
address
Relationship Mapping
flr_nbr
flr_bdg
id
address
15
100
100
40 Potters Lane
25
100
201
5E
201
7B
201
FLOORS
18
sue_nbr
flr_nbr
bdg_id
100
100
201
201
ROOMS
rom_nbr
sue_nbr
sue_flr_nbr
sue_bdg_id
15
100
15
100
7B
201
Relationship Mapping
19
Relationship Mapping
CRITIC
# id
* name
MOVIE
# id
* title
CRITICS (CTC)
pk
REVIEWS (RVW)
id
name
MOVIES (MVE)
pk
20
id
title
Key Type
Optionality
Column Name
pk, fk1
ctc_id
pk, fk2
mve_id
rating
Relationship Mapping
21
Relationship Mapping
SODA BOTTLE
# id
* name
BOTTLE CAP
# code
* description
sealed with
the sealer for
SODA_BOTTLES (SBE)
pk
fk, uk
22
id
name
bcp_code
BOTTLE_CAPS (BCP)
pk
code
description
Relationship Mapping
Optional One-to-One
If the relationship is optional on both sides, you can
choose which table gets the foreign key. There are no
absolute rules, but here are some guidelines:
Implement the foreign key in the table with fewer rows to
save space.
Implement the foreign key where it makes more sense
for the business.
23
Relationship Mapping
24
Relationship Mapping
SPACE
# id
* description
parked in
the location for
Car-Rental Business
CARS (CAR)
pk
fk, uk
SPACES (SPE)
lic_plate
model
spe_id
pk
id
description
Parking-Lot Business
CARS (CAR)
pk
SPACES (SPE)
lic_plate
model
pk
fk, uk
25
id
description
car_lic_plate
Relationship Mapping
Enforcing One-to-Many
If the relationship is mandatory at both ends, you have the
same limitation in the database as a 1:M relationship that
is mandatory at the one end. Therefore, you would need
to write additional code to enforce it.
26
Relationship Mapping
Mapping Arcs
The entity that has the arc will map to a table that contains
foreign keys from the tables on the one end of the
relationships.
Note that even if the relationships in the arc are
mandatory on the many side, the resulting foreign keys
have to be optional (because one of them will always be
blank).
27
Relationship Mapping
held at
EVENT
# id
* name
* date
* cost
o description
EVENTS (EVT)
pk
28
id
name
date
cost
description
fk1
pse_id
fk2
phe_id
PRIVATE HOME
# id
* address
o comments
PUBLIC_SPACES (PSE)
pk
id
address
rental_fee
comments
id
address
comments
Relationship Mapping
29
Relationship Mapping
30
Relationship Mapping
address
rental fee
comments
900 Chestnut St
100.00
No elevator
10
78.00
Church
basement
address
15
4 Via Maria
20
31
id
name
date
cost
42
Jones
reception
05 June
500
48
Morales
party
08 July
750
Surprise 40th
50
Brennan
dinner
12 July
400
Catered
description
pse_id
phe_id
6
10
20
comments
Large projection TV
Relationship Mapping
32
Relationship Mapping
Terminology
Key terms used in this lesson included:
Cascade barred relationship
Intersection entity
Nontransferable relationship
33
Relationship Mapping
Summary
In this lesson, you should have learned how to:
Apply the rule of relationship mapping to correctly
transform 1:M and barred relationships
Apply the rule of relationship mapping to correctly
transform M:M relationships
Transform 1:1 relationships
Apply the rule of relationship mapping to correctly
transform relationships in an arc
34