0% found this document useful (0 votes)
9 views18 pages

Chapter 2 - C_ER_model

sss

Uploaded by

YouTubeATP
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)
9 views18 pages

Chapter 2 - C_ER_model

sss

Uploaded by

YouTubeATP
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/ 18

Chapter 2C

Foreign Key
COMP3278 Introduction to
Database Management Systems

Department of Computer Science, The University of Hong Kong


Slides prepared by - Dr. Chui Chun Kit for students in COMP3278
For other uses, please email : ckchui@cs.hku.hk
Example 1

2
Example 1

Author (authorID, name, date of birth)


To transform an ER model to
Book (bookID, title, publisher) relational tables…
Step 1. Entity set -> table
Each entity set becomes a table.
Each attribute becomes a column.
Each entity is a tuple in the table.

3
Example 1

Author (authorID, name, date of birth)

Book (bookID, title, publisher) Step 2. Relationship set


Whether a relationship set
becomes a table or not depends
Writes ( ) on the mapping cardinality of the
relationship.
(many to many) , a table.
4
Example 1

Author (authorID, name, date of birth) Step 3. Identify the key


What is the primary key of
each table? Any foreign keys?
Book (bookID, title, publisher)
bookID is a Foreign key , this key is
referencing the column bookID in the Book
Writes (authorID, bookID) table.
authorID is another Foreign key , this key is
referencing the column authorID in the
Author table 5
Foreign key
A foreign key is a referential constraint between
two tables.
A foreign key is a field in a relational table that matches
a candidate key of another table.

The foreign key can be used to cross-reference tables.


It is used to link information together.

An essential part of database normalization (To be


discussed in Chapter 5).
6
Example 1

Author (authorID, name, date of birth)


Foreign key: none

Book (bookID, title, publisher)


Foreign key: none
Writes (authorID, bookID)
Foreign keys: {authorID} referencing Author
{bookID} referencing Book
7
Example 2

8
Example 2

To transform an ER model to
relational tables…
Step 1. Entity set -> table
Course (course code, name) Each entity set becomes a table.
Each attribute becomes a column.
Each entity is a tuple in the table.

Tutorial (tutorial number, date, time, venue)

9
Example 2

Step 2. Relationship set


Whether a relationship set becomes a
table or not depends on the mapping
cardinality of the relationship.
Course (course code, name) (one to many or many to one),
attributes go to “many” side.

Tutorial (tutorial number, date, time, venue, course_code)

10
Example 2

Step 3. Identify the key


What is the primary key of
Course (course code, name) each table? Any foreign keys?

Foreign key: none

Tutorial (tutorial number, date, time, venue, course_code)


Foreign key: {course code} referencing Course

11
Example 3

12
Example 3
Option 1
Customer (customerID, name)
Foreign key: none
VIP (customerID, discount_rate)
Foreign key: {customerID} referencing Customer
Member (customerID, accumulated_points)
Foreign key: {customerID} referencing Customer

Handling ISA relationship


Option 1 :
Form a table for higher-level entity set.
Form a table for each lower-level entity set,
which contains the primary key of the higher-
level entity set and local attributes.

13
Example 3
Option 1
Customer (customerID, name)
Foreign key: none
VIP (customerID, discount_rate)
Foreign key: {customerID} referencing Customer
Member (customerID, accumulated_points)
Foreign key: {customerID} referencing Customer

Option 2
Customer (customerID, name)
Foreign key: none
Handling ISA relationship VIP (customerID, name, discount_rate)
Option 2 : Foreign key: {customerID} referencing Customer
Member (customerID, name, accumulated_points)
Form a table for each entity
Foreign key: {customerID} referencing Customer
set with all local and inherited
attributes 14
Example 3
Option 1
[Storage] Option 1 has less Customer (customerID, name)
storage redundancy. Foreign key: none
[Efficiency] Accessing data (e.g, VIP (customerID, discount_rate)
retrieving the name and Foreign key: {customerID} referencing Customer
discount_rate of a VIP) in option Member (customerID, accumulated_points)
1 requires accessing two tables Foreign key: {customerID} referencing Customer
(not as efficient as option 2,
which requires accessing one
table only)! Option 2
Customer (customerID, name)
Foreign key: none
VIP (customerID, name, discount_rate)
Foreign key: {customerID} referencing Customer
What are the advantage
Member (customerID, name, accumulated_points)
and disadvantage of these
Foreign key: {customerID} referencing Customer
options?
15
Example 3
Option 1
Customer (customerID, name)
Foreign key: none
Total VIP (customerID, discount_rate)
specialization Foreign key: {customerID} referencing Customer
Member (customerID, accumulated_points)
Foreign key: {customerID} referencing Customer

Option 2
Customer (customerID, name)
Foreign key: none
Option 3 VIP (customerID, name, discount_rate)
VIP (customerID, name, discount rate) Foreign key: {customerID} referencing Customer
Foreign key: none Member (customerID, name, accumulated_points)
Member (customerID, name, Foreign key: {customerID} referencing Customer
accumulated points)
Foreign key: none
16
Example 4

Handling Multivalue
attributes
Multivalue attribute
becomes a table.
Customer (customerID, name)
Foreign key: none

CustomerPhone (customerID, phone)


Foreign key: {customerID} referencing Customer

17
Chapter 2C

END
COMP3278 Introduction to
Database Management Systems

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : ckchui@cs.hku.hk

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy