Sei sulla pagina 1di 8

5/2/2015

MappingERModelsintoRelations

VillaPlotsinNorthBLR
PeerlessIntegratedResidentialTownship.BookingsOpenNow!

PreviousPage NextPage UpOneLevel


LectureSlidesavailable:PDFPowerPoint

MappingERModelsinto
Relations
Contents
Whatisarelation?
Foreignkeys
PreparingtomaptheERmodel
Mapping1:1relationships
Mandatoryatbothends
Whennottocombine
Ifnotcombined...
Example
Mandatory<>Optional
Mandatory<>OptionalSubsume?
Summary...
Optionalatbothends...
Mapping1:mrelationships
Mappingn:mrelationships
Summary

DatabaseNotes
OnlineNotes
ReferencePages
TutorialActivities
OnlineSQL
OnlineQuiz
DiscussionForum
FutureStuff
OnlineRelational
Algebra
News

Overview
map1:1relationshipsintorelations
map1:mrelationshipsintorelations
mapm:nrelationshipsintorelations
differencesbetweenmappingoptionalandmandatory
relationships.

Whatisarelation?
Arelationisatablethatholdsthedataweareinterestedin.Itistwo
dimensionalandhasrowsandcolumns.
EachentitytypeintheERmodelismappedintoarelation.
Theattributesbecomethecolumns.
Theindividualentitiesbecometherows.

http://db.grussell.org/section006.html

1/8

5/2/2015

MappingERModelsintoRelations

Figure:arelation

Relationscanberepresentedtextuallyas:
tablename(primarykey,attribute1,attribute2,...,foreignkey)

Ifmatric_nowastheprimarykey,andtherewerenoforeignkeys,
thenthetableabovecouldberepresentedas:
student(matricno,name,address,date_of_birth)

Whenreferringtorelationsortables,cardinalityisconsideredtothe
thenumberofrowsintherelationortable,andarityisthenumberof
columnsinatableorattributesinarelation.

Foreignkeys
Aforeignkeyisanattribute(orgroupofattributes)thatisthe
primarykeytoanotherrelation.
Roughly,eachforeignkeyrepresentsarelationshipbetween
twoentitytypes.
Theyareaddedtorelationsaswegothroughthemapping
process.
Theyallowtherelationstobelinkedtogether.
Arelationcanhaveseveralforeignkeys.
Itwillgenerallyhaveaforeignkeyfromeachtablethatitis
relatedto.
Foreignkeysareusuallyshowninitalicsorwithawiggly
underline.

PreparingtomaptheERmodel
Beforewestarttheactualmappingprocessweneedtobecertain
thatwehavesimplifiedtheERmodelasmuchaspossible.
Thisistheidealtimetocheckthemodel,asitisreallythelast
chancetomakechangestotheERmodelwithoutcausingmajor
complications.

Mapping1:1relationships
Beforetacklinga1:1relationship,weneedtoknowitsoptionality.
Therearethreepossibilitiestherelationshipcanbe:
http://db.grussell.org/section006.html

2/8

5/2/2015

MappingERModelsintoRelations

1. mandatoryatbothends
2. mandatoryatoneendandoptionalattheother
3. optionalatbothends

Mandatoryatbothends
Iftherelationshipismandatoryatbothendsitisoftenpossibleto
subsumeoneentitytypeintotheother.
Thechoiceofwhichentitytypesubsumestheotherdepends
onwhichisthemostimportantentitytype(moreattributes,
betterkey,semanticnatureofthem).
Theresultofthisamalgamationisthatalltheattributesofthe
`swallowedup'entitybecomeattributesofthemoreimportant
entity.
Thekeyofthesubsumedentitytypebecomesanormal
attribute.
Ifthereareanyattributesincommon,theduplicatesare
removed.
Theprimarykeyofthenewcombinedentityisusuallythe
sameasthatoftheoriginalmoreimportantentitytype.

Whennottocombine
Thereareafewreasonwhyyoumightnotcombinea1:1mandatory
relationship.
thetwoentitytypesrepresentdifferententitiesinthe`real
world'.
theentitiesparticipateinverydifferentrelationshipswithother
entities.
efficiencyconsiderationswhenfastresponsesarerequiredor
differentpatternsofupdatingoccurtothetwodifferententity
types.

Ifnotcombined...
Ifthetwoentitytypesarekeptseparatethentheassociation
betweenthemmustberepresentedbyaforeignkey.
Theprimarykeyofoneentitytypecomestheforeignkeyinthe
other.
Itdoesnotmatterwhichwayarounditisdonebutyoushould
nothaveaforeignkeyineachentity.

Example
Twoentitytypesstaffandcontract.
Eachmemberofstaffmusthaveonecontractandeach
contractmusthaveonememberofstaffassociatedwithit.
Itisthereforeamandatoryrelationsatbothends.

http://db.grussell.org/section006.html

3/8

5/2/2015

MappingERModelsintoRelations

Figure:1:1mandatoryrelationship

Thesetoentitytypescouldbeamalgamatedintoone.
Staff(emp_no,name,cont_no,start,end,position,salary)

orkeptapartandaforeignkeyused
Staff(emp_no,name,contract_no)
Contract(cont_no,start,end,position,salary)

or
Staff(emp_no,name)
Contract(cont_no,start,end,position,salary,emp_no)

Mandatory<>Optional
Theentitytypeoftheoptionalendmaybesubsumedintothe
mandatoryendasinthepreviousexample.
ItisbetterNOTtosubsumethemandatoryendintotheoptionalend
asthiswillcreatenullentries.

