0% found this document useful (0 votes)
117 views34 pages

10-Relational Data Modeling - D2L v3

d2l

Uploaded by

Cristhian Javier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views34 pages

10-Relational Data Modeling - D2L v3

d2l

Uploaded by

Cristhian Javier
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

4/19/2017

Optativa 4to (IIN400)

Modelado de Datos Relacionales

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. RELATIONAL DATA MODELING


 Previous database chapters: how to construct conceptual
schema, articulate via tables or E-R diagrams.
 Next step: determine how conceptual schema can be
translated into computer-compatible form.
 This is called the relational schema
= computer's view of data.

 Let’s continue using Anna’s Books for our study of


relational data modeling.
 We’ll then develop relational schema for both Anna’s
Books and JA Machining.

10 – 1

1
4/19/2017

10.1 Introduction to the Relational Model

 Relational data model = data description via set of 2D tables.


(relation = rectangular table of data)

 Relational schema = relational model applied to a given


problem.
 Example: relational schema, partial relation for Customer:
Customer(customerId,lastName,firstName,street,
city,state,zip)
customer last first street city state zip
Id Name Name
3 Smith John 214 Church St. Blacksburg VA 24073
17 Davis Diane 185 Clay St. Blacksburg VA 24073

10 – 2

 How does the table correspond to E-R model?


• column headings  attributes
• data in columns  attribute values
• rows (records)  entities instance or entities

 Attributes in a relation schema must be atomic 


composite and multivalued attributes must somehow be
translated into equivalent single-valued attributes.

 Every relation schema requires at least one key for the


relation (denote via underscore).
– key attribute value(s) must be unique for each row
(entity) – Chapter 8.
– If true, relation exhibits what? Entity Integrity
– if true, relation exhibits what? 10 – 3

2
4/19/2017

10.2 Developing Relational Schema

Step 1: Strong Entity Classes


 For each strong entity class (including superclasses):
i) Define a relation schema having the same name.
ii) For each simple (i.e., single-valued) attribute, create an
attribute and add to the schema.
iii) For each composite (i.e., multi-valued) attribute,
create an attribute for each component and add to the
schema.
iv) Define the key of the schema as the primary key of
the entity class.
10 – 4

 e.g., Shelf, Purchase, and Supplier entity classes

purchaseId totalCost
shelfId name

Shelf Purchase Supplier

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

Step 2: Weak Entity Classes

 For each weak entity class:

i) Define a relation schema having the same name.

ii) For each simple or component attribute, create an


attribute and add to the schema.

iii) Add the key attributes of the owner class to the


schema (as foreign key attributes).

iv) Define the key of the schema as the combination of


the foreign key attributes and any discriminator
attributes found in the weak entity class.

10 – 6

 foreign key = set of attributes in one relation which are


identical to one or more key attributes in another relation.
e.g.,
Customer Order
customerName state orderNo customerName itemNo
John Smith VA 4104 Tim Richards 5779A
Alice Jones WV 2336 Julie Barnes 12GB-05
Tim Richards PA 2875 Tim Richards 7084A-12
Julie Barnes VA
common field Foreign key
Primary key

Customer(customerName,state)
Order(orderNo,customerName references Customer,
itemNo)
10 – 7

4
4/19/2017

 If O:W is 1:1, key of relation schema = key of owner class.


e.g., Purchase weak entity class (book = physical item):

O:W
Owner: Weak 1 1
Purchase Has Book

Weak Owner
saleDate bookId

i) Relation schema = Purchase


ii) Add saleDate
iii) Add bookId as foreign key
iv) Key of Purchase = bookId
 Purchase(bookId references Book,saleDate)

10 – 8

W
Book Purchase
O bookId title ... bookId saleDate
(Owner (Weak entity
class) class)

primar foreign
y key
key
primary key

 Purchase(bookId references Book,saleDate)

10 – 9

5
4/19/2017

 If O:W is 1:M, key of relation schema = key of owner class


+ discriminator.
e.g., UsedBook weak entity class:
Weak
i) Relation schema
bookNum

= UsedBook UsedBook condition

ii) Add bookNum,price, M price

condition Is
Copy
iii) Add ISBN as foreign key Of
iii) Key of UsedBook 1

= ISBN + bookNum MediaType ISBN

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

 UsedBook(ISBN references MediaType,


bookNum,condition,price)

10 – 11

6
4/19/2017

Step 3: Multivalued Attributes

 For each multivalued attribute M in an entity class:

i) Define an appropriately-named relation schema.

ii) Add M (or it’s components) to the schema.

iii) Add the key attributes of the entity class to the


schema (as foreign key attributes).

iv) Define the key of the schema as the combination of


all it’s attributes.

10 – 12

name

 e.g., Supplier entity class


i) Relation schema = SupplierPhone Supplier
ii) Add phone
iii) Add name as foreign key state phone rating

iv) Key = {name, phone}


Supplier SupplierPhone
name rating name phone
S1 xxx S1 ph1 (new table)
S2 xxx S2 ph3
S3 xxx S2 ph5
S4 xxx S2 ph8
S3 ph2
primar S4 ph9
y foreign S4 ph7
key key
primary key
 SupplierPhone(name references Supplier,phone)
10 – 13

7
4/19/2017

Step 4: 1:1 Relationship Types

 For each 1:1 relationship type:

i) Select one of the classes to be the donor and the


other the recipient. Try and minimize nulls (if
applicable).

ii) Add the key attributes of the donor to the recipient


(as foreign key attributes).

iii) Add any attributes associated with the relationship


type to the recipient.

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

 So which should we choose?

(the one that has mandatory participation)

 What if both classes have mandatory participation?


• Nulls will never occur.
• So choose either class as recipient.

 What if neither class has mandatory participation?

(the one that has less options for nulls)

 Even if we cannot eliminate nulls it is important to know


whether or not they are allowed.
• we will see this when we implement using MS Access.
10 – 17

9
4/19/2017

 Another popular approach: combine the two tables into


one, e.g.,
other attributes
from second table
CombinedTable 
purchaseId saleDate saleTime orderId orderDate ...
P1 xxx xxx O1 xxx
P2 xxx xxx
P3 xxx xxx O2 xxx

 While many people think this is a good idea as it reduces


the number of tables, it is not. Problems:
‐ Null values!
‐ Data duplication, not efficient

10 – 18

Step 5: 1:M Relationship Types

 For each 1:M relationship type between classes A and B:

i) Add key attributes of A to B (as foreign key


attributes). Name the attributes appropriately.

ii) Add any attributes associated with the relationship


type to B.

10 – 19

10
4/19/2017

1
e.g., Customer:Purchase Customer
M
 Makes Purchase
relationship type
i) Add key of Customer customerId purchaseId

to Purchase (as foreign key)


ii) No relationship type attributes
Customer Purchase
customerId lastName ... zip purchaseId ... saleTime customerId
C1 xxx xxx xxx P1 xxx xxx C2
C2 xxx xxx xxx P2 xxx xxx C2
C3 xxx xxx xxx P3 xxx xx C3
primary key foreign key

so Purchase(purchaseId,saleDate,saleTime)
becomes Purchase(purchaseId,saleDate,saleTime,
customerId references Customer)
10 – 20

 Note that the foreign key (customerId) in the


Purchase relation can never have null values here, as
participation of Purchase class is mandatory.

 If participation was optional, however, nulls could indeed


occur.

10 – 21

11
4/19/2017

Step 6: M:N Relationship Types

 For each M:N relationship type between two entity classes:

i) Define an appropriately-named relation schema.

ii) Add the key attributes of both classes to the schema (as
foreign key attributes).

iii) Add any attributes associated with the relationship


type to the schema.

iv) Define the key of the schema as the combination of all


foreign key attributes.

10 – 22

 e.g., Purchase:MediaType relationship type:

M Has N
Purchase Copy MediaType
Of
purchaseId ISBN
quantity purchFormat

i) Relation schema = MediaTypePurchase


ii) Add purchaseId and ISBN as foreign keys
iii) Add purchFormat, quantity (purchFormat is a subtype 
descriminator attribute)
iv) key of MediaTypePurchase
= {purchaseId, ISBN, purchFormat}
 MediaTypePurchase(purchaseId references Purchase,
ISBN references MediaType,purchFormat,quantity)

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

purchaseId saleDate saleTime customerId


Purchase
P1 xxx xxx C2
P2 xxx xxx C2
P3 xxx xxx C3
primary key
 MediaTypePurchase(purchaseId references Purchase,
ISBN references MediaType,purchFormat,quantity)

10 – 24

Step 7: Subclasses (Inheritance)

 For each subclass:

i) Define a relation schema.

ii) For each simple or component attribute, create an


attribute and add to the schema.

iii) Add the key attributes of the superclass to the schema


(as foreign key attributes).

iv) Define the key of the schema as the foreign key


attributes.

10 – 25

13
4/19/2017

e.g., Book subclass


ISBN MediaType

i) Relation schema = Book format


ii) Add onHand, price, "paper"
o
binding, #ofPages
iii) Add ISBN as foreign key onHand binding

iv) Key of Book = ISBN price


Book
#ofPages

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.3 Relational Schema Diagrams and Data


Dictionaries

 Up to now: textual approach for specifying relational


schema.

 As with our conceptual schema, where we used E-R


diagrams, we can also employ a graphical approach to
specify a relational schema. This results in a relational
schema diagram.

 To simplify this discussion, let’s consider only a subset of


our conceptual schema for Anna’s Books. This is shown
on the following page:

10 – 27

14
4/19/2017

orderId
ISBN

Order Results MediaType


1 In title
orderDate
M 1

Placed Defines
For Is
CopyOf
purchaseId totalCost
1 1 M
1 M 1 M
Customer Makes Purchase Has UsedBook

lastName firstName saleDate condition price

customerId bookNum

10 – 28

 Corresponding relational schema:

Order(orderId,orderDate,purchaseId references
Purchase,customerId references Customer)

Customer(customerId,lastName,firstName)

Purchase(purchaseId,saleDate,customerId references
Customer)

UsedBook(ISBN references MediaType,bookNum,condition,


price,purchaseId references Purchase)

MediaType(ISBN,title)

10 – 29

15
4/19/2017

 Expressed as a relational schema diagram:

Order
Order(orderId,orderDate,purchaseId
orderId references Purchase,customerId
orderDate references Customer)
1
purchaseId
customerId
M

Customer Purchase UsedBook MediaType


1 1 1 1
customerId purchaseId ISBN M ISBN
lastName 1 saleDate bookNum title
firstName customerId condition
M
price
M purchaseId

10 – 30

 Whether textually or graphically, however, still missing


certain information for each attribute:
• data type, length, and format
• domain
• whether nulls allowed
• whether duplicated values allowed

 Data Dictionary = single document which specifies


relational schema plus all additional information.

 A data dictionary for the current example is shown on


the following page.

10 – 31

16
4/19/2017

Range Value Index Foreign Key


Attribute or PK FK Req’d (Dupl Referenced
Name Description Type Length Format Choices ? ? ? Values)? Table
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
Customer
customerId Customer ID INT 3 PK
lastName Last name VCHAR 30 Y Y (Y)
firstName First name VCHAR 30 Y N
Purchase
purchaseId Cust. purch. # INT 5 PK
saleDate Purchase date DATE 8 dd/mm/yy N N
customerId Customer ID INT 3 FK Y Y (Y) Customer
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", "Very Good", Y N
"Excellent", "Like New"
price Unit price CURRENCY 5 $xx.xx Y N
purchaseId CustomerPO INT 5 FK N N Purchase
MediaType
ISBN Book ID FCHAR 10 x-xxxx-xxxx-x PK
title Book title VCHAR 80 Y N
10 – 32

Range Value Index Foreign Key


Attri bute or PK FK Req’d (Dupl Referenced
Name Description Type Length Format Choices ? ? ? Values)? Table

 Creating data dictionary:

Attribute Name, Description, Type, Length, Format,


Range or Choices – define attributes as per Chapter 8.

PK? – enter PK if the attribute is a primary key.

FK? – enter FK if the attribute is a foreign key.

Value Req’d? – enter Y if a value is required for the attribute


(i.e., nulls not allowed); N otherwise.

10 – 33

17
4/19/2017

Range Value Index Foreign Key


Attri bute or PK FK Req’d (Dupl Referenced
Name Description Type Length Format Choices ? ? ? Values)? Table

Index (Dupl Values) – an index allows you to search or


sort by the values. Select one of the following:
N do not index.
Y (N) index, no duplicate values allowed.
Y (Y) index, duplicate values allowed.

Indexes ‐> support structures that speed the sorting 
and searching of database data, created by a DBMS

Foreign Key Referenced Table – if attribute is a foreign key,


enter name of table where corresponding primary key found.

10 – 34

 Values for columns 7-10 in the data dictionary are related


to one another and can be set as follows:
Attribute PK FK Value Index
Type ? ? Req’d? (Dupl Values)?
Primary key PK

Both primary PK FK
and foreign key

Foreign key FK N (optional rel.) 1:1  Y (N)


only Y (mandatory rel.) 1:M  N or Y (Y)
Non-key N or Y (preference) N, Y (N), or Y(Y)

 Examples for foreign keys: next two pages.

10 – 35

18
4/19/2017

i) Purchase:Order (1:1, participation of Order mandatory)


