10-Relational Data Modeling - D2L v3
10-Relational Data Modeling - D2L v3
Carlos A. Suárez-Núñez
[email protected]
Slides adaptados de: ISE 3024 – Virginia Tech - Prof. John P. Shewchuk
Copyright ©2013 Pearson Education, Inc. publishing as Prentice Hall.
1‐0
10 – 1
1
4/19/2017
10 – 2
2
4/19/2017
purchaseId totalCost
shelfId name
saleDateTime
row shelfNum state phone rating
date time
Shelf(shelfId,row,shelfNum)
No derived attributes
Purchase(purchaseId,saleDate,saleTime)
Supplier(name,state,rating) No multivalued attributes
10 – 5
3
4/19/2017
10 – 6
Customer(customerName,state)
Order(orderNo,customerName references Customer,
itemNo)
10 – 7
4
4/19/2017
O:W
Owner: Weak 1 1
Purchase Has Book
Weak Owner
saleDate bookId
10 – 8
W
Book Purchase
O bookId title ... bookId saleDate
(Owner (Weak entity
class) class)
primar foreign
y key
key
primary key
10 – 9
5
4/19/2017
condition Is
Copy
iii) Add ISBN as foreign key Of
iii) Key of UsedBook 1
Owner
UsedBook(ISBN references MediaType,
bookNum,condition,price)
10 – 10
MediaType UsedBook
W
O ISBN title ... ISBN bookNum condition price
(Weak
(Owner entity
class) class)
primar foreign
y key
key
primary key
10 – 11
6
4/19/2017
10 – 12
name
7
4/19/2017
10 – 14
e.g., Order:Purchase 1 1
Order Results Purchase
relationship type In
orderId purchaseId
If Purchase is donor:
i) Add purchaseId to Order as foreign key
ii) No relationship type attributes
Order Purchase
orderId orderdate purchaseId purchaseId saleDate saleTime
O1 xxx P1 P1 xxx xxx
O2 xxx P3 P2 xxx xxx
P3 xxx xxx
foreign key primary key
Order(orderId,orderDate)
becomes Order(orderId,orderDate,purchaseId
references Purchase)
10 – 15
8
4/19/2017
1 1
Order Results Purchase
In
orderId purchaseId
If Order is donor:
i) Add orderId to Purchase as foreign key
ii) No relationship type attributes
Order Purchase
orderId orderdate purchaseId saleDate saleTime orderId
O1 xxx P1 xxx xxx O1
O2 xxx P2 xxx xxx
P3 xxx xxx O2
primary key foreign key
Purchase(purchaseId,saleDate,saleTime)
becomes Purchase(purchaseId,saleDate,saleTime,
orderId references Order)
10 – 16
(the one that has mandatory participation)
(the one that has less options for nulls)
9
4/19/2017
10 – 18
10 – 19
10
4/19/2017
1
e.g., Customer:Purchase Customer
M
Makes Purchase
relationship type
i) Add key of Customer customerId purchaseId
so Purchase(purchaseId,saleDate,saleTime)
becomes Purchase(purchaseId,saleDate,saleTime,
customerId references Customer)
10 – 20
10 – 21
11
4/19/2017
ii) Add the key attributes of both classes to the schema (as
foreign key attributes).
10 – 22
M Has N
Purchase Copy MediaType
Of
purchaseId ISBN
quantity purchFormat
10 – 23
12
4/19/2017
primary key
MediaTypePurchase MediaType
purchaseId ISBN purchFormat quantity ISBN ... format
P1 1 paper a 1 ... paper
P1 2 audio c 2 ... audio
P2 1 paper f 3 ... both
P2 3 paper a
P3 3 paper c primar
P3 3 audio b y
key
10 – 24
10 – 25
13
4/19/2017
MediaType Book
ISBN ... format ISBN onHand price binding #ofPages
1 ... paper 1 xxx xxx xxx xxx
2 ... audio 3 xxx xxx xxx xxx
3 ... both foreign
primary key key
primary key
Book(ISBN references MediaType,onHand,
price,binding,#ofPages)
10 – 26
10 – 27
14
4/19/2017
orderId
ISBN
Placed Defines
For Is
CopyOf
purchaseId totalCost
1 1 M
1 M 1 M
Customer Makes Purchase Has UsedBook
customerId bookNum
10 – 28
Order(orderId,orderDate,purchaseId references
Purchase,customerId references Customer)
Customer(customerId,lastName,firstName)
Purchase(purchaseId,saleDate,customerId references
Customer)
MediaType(ISBN,title)
10 – 29
15
4/19/2017
Order
Order(orderId,orderDate,purchaseId
orderId references Purchase,customerId
orderDate references Customer)
1
purchaseId
customerId
M
10 – 30
10 – 31
16
4/19/2017
10 – 33
17
4/19/2017
Indexes ‐> support structures that speed the sorting
and searching of database data, created by a DBMS
10 – 34
Both primary PK FK
and foreign key
10 – 35
18
4/19/2017
foreign
key
1:M each purchase can have multiple usedbooks.
given purchaseId can be in UsedBook multiple times.
duplicate values allowed.
set Index (Dupl Values)? to N, or Y(Y), for foreign key.
19
4/19/2017
Book AudioBook
Order(orderId,orderDate)
10 – 38
customerId
Customer(customerId,lastName,firstName,street,
city,state,zip)
10 – 39
20
4/19/2017
Book AudioBook
Purchase(purchaseId,saleDate,saleTime)
10 – 40
Order Results
1 In
M
Placed
For
1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Is
Copy
Of
1 M
Has
Copy MediaType Available Supplier
Of N M From N
o
Book AudioBook
MediaType(ISBN,title,author,publisher,year,
format)
10 – 41
21
4/19/2017
shelfId
Order Results
Shelf M
1 In
Placed
For
1 1
row shelfNum 1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Is
name Copy
Of
1 M
Has
Copy MediaType Available Supplier
Of N M From N
Supplier o
Book AudioBook
Shelf(sheldIf,row,shelfNum)
Supplier(name,state,rating)
10 – 42
10 – 43
22
4/19/2017
Book AudioBook
10 – 44
Order(orderId,orderDate)
becomes
Order(orderId,orderDate,purchaseId references
Purchase)
10 – 45
23
4/19/2017
Order(orderId,orderDate,purchaseId references
Purchase)
becomes
Order(orderId,orderDate,purchaseId references
Purchase,customerId references Customer)
10 – 46
Order Results
1 In
M
Placed
For
1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Is
Copy
Of
M
Has
1 M Copy
N
MediaType Available Supplier
Customer Makes Purchase Of M From N
o
customerId Book
Purchase(purchaseId,saleDate,saleTime)
becomes
Purchase(purchaseId,saleDate,saleTime,
customerId references Customer)
10 – 47
24
4/19/2017
Order Results
1 In
M
Placed
For
1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Is
Copy
Of
1 M
Has
1 M Copy
N
MediaType Available Supplier
Purchase Has UsedBook Of M From N
o
10 – 48
Order Results
1 In
UsedBook M
Placed
For
M 1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
On
Is 1 M
M M
Copy Is
Copy
Of Of
1 M
Has
1 Copy
N
MediaType Available Supplier
M From N
Of
o
MediaType ISBN
Book AudioBook
10 – 49
25
4/19/2017
Order Results
1 In
MediaType M
Placed
For
M 1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Found Is
On Copy
Of
1 M
Has
1 Copy
N
MediaType Available Supplier
M From N
Of
o
Shelf shelfId
Book AudioBook
MediaType(ISBN,title,author,publisher,year,
format)
becomes
MediaType(ISBN,title,author,publisher,year,
format,shelfId references Shelf)
10 – 50
MediaType ISBN
10 – 51
26
4/19/2017
Order Results
1 In
M
Placed
For
1 1
1 M 1
purchFormat Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Is
Copy
Of
M N 1 M
Has
Copy MediaType Available Supplier
MediaType Available Supplier Of N M From N
From o
price
10 – 52
binding #ofPages
10 – 53
27
4/19/2017
Order Results
1 In
M
ISBN MediaType Placed
For
1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
o M
Is
M
Copy
Of
1 M
Has
Copy MediaType Available Supplier
Of N M From N
onHand
o
AudioBook
price
Book AudioBook
language runLength
10 – 54
Customer(customerId,lastName,firstName,street,
city,state,zip)
MediaType(ISBN,title,author,publisher,year,format,
shelfId references Shelf)
10 – 55
28
4/19/2017
Order(orderId,orderDate,purchaseId references
Purchase,customerId references Customer)
Purchase(purchaseId,saleDate,saleTime,customerId
references Customer)
Shelf(shelfId,row,shelfNum)
Supplier(name,state,rating)
10 – 56
No. Relational approach can only model 1:1, 1:M
M:N = two 1:M’s
10 – 57
29
4/19/2017
Purchase Supplier
Customer 1 UsedBook MediaType
1 1 name
1 1
purchaseId M 1
customerId ISBN ISBN 1
1 bookNum 1 1 title state
lastName 1 saleDate 1
rating
firstName saleTime condition author
street customerId price publisher
M Supplier
city purchaseId year Phone
state M format M
M name
zip shelfId
phone
10 – 58
Data dictionary:
Range Value Index Foreign Key
Attribute or PK FK Req’d (Dupl Referenced
Name Description Type Length Format Choices ? ? ? Values)? Table
AudioBook
ISBN Book ID FCHAR 10 x-xxxx-xxxx-x PK FK MediaType
onHand Qty. avail. INT 3 Y N
price Unit price CURR 5 $xx.xx Y N
language Language VCHAR 10 "English" Y Y (Y)
"French"
"Spanish"
runLength minutes INT 3 N N
Book
ISBN
Book ID FCHAR 10 x-xxxx-xxxx-x PK FK MediaType
onHand Qty. avail. INT 3 Y N
price Unit price CURR 5 $xx.xx Y N
binding Binding type VCHAR 9 "Paperback", Y N
"Hardback"
#ofPages Page count INT 3 N N
10 – 59
30
4/19/2017
10 – 60
MediaTypeSupplier
ISBN Book ID FCHAR 10 x-xxxx-xxxx-x PK FK MediaType
name Supplier name VCHAR 30 PK FK Supplier
purchFormat Book or audio VCHAR 5 "Paper","Audio" PK
price Unit price CURR 5 $xx.xxx N N
Order
orderId
Cust. order # VCHAR 4 PK
orderDate Order date DATE 8 dd/mm/yy Y N
purchaseId Customer PO INT 5 FK Y Y (N) Purchase
customerId Customer ID INT 3 FK Y N Customer
Purchase
purchaseId
Cust. purch. # INT 5 PK
saleDate Purchase date DATE 8 dd/mm/yy N N
saleTime Purchase time TIME 5 hh:nn N N
customerId Customer ID INT 3 FK Y Y (Y) Customer
10 – 61
31
4/19/2017
SupplierPhone
name Supplier name VCHAR 30 PK FK Supplier
phone Suppl. phone # FCHAR 14 (xxx) xxx-xxxx PK
UsedBook
ISBN BookID FCHAR 10 x-xxxx-xxxx-x PK FK MediaType
bookNum e.g., 1 = 1st INT 3 PK
condition Book condition VCHAR 9 "Good", Y N
"Very Good",
"Excellent",
"Like New"
price Unit price CURRENCY 5 $xx.xx Y N
purchaseId CustomerPO INT 5 FK N N Purchase
10 – 62
Shelf
shelfId row shelfNum
s1 1 1
s2 1 2
s3 2 1
s4 2 2
s5 3 1
s6 3 2
s7 4 1
s8 4 2
10 – 63
32
4/19/2017
MediaType
shelf
ISBN title author publisher year format Id
0-3454-6115-0 Star Wars: Jedi Trial Dan Cragg Random House 2005 Paper s4
0-7432-2454-X Band of Brothers Stephen Ambrose Simon & Schuster 2001 Both s1
1-5651-1748-4 A History of Ice Hockey Greg Proops Highbridge 2003 Audio s7
0-4402-1126-3 Circle of Friends Maeve Binchy Dell Publishing 2006 Paper s3
1-5948-3636-1 Message In ABottle Nicholas Sparks Time Warner 1999 Both s3
0-6848-7385-0 Beyond Valor Patrick O’Donnell Simon & Schuster 2001 Paper s1
0-8072-1873-1 Cat In The Hat Dr. Seuss Listening Library 2003 Audio s6
0-3951-5023-X Curious George Margaret & H.A. Rey Simon & Schuster 1973 Paper
Book AudioBook
on #of on run
ISBN Hand price binding Pages ISBN Hand price language Length
0-3454-6115-0 15 $6.99 Paperback 155 0-7432-2454-X 2 $32.00 English 300
0-7432-2454-X 6 $17.82 Hardback 336 1-5948-3636-1 4 $19.98 English 180
0-4402-1126-3 9 $7.99 Paperback 596 1-5651-1748-4 3 $22.95 French 90
1-5948-3636-1 24 $7.50 Paperback 0-8072-1873-1 5 $19.95 English
0-6848-7385-0 2 $8.99 Paperback 384
0-3951-5023-X 0 $14.00 Hardback 32
10 – 64
UsedBook
ISBN bookNum condition price purchaseId
0-4402-1126-3 137 Very Good $5.99 1203
0-4402-1126-3 175 Good $3.00
0-3951-5023-X 46 Good $7.99 1273
0-7432-2454-X 76 Very Good $12.00 1286
1-5948-3636-1 121 Excellent $6.50 1286
0-7432-2454-X 94 Good $4.00
Order
order order purchase customer
Id Date Id Id
MediaTypePurchase O336 5/01/10 1276 17
purchase purch O346 8/05/10 1503 13
Id ISBN Format quantity
Purchase
1264 0-7432-2454-X Paper 1 purchase sale sale customer
1273 0-4402-1126-3 Paper 1 Id Date Time Id
1273 0-8072-1873-1 Audio 1
1203 2/03/10 10:15 6
1276 0-3454-6115-0 Paper 2
1264 2/12/10 15:20 3
1286 0-6848-7385-0 Paper 2
1273 4/30/10 10:20 2
1304 1-5948-3636-1 Audio 1
1276 17
1304 0-3951-5023-X Paper 2
1503 1-5651-1748-4 Audio 1 1286 5/06/10 14:32 13
1304 7/23/10 11:15 2
1503 13
10 – 65
33
4/19/2017
MediaTypeSupplier
ISBN name purchFormat price
0-4402-1126-3 Thomson International Paper $4.99
1-5948-3636-1 ABC Books Audio $15.00
1-5948-3636-1 ABC Books Paper $5.00
1-5948-3636-1 Thomson International Paper $5.79
0-8072-1873-1 Thomson International Audio $14.99
0-8072-1873-1 Book World Audio $15.98
0-3951-5023-X ABC Books Paper $10.99
1-5651-1748-4 Book World Audio $17.95
Supplier SupplierPhone
name state rating name phone
Thomson International CA A Thomson International 216-175-5065
ABC Books IN A ABC Books 216-831-9175
Book World VA C ABC Books 704-668-2203
Book World 331-791-4982
Book World 501-773-7381
10 – 66
34