Figure:1:1with1optionalend

Ifweaddtothespecificationthateachstaffmembermayhaveat
mostonecontract(thusmakingtherelationoptionalatoneend).
MaptheforeignkeyintoStaffthekeyisnullforstaffwithouta
contract.
Staff(emp_no,name,contract_no)
Contract(cont_no,start,end,position,salary)

MaptheforeignkeyintoContractemp_noismandatorythus
nevernull.
Staff(emp_no,name)
Contract(cont_no,start,end,position,salary,emp_no)

Example
Considerthisexample:
StaffGordon,empno10,contractno11.
http://db.grussell.org/section006.html

4/8

5/2/2015

MappingERModelsintoRelations

StaffAndrew,empno11,nocontract.
Contract11,from1stJan2001to10thJan2001,lecturer,on
2.00ayear.
ForeignkeyinStaff:
ContractTable:
Cont_no Start
11

End

Position Salary

1stJan2001 10thJan2001 Lecturer 2.00

StaffTable:
Empno Name

ContractNo

10

Gordon 11

11

Andrew NULL

However,ForeignkeyinContract:

http://db.grussell.org/section006.html

5/8

5/2/2015

MappingERModelsintoRelations

ContractTable:
Cont_no Start
11

End

Position Salary Empno

1stJan2001 10thJan2001 Lecturer 2.00

10

StaffTable:
Empno Name
10

Gordon

11

Andrew

Asyoucansee,bothwaysstorethesameinformation,butthe
secondwayhasnoNULLs.

Mandatory<>OptionalSubsume?
Thereasonsfornotsubsumingarethesameasbeforewiththe
followingadditionalreason.
veryfewoftheentitiesfromthemandatoryendareinvolvedin
therelationship.Thiscouldcausealotofwastedspacewith
manyblankornullentries.

Figure:1optionalend

IfonlyafewlecturersmanagecoursesandCourseis
subsumedintoLecturerthentherewouldbemanynullentries
inthetable.
Lecturer(lect_no,l_name,cno,c_name,type,yr_vetted,external)

Itwouldbebettertokeepthemseparate.
Lecturer(lect_no,l_name)
Course(cno,c_name,type,yr_vetted,external,lect_no)

Summary...
Sofor1:1optionalrelationships,taketheprimarykeyfromthe
`mandatoryend'andaddittothe`optionalend'asaforeignkey.
So,givenentitytypesAandB,whereA<>Bisarelationshipwhere
http://db.grussell.org/section006.html

6/8

5/2/2015

MappingERModelsintoRelations

theAenditoptional,theresultwouldbe:
A(primarykey,attribute,...,foreignkeytoB)
B(primarykey,attribute,...)

Optionalatbothends...
Suchexamplescannotbeamalgamatedasyoucouldnotselecta
primarykey.Instead,oneforeignkeyisusedasbefore.

Figure:2optionalend

Eachstaffmembermayleaseuptoonecar
Eachcarmaybeleasedbyatmostonememberofstaff
Ifthesewerecombinedtogether...
Staff_car(emp_no,name,reg_no,year,make,type,colour)

whatwouldbetheprimarykey?
Ifemp_noisusedthenallthecarswhicharenotbeingleased
willnothaveakey.
Similarly,ifthereg_noisused,allthestaffnotleasingacar
willnothaveakey.
Acompoundkeywillnotworkeither.

Mapping1:mrelationships
Tomap1:mrelationships,theprimarykeyonthe`oneside'ofthe
relationshipisaddedtothe`manyside'asaforeignkey.
Forexample,the1:mrelationship`coursestudent':

Figure:Mapping1:mrelationships

Assumingthattheentitytypeshavethefollowingattributes:
Course(course_no,c_name)
Student(matric_no,st_name,dob)

Thenaftermapping,thefollowingrelationsareproduced:
Course(course_no,c_name)
Student(matric_no,st_name,dob,course_no)

Ifanentitytypeparticipatesinseveral1:mrelationships,then
youapplytheruletoeachrelationship,andaddforeignkeys
asappropriate.
http://db.grussell.org/section006.html

7/8

5/2/2015

MappingERModelsintoRelations

Mappingn:mrelationships
Ifyouhavesomem:nrelationshipsinyourERmodelthentheseare
mappedinthefollowingmanner.
Anewrelationisproducedwhichcontainstheprimarykeys
frombothsidesoftherelationship
Theseprimarykeysformacompositeprimarykey.

Figure:Mappingn:mrelationships

Thus
Student(matric_no,st_name,dob)
Module(module_no,m_name,level,credits)

becomes
Student(matric_no,st_name,dob)
Module(module_no,m_name,level,credits)
Studies(matric_no,module_no)

Thisisequivalentto:

Figure:AfterMappingan:mrelationship
Student(matric_no,st_name,dob)
Module(module_no,m_name,level,credits)
Study()

Summary
11relationships
Dependingontheoptionalityoftherelationship,theentities
areeithercombinedortheprimarykeyofoneentitytypeis
placedasaforeignkeyintheotherrelation.
1mrelationships
Theprimarykeyfromthe`oneside'isplacedasaforeignkey
inthe`manyside'.
mnrelationships
Anewrelationiscreatedwiththeprimarykeysfromeach
entityformingacompositekey.
PreviousPage NextPage UpOneLevel

http://db.grussell.org/section006.html

8/8

Potrebbero piacerti anche