Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model...

33
Database Principles ER to RDM Mapping

Transcript of Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model...

Page 1: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

ER to RDM Mapping

Page 2: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Mapping from ER to Relational Data Model

Straight-forwardMapping

Straight-forwardMapping

database

Real World

Chen’s Proposal

Relational Data Model

E1

E3

E2

the next phase

Exercise: Give me some suggestions on what this mapping would look like.

Page 3: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Relational Data Model

• Models all information stored in a database as a collection of related mathematical structures called relations.

• Suppose

– Students = {s1, s2, s3, …, sn} and

– Courses = {c1, c2, c3, …, cm}

• The Cartesian Product of these two sets is:– Students x Courses =

{ (si, cj) | si ε Students, cj ε Courses }

remember this word

Page 4: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Student x Course Grid:

. . . . ..

.

.

.

.

. . . . .

. . . . .

. . . . .

. . . . .

...

...

Students

Cou

rses

s1 s2 s3 sn

cm

c3

c2

c1

(s1,c1)

(s2,c3)

(sn,cm)

Page 5: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Relational Data Model

• A relation is a subset of a Cartesian Product.

• Enrollment = {(s1, c2), (s2, c3), (s2, c2) }

• Enrollment is a subset of Students x Courses

Exercise: Suppose Student is a set of patients and Professor is a set of doctors.Give several examples of relations that are subsets of Student x Professor.

Exercise: Go through the exercise of asking a student you do not know for examples of useful relations and identity their corresponding Cartesian Products.

Page 6: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Enrollment

. . . . ..

.

.

.

.

. . . . .

. . . . .

. . . . .

. . . . .

...

...

Students

Cou

rses

s1 s2 s3 sn

cm

c3

c2

c1

(s2, c3)

(s1, c2) (s2, c2)

NOTE: Points inside theellipse correspond to real-world information. Points outside the ellipse do notcorrespond to any real-world event and we callthem noise.

Enrollment

noisepoints

Page 7: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Exercise:

• A tournament of local soccer teams is run as a double round-robin tournament. What does this mean?

• Describe the difference between such a tournament and one we could describe as a cartesian product tournament.

• Work in pairs. If you know what double round-robin means find someone who doesn’t and explain it to them. Then answer the question.

• Draw a simple ER for thisexample

Team

Name

plays(1,n)

(1,n)

home

awayhome_scoreaway_score

Page 8: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Relational Data Model

• A table containing a list of all suppliers - an ID, name and address for each – is called Supplier.

• This table is a subset of the Cartesian Product

so mathematically it too is a relation. • Of course, the only rows in the table are the useful tuples

of the Cartesian Product, those that really correspond to some supplier.

ID SName SAddress

Supplier

SupplierIDs x SupplierNames x SupplierAddresses

Page 9: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Exercise:

• Consider the previous round-robin tournament.

• What happens if we have an extra line in the Plays table?• This line is noise!! It makes the entire table useless. Why?

Team

Name

plays(1,n)

(1,n)

home

awayhome_scoreaway_score

HomeName AwayName HomeScore AwayScore

A B 2 1B A 3 3A C 0 1C A 2 0B C 1 1C B 1 1

Plays

A C 2 1noise

Rule to Follow: Database tables should contain no noise.

Page 10: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Table Parts

• A table has two parts – intension and extension• Intension: what the table is “intended” to model. In

essence, the table name and the name of each of its columns; otherwise known as the table schema.

• Extension: A list of all possible rows of the table; both presently existing and what might exist in the future or has existed in the past.

ID SName SAddress

SupplierTable Schema

extension

Page 11: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Notational Correspondence:

ER• Entity• Relationship• Attribute• Key Attributes• Instance

RDM• Table/Relation Schema• Table/Relation Schema• Column/Attribute• Primary Key• Row/Tuple

Exercise: The title of this lecture is “ER-to-RDM Mapping“. The above notational correspondence leads one to think that this mapping is quitestraight forward.

What is the one thing that is not straight forward about this implied mapping?

Page 12: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

ER-to-RDM Mapping, Rule 1:

• Each entity is mapped to a table schema with the same columns as the entity has attributes.

CARDHOLDER