Purchase Order
purchaseId ... customerId orderId ... purchaseId customerId
P1 xxx C2 O1 xxx P1 C2
P2 xxx C2 O2 xxx P3 C3
P3 xxx C3
foreign key

1:1  each purchase can have only one order.


 given purchaseId can appear in Order table once.
 no duplicate values allowed.
 set Index (Dupl Values)? to Y(N) for foreign key.

Order mandatory  each order must have a purchase #.


 purchaseId value required in
each row of Order table.
 set Values Req’d to Y for foreign key.
10 – 36

ii) Purchase:UsedBook (1:M, partic. of UsedBook optional)


Purchase UsedBook
purchaseId ... customerId ISBN bookNum ... purchaseId
P1 xxx C2 xxx xxx xxx P3
P2 xxx C2 xxx xxx xxx
P3 xxx C3 xxx xxx xxx P3

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.

UsedBook optional  each used book does not need purch #.


 purchaseId value not required each
row of UsedBook.
 set Values Req’d to N for foreign key.
10 – 37

19
4/19/2017

10.4 Example: Relational Schema for Anna’s Books


Step 1: Strong Entity Classes
Order Results
1 In
M
Placed
For
1
Order 1
1 M
Found
1
Customer Makes Purchase Has UsedBook Shelf
1 M On
M M
Is
Copy
orderId orderDate Of
1 M
Has
Copy MediaType Available Supplier
Of
N M From N
o

Book AudioBook

Order(orderId,orderDate)

10 – 38

customerId

lastName firstName Order Results


1 In
M
Placed
For
1
Customer 1
1 M
Found
1
Customer Makes Purchase Has UsedBook Shelf
1 M On
M M
Is
address Copy
Of
1 M
Has
Copy MediaType Available Supplier
Of N M From N
street zipcode o

city state Book AudioBook

Customer(customerId,lastName,firstName,street,
city,state,zip)

10 – 39

20
4/19/2017

purchaseId totalCost Order Results


1 In
M
Placed
For
Purchase 1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Is
saleDateTime Copy
Of
1 M
Has
Copy MediaType Available Supplier
date time Of N M From N
o

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

state phone rating

Shelf(sheldIf,row,shelfNum)

Supplier(name,state,rating)

10 – 42

Step 2: Weak Entity Classes

bookNum Order Results


1 In
M

UsedBook condition Placed


For
1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
price On
M 1 M
M M
Is
Is Copy
Of
Copy 1 M
Has
Of Copy
N
MediaType Available Supplier
Of M From N
1 o

MediaType ISBN Book AudioBook

UsedBook(ISBN references MediaType,bookNum,


condition,price)

10 – 43

22
4/19/2017

Step 3: Multivalued Attribute


Order Results
1 In
M
Placed
For
1
Supplier 1
1 M
Found
1
Customer Makes Purchase Has UsedBook Shelf
1 M On
M M
Is
Copy
name rating phone Of
1 M
Has
Copy MediaType Available Supplier
Of N M From N
o

Book AudioBook

SupplierPhone(name references Supplier,phone)

10 – 44

Step 4: 1:1 Relationship Types


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 1 Copy
N
MediaType Available Supplier
Order Results Purchase Of M From N
In o

orderId purchaseId Book AudioBook

Order(orderId,orderDate)
becomes
Order(orderId,orderDate,purchaseId references
Purchase)

10 – 45

23
4/19/2017

Step 5: 1:M Relationship Types


Order Results
1 In
Order M
Placed
For
M 1 1
1 M 1
Customer Makes Purchase Has UsedBook Found Shelf
1 M On
M M
Is
Placed Copy
Of
For 1 M
Has
Copy MediaType Available Supplier
Of N M From N
1 o

Customer customerId Book AudioBook

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

purchaseId Book AudioBook

UsedBook(ISBN references MediaType,bookNum,


condition,price)
becomes
UsedBook(ISBN references MediaType,bookNum,
condition,price,purchaseId references Purchase)

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

UsedBook(ISBN references MediaType,bookNum,


condition,price,purchaseId references Purchase)
remains unchanged (why?)
Already added ISBN to UsedBook when creating UsedBook table (weak entity class)

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

Step 6: M:N Relationship Types


Order Results
purchaseId 1 In
M
Placed
For
1 1
Purchase Customer Makes Purchase
1
Has
M
UsedBook Found
1
Shelf
1 M On
M M
Is
M Copy
Of
purchFormat 1 M
Has Has
Copy Copy MediaType Available Supplier
Of N M From N
Of quantity o
N
Book AudioBook

MediaType ISBN

MediaTypePurchase(purchaseId references Purchase,


ISBN references MediaType,purchFormat,quantity)

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

ISBN name Book AudioBook

price

MediaTypeSupplier(ISBN references MediaType name


references Supplier,purchFormat,price)

10 – 52

Step 7: Subclasses (Inheritance)


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
Book
price
Book AudioBook

binding #ofPages

Book(ISBN references MediaType,onHand,price,


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

AudioBook(ISBN references MediaType,onHand,price


language,runLength)

10 – 54

 Final relational schema:


AudioBook(ISBN references MediaType,onHand,price,
language,runLength)

Book(ISBN references MediaType,onHand,price,


binding,#ofPages)

Customer(customerId,lastName,firstName,street,
city,state,zip)

MediaType(ISBN,title,author,publisher,year,format,
shelfId references Shelf)

MediaTypePurchase(purchaseId references Purchase,


ISBN references MediaType,purchFormat,quantity)

10 – 55

28
4/19/2017

MediaTypeSupplier(ISBN references MediaType,name


references Supplier,purchFormat,price)

Order(orderId,orderDate,purchaseId references
Purchase,customerId references Customer)

Purchase(purchaseId,saleDate,saleTime,customerId
references Customer)

Shelf(shelfId,row,shelfNum)

Supplier(name,state,rating)

SupplierPhone(name references Supplier,phone)

UsedBook(ISBN references MediaType,bookNum,


condition,price,purchaseId references Purchase)

10 – 56

 Relational schema diagram: next page.

 We have two M:N relationship types in our conceptual


schema, but do not see these in the diagram. Did we
make a mistake?

No. Relational approach can only model 1:1, 1:M

M:N = two 1:M’s

10 – 57

29
4/19/2017

Order MediaType MediaType


orderId Purchase Supplier
M purchaseId M ISBN
orderDate M
purchaseId 1 ISBN name
purchFormat M purchFormat
customerId
M quantity price

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

Book Shelf AudioBook


ISBN ISBN
1 1 shelfId 1
onHand row onHand
price shelfNum price
binding language
#ofPages runLength

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

Range Value Index Foreign Key


Attribute or PK FK Req’d (Dupl Referenced
Name Description Type Length Format Choices ? ? ? Values)? Table
Customer
customerId Customer ID INT 3 PK
lastName Last name VCHAR 30 Y Y (Y)
firstName First name VCHAR 30 Y N
street Street VCHAR 36 Y N
city City VCHAR 36 N Y (Y)
state State FCHAR 2 "AL",...,"WY" N Y (Y)
zip Zipcode FCHAR 5 N N
MediaType
ISBN
Book ID FCHAR 10 x-xxxx-xxxx-x PK
title Book title VCHAR 80 Y N
author Book author VCHAR 20 Y Y (Y)
publisher Book publisher VCHAR 20 Y N
year Publ. year INT 4 Y N
format Item type VCHAR 5 "Paper", Y Y (Y)
"Audio",
"Both"
shelfId Shelf identifier VCHAR 3 FK N N Shelf

10 – 60

Range Value Index Foreign Key


Attribute or PK FK Req’d (Dupl Referenced
Name Description Type Length Format Choices ? ? ? Values)? Table
MediaTypePurchase
purchaseId Cust. purch. # INT 5 PK FK Purchase
ISBN Book ID FCHAR 10 x-xxxx-xxxx-x PK FK MediaType
purchFormat Book or audio VCHAR 5 "Paper","Audio" PK
quantity Qty. ordered INT 3 Y N

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

Range Value Index Foreign Key


Attribute or PK FK Req’d (Dupl Referenced
Name Description Type Length Format Choices ? ? ? Values)? Table
Shelf
shelfId Shelf identifier VCHAR 3 PK
row Row shelf in INT 1 Y Y (Y)
shelfNum e.g., 1 = 1st INT 1 Y Y (Y)
Supplier
name Supplier name VCHAR 30 PK
state Supplier state FCHAR 2 "AL",...,"WY" N N
rating ABC rating FCHAR 1 "A","B","C" N Y (Y)

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

 Example of populated tables:


Customer
customerId lastName firstName street city state zip
3 Smith John 214 Church St. Blacksburg VA 24073
17 Davis Diane 185 Clay St. Blacksburg VA 24073
6 O’Keefe Patrick 35 Cambria St. Christiansburg VA 24061
7 Smith John 24 Range Road Radford VA 24141
2 Morgan Leslie 240 Kent St., Apt. 27 Christiansburg VA 24061
13 Carter Brian 14 2nd Street Lafayette IN 47906

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

You might also like