borrowerid

b_name

b_addr

b_status

loan_limit

borrowerid b_name b_addr b_status loan_limit

CARDHOLDER

pk

maps to

Page 13: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Rule 1 (cont)

• The actual mapping is to syntax in SQL which can be used to create the table described by our schematic.

Create table CARDHOLDER ( borrowerid int not null primary key, b_name varchar(10), b_addr varchar(10), b_status char(6), loan_limit int )

borrowerid b_name b_addr b_status loan_limit

CARDHOLDER

pk

its an integercan never be nullis the primary key of the table

Page 14: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

ER-to-RDM Mapping, Rule 2a:

• If a relationship has no (1,1) participation number pair then it too is mapped to a table schema. The table schema consists of:– The key attributes of all entities participating in the

relationship– All attributes of the relationship.

• The primary key of the new table consists of all the key attributes of the entities participating in the relationship and any relationship attribute identifies as part of the key.

Page 15: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

ER-to-RDM Mapping, Rule 2a:

Book

ISBNAuthorTitlePub_namePub_dateC_price

Borrower

BorroweridB_nameB_addrB_statusLoan_limit

Reserves

r_date

(0,n) (0,n)

borrowerid ISBN r_datepk

reserves

maps to

Page 16: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

ER-to-RDM Mapping, Rule 2b:

• If a relationship has a (1,1) participation number pair then it is not mapped to a table schema.

• Instead, the table schema corresponding to the entity with the (1,1) pair takes on additional columns:– The keys of all other entities participating in the

relationship are migrated to the entity table schema– The attributes of the relationship itself are migrated to

the entity able schema.• The primary key of the entity table schema does not

change.

Page 17: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

ER-to-RDM Mapping, Rule 2b:

Book

ISBNAuthorTitlePub_dateC_price

Publisher

Pub_name

pub_date

(1,n)(1,1)

ISBN Author Title Pub_date C_price Pub_name Pub_date

pk

maps to

stays the same

BOOK

Page 18: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Review of Mapping Complexity:

• Relationships model real-world associations. • In a database, these associations can be modeled in one

of two ways:– By a table of their own; the table key is a combination

of entity keys– Within one of the participating entity tables.

Page 19: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Weak Entity ER-to-RDM Mapping, Rule 2b:

• Relationships that attach to a weak entity always follow Rule 2b. This is because the weak entity always has a (1,1) pair. However, the key to the table schema corresponding to the weak entity consists of the attributes in the weak entity key.

Course

CourseNum

Student

StudentNum(1,n) (1,n)

enrolls

Enrollment

Grade

(1,1)

StudentNum CourseNum Grade

pk

Enrollment

Page 20: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

IS_A ER-to-RDM Mapping, Rule 2b:

• IS_A relationships always follow Rule 2b because the sub-entity always has a (1,1) pair.

Person

IDNameDOB

Professor

OfficeNumber

Student

GPA

is_a is_a(0,1) (0,1)

(1,1) (1,1)

ID OfficeNumber

Professor

ID GPA

Student

maps to

Page 21: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Considering (0,1) as (1,1)

• What if a relationship with a (0,1) pair were mapped to a RDM under the assumption that the (0,1) pair is really (1,1)?

becomes (1,1)

disappears

Page 22: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Are These Models Equivalent?

• Suppose two copies exist – one on loan and one not.

accession_no ISBN p_price

Copy

borrowerid accession_no l_date

Borrows

accession_no borrowerid ISBN p_price l_date

Copy

1234 qt-23.4-c1 nov 3

qt-23.4-c1qs-44.7_c1

qt-23.4-c1qs-44.7_c1

1-234-76

20.0012.00

1234null

1-234-76

20.0012.00

nov 3null

There can only be one row in Borrows that contains thevalue “qt-23.4_c1”. Why?

So there can only be one possible value that can go into the borrowerid columnof the row in Copy that contains “qt-2.34_c1”

since this book is noton loan there is no borrowerid or l_date

Page 23: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Considering (1,1) as (1,n):

• Suppose you are a database designer and are told that every project is managed by precisely one employee.

• But you ask, “Might it be possible in the future to have more than one manager for a project?”, and the answer is “Yes”.

managed by

start_date

(0,n) (1,n) Project

ProjectIDTitle

Employee

EmployeeIDE_name

managed by

start_date

(0,n) (1,1) Project

ProjectIDTitle

Employee

EmployeeIDE_name

Page 24: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Considering (1,1) as (1,n):

• xmanaged by

start_date

(0,n) (1,1) Project

ProjectIDTitle

Employee

EmployeeIDE_name

managed by

start_date

(0,n) (1,n) Project1

ProjectIDTitle

Employee1

EmployeeIDE_name

EmployeeID E_Name ProjectID Title EmployeeID start_date

Employee Project

EmployeeID E_Name ProjectID Title

Employee Project

ProjectID EmployeeID start_date

Managed_by

1 A 5 apr 45 mary

5 mary 1 5 apr 4 1 A

Page 25: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Considering (1,1) as (1,n):

• The advantage of the second model is that the relationship <managed by> will be mapped to its own table. This is not true in the first case.

• If you create the ManagedBy table from the very beginning it will be there when you need it.

• A little more work up front saves a lot of work later on.• You can show off later on by saying “It will only take a

second”, come back in 15 minutes and say “the database can now handle multiple project managers” because it always could even when it didn’t need to.

• If you are working as a consultant you can bill for 8 hours even though it didn’t take any time at all.

Page 26: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Library to Relational Data Model

• In the next series of slides we map the Library ER Model to a Relational Data Model

Page 27: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Cardholder to RDM

NOTE: As an entity,Cardholder maps to a table with all its attributesas table columns.The key to Cardholderbecomes the primary key to the table Cardholder.Since Cardholder has no(1,1) participation numberpairs, no new columns are added to the table.

not (1,1)

Page 28: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Book to RDM

NOTE: As an entity,Book maps to a table with all its attributesas table columns.The key to Bookbecomes the primary key to the table Book.Since Book has no (1,1)participation numberpairs, no new columns are added to the table.

not (1,1)

Page 29: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Reserves to RDM

NOTE: As a relationshipwith no (1,1) pair, reservesis mapped to a table whosecolumns include the keysto the entities participatingin the reserves relationshiptogether with the attributesof reserves.

Page 30: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Borrows to RDM

NOTE: As a relationshipwith no (1,1) pair, borrowsis mapped to a table whosecolumns include the keysto the entities participatingin the borrows relationshiptogether with the attributesof borrows.

Page 31: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Copy to RDM

NOTE: As an entity witha (1,1) participation numberpair, Copy is mapped to a table that contains all the Copy attributes and the keyattributes of the other entities(Book) in the relationship where Copy has a (1,1) pair.

The relationship with the (1,1) pair is not mapped toa table.

Page 32: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

Putting it all Together:

Page 33: Database Principles ER to RDM Mapping. Database Principles Mapping from ER to Relational Data Model the next phase Exercise: Give me some suggestions.

Database Principles

is_a

is_a

(0,1)

(0,1) (1,1)

(1,1)

Person

PK ID

FName LName DOB Addr

Student

NumCredits OnProbation

Professor

PhoneNum Rank

CrsSection

PK SecNumPK SemOffered

Location TimeTableSlot

Course

PK CrsNum

CrsName NumCredits SemLastTaught

has_taught

semester

(1,n)

(1,n)

(1,1)

(1,n)enrolled_in

(0,n) (1,n)

grade

Office

PK OfficeNum

Location(1,n)

(1,1)

ID Fname Lname DOB Addr

pk

PersonPersonID OfficeNum PhoneNum Rank

Professor

PersonID NumCredits OnProbation

Student

pk

pk fk

CrsNum CrsName NumCredits SemLastTaught

pk

Course

PersonID CrsNum Semester

Has_taught

pk

CrsNum SecNum SemOffered Location TimeTableSlot

CrsSection

pk

PersonID CrsNum SecNum SemOffered Grade

Enrolled_in

pk

fkfk

fk

fk fk fk fk

OfficeNum Location

pk

Office

Exercise: ER 2 RDM

Person, Office, Professor Student, Course, Has_taughtCrsSection, Enrolled_in