Weak Entity Sets

Transcription

Weak Entity Sets
Entity-Relationship
Diagrams
MA RCH14 /15, 2016
LectureOverview
Administration
ERDiagrams
WeakEntitySets
DesignTechniques
ERDiagramtoSchema
It is a melancholy
truth that even
great men have
their poor relations.
- CharlesDickens
Administration
Project4specificationhasbeenreleased
(duenextThursday).
HomeworkswillstillbedueonTuesdaysat
10PM
◦ However, Iwillbereleasingfuture homeworks
Tuesdaynights (oneweekbefore theyare
due)
◦ Someofthehomework questions willbe
marked"CoveredWednesday/Thursday"
indicatingthattheycovercontentthathasn't
beentaughtatthetimethehomework was
released.
http://mrsturnbull.com/pb/wp_02a30e70/wp_02a30e70.html
LectureOverview
Administration
ERDiagrams
WeakEntitySets
DesignTechniques
ERDiagramtoSchema
It is a melancholy
truth that even
great men have
their poor relations.
- CharlesDickens
type
Species
name
Instance
Of
level
Pokémon
name
status
Can
Learn
Has
Moves
type
id
PurposeofEntity-RelationModel
TheE/Rmodelisawaytoroughlydescribethe
systemwewanttorepresentinadatabase
Themodelrepresentssomeoftheconstraints
wewanttocapture
Itmayseemsillyatfirst,butdesignisserious
business
Thesediagramscanhelpillustratepotential
waystorepresentyourspecificsystem
◦ Youcan'thaveanefficientdatabasewithoutgood
design
http://www.texample.net/tikz/examples/entityrelationship-diagram/
EntitySet
Entity="thing"orobject
EntitySet=collectionofsimilarentities
◦ Akintoaclassinobject-orientedlanguages
Attribute=propertyof(theentitiesof)
anentityset.
◦ Attributesaresimple(non-compound
values):integers,strings,etc.
◦ Youshouldnothaveanattributethatisa
collection(nolists,structs,sets,etc.)
http://www.imdb.com/media/rm1147641088
/tt0082334
EntitySetsandAttributes
InanEntity-RelationshipDiagram:
◦ Entityset=rectangle
◦ Attribute=oval(withalinetothe
rectanglerepresentingitsentityset)
Here,"Species"istheentityset.
"type"and"name"areattributesof
"Species".
Everyentityin"Species"hasa
"type"and"name".
type
Species
name
Relationships
Arelationshipconnectstwoormoreentitysets.
Eachisrepresentedbyadiamond,withlinestotheentitysets
involved.
type
Species
name
Instance
Of
level
Pokémon
name
status
ValueofanEntitySet
Thecurrent"value"ofanentitysetisthesetofentitiesthatbelongtoit.
Pokémon
name
level
status
aaabaaaajss
69
Sleeping
AIIIIIIRRR
31
NULL
AATTVVV
29
Paralyzed
Species
name
type
Pidgeot
Flying
Lapras
Ice
Venomoth
Poison
ValueofaRelationshipSet
Thecurrent"value"ofarelationshipsetisasetoftupleswithonecomponentforeachrelated
entityset.
InstanceOf
Pokémon
Species
aaabaaaajss
Pidgeot
AIIIIIIRRR
Lapras
AATTVVV
Venomoth
Multiway Relationships
Relationshipscanconnectmorethantwoentitysets.
Supposethattherelationshipset"Battle"involvestheentitysets"Pokémon","Trainers",and
"Music".
name
level
Trainers
Battle
Pokémon
name
status
theme
Music
Multiplicityofrelationships
One-to-One
Many-to-one
Many-to-many
Species
Pokemon
Trainer
has
Instance
of
has
Cry
Species
Item
Whatisthemultiplicityofcharacterto
costume?
character
costume
Harry
Robe
Gandalf
Robe
Jaime
Armor
Jaime
Cape
Mal
Duster
1.One-to-One
2.One-to-Many
3.Many-to-One
4.Many-to-Many
Representing"Multiplicity"
One-to-Onerelationshiphasarrowsenteringbothentitysets.
Many-to-Onerelationshiphasanarrowenteringthe"one"side.
Many-to-Manyrelationshiphasalineconnectingbothentitysets.
ARoundedArrowmeans"exactlyone"
◦ Eachentityofthefirstsetisrelated toexactlyoneentityofthetargetset.
◦ Duetolimitations ofPowerPoint,Iamusing->arrowstorepresentroundedonesandtriangle arrowsfornormalones.
Species
Pokémon
has
has
Cry
Trainer
Eachspeciesmusthaveone cry
(rounded arrow)
EachPokémon can
haveatmostone
trainer(normal arrow)
WhatisthemultiplicityofTrainerto
Catchphrase?
Trainer
says
Catchphrase
1.Exactly-One-to-Exactly-One
2.One-to-One
3.Many-to-Many
4.Toooften
MultipleRelationships
Youcanhavemultiplerelationshipsbetweenthesameentitysets
Pokémon
belongs
fought
Trainer
AttributesonRelationships
Sometimesitisusefultoattachanattributetoarelationship.
Thinkofthisattributeasapropertyoftuplesintherelationshipset.
caught
by
Pokémon
Pokéball
Type
Location
Trainer
EquivalentDiagramWithoutAttributes
onRelationships
caught
by
Pokémon
Capture
Pokéball
Type
Location
Trainer
Roles
Sometimesanentitysetappearsmultipletimesinarelationship.
Theedgesneedtobelabeledwitharole,todisambiguate.
Trainer
Attacker
Defender
battle
Subclasses
Asubclass isaspecialcaseofanentitysetwith
fewerentriesthantheoriginal,butmore
properties.
◦ Aproperty isanattributeorrelationship.
◦ Nomultiple inheritanceallowed!
name
type
Pokémon
Example:"WildPokémon"isasubclassof
"Pokémon".
◦ NoteveryPokémon isaWildPokémon (some have
beencaughtbyTrainers).
◦ WildPokémon haveproperties inaddition to
normal Pokémon (suchasLocationwheretheyare
found orpropensity torunaway).
The"isa"trianglepointsatfromthesubclassto
thesuperclass.
isa
Wild
Pokémon
Location
found
run_away
E/RSubclassesVersusObject-Oriented
Subclasses
InOO,objectseachbelongtooneclassonly.
◦ Subclassesinheritfromsuperclasses
InE/R,entitieshaverepresentativesinallsubclassestowhichtheybelong
◦ Anentityinasubclass,musthaveanentityinallofitssuperclasses.
Example:
◦ TorepresentawildAbra,youneedanentityinthe"WildPokémon"entityset(whichhasits
"found"and"run_away"properties)andinthe"Pokémon"entityset(whichhasitsnameand
typeproperties).
Moredetailslaterinlecture
Keys
Akey isasetofattributesforoneentitysetsuch
thatnotwoentitiesinthissetagreeonall
attributesofthekey.
◦ Itisallowedfortwoentitiestoagreeonsome, but
notall,ofthekeyattributes.
Youmustdesignateakeyforeveryentityset
(underlinethekeyattributes).
Ina"isa"hierarchy,onlytherootentityset(most
superofsuperclasses)hasakey,andthatkey
mustserveasthekeyforallentitiesinthe
hierarchy.
Location
Box
Pokémon
Bill'sPC
3- minutebreak
LectureOverview
Administration
ERDiagrams
WeakEntitySets
DesignTechniques
ERDiagramtoSchema
It is a melancholy
truth that even
great men have
their poor relations.
- CharlesDickens
WeakEntitySets
Occasionally,entitiesofanentitysetneed“help”toidentify
themuniquely(theydon'thaveakey).
EntitysetE issaidtobeweak ifinordertoidentifyentities
ofE uniquely,weneedtofollowoneormoremany-one
relationshipsfromE andincludethekeyoftherelated
entitiesfromtheconnectedentitysets.
Example
Twoentitysets:
◦ Pokémon (name, position)
◦ Status(effect,duration)
Theentityset"Status"requiresa
lookupofthemany-to-exactlyone
relationship"has"togetallofitskeys.
position
name
has
Pokémon
◦ Tospecifyaunique entityin"Status"you
needthe"effect"andthe"position".
Theweakentitysetandthesupporting
many-to-exactly-onerelationships
shouldbedoubled(likeunderliningto
indicatepartofkey).
Status
effect
duration
LectureOverview
Administration
ERDiagrams
WeakEntitySets
DesignTechniques
ERDiagramtoSchema
It is a melancholy
truth that even
great men have
their poor relations.
- CharlesDickens
DesignTechniques
1. Avoidredundancy.
2. Don'tuseanentitysetwhenanattributewilldo.
3. Limittheuseofweakentitysets.
AvoidingRedundancy
Redundancy=sayingthesame
thingintwo(ormore)ways.
Wastesspaceand(more
importantly)encourages
inconsistency.
◦ Tworepresentationsofthesame
factmaybecomeinconsistentif
wechangeoneandforgetto
changetheother.
http://blog.codinghorror.com/departmentof-declaration-redundancy-department/
Example:Good
name
name
Pokémon
position
species
belongs
Trainer
badges
Example:Bad
name
name
belongs
Pokémon
position
species
trainer_name
Trainer
badges
Example:Bad
name
badges
Pokémon
position
species
trainer_name
EntitySetsVersusAttributes
Inordertofavormakinganentitysetoveranattribute,the
entitysetshould:
◦ Bemorethanthenameofsomething.Itshouldhaveatleastone
non-keyattribute.
OR
◦ Itisthe"many"inpartofamany-to-oneormany-to-many
relationship
Example:Good
Pokémonshouldbeanentityset
becauseitisthe"many"sideofa
relationship.
Trainershouldbeanentityset
becauseithasanon-keyattribute.
name
belongs
Pokémon
name
Trainer
badges
Example:Good
Ifwedidn'tcareaboutthebadges...
Trainerdoesn'tneedtobeanentity
setifthenameisallweknowabout
it.
Pokémon
name
trainer
Example:Bad
EitherTrainerorPokémonshouldbe
convertedtoanattribute(notan
entityset)astheydon'thavenonkeyattributes.
name
belongs
Pokémon
name
Trainer
Don’tOveruseWeakEntitySets
Beginningdatabasedesignersoftendoubt
thatanythingcouldbeakeybyitself.
◦ Theymakeallentitysetsweak,supportedbyall
otherentitysetstowhichtheyarelinked.
Inreality,weusuallycreateuniqueID’sfor
entitysets.
◦ Examplesincludesocial-securitynumbers,
automobileVIN’s,Pokémonid,TMnumber, etc.
http://www.keepcalm-omatic.co.uk/p/keep-calm-anddont-be-weak-1/
WhenDoWeNeedWeakEntitySets?
Theusualreasonisthatthereisno
globalauthoritycapableofcreating
uniqueID’s.
Example:itisunlikelythattherecould
beanagreementtoassignunique
playernumbersacrossallfootballteams
intheUS.
http://www.mgoblue.com/sports/m
-footbl/spec-rel/072312aab.html
LectureOverview
Administration
ERDiagrams
WeakEntitySets
DesignTechniques
ERDiagramtoSchema
It is a melancholy
truth that even
great men have
their poor relations.
- CharlesDickens
FromE/RDiagramstoRelations
EntitySet->Relation.
◦Attributes->Attributes.
Relationships->Relationswhoseattributesareonly:
◦Thekeysoftheconnectedentitysets.
◦Attributesoftherelationshipitself.
EntitySet->Relation
Relation:
◦Pokémon(name,species)
name
Pokémon
species
Relationship->Relation
name
name
Pokémon
belongs
Trainer
attacker
species
Relations:
fought
defender
◦Pokémon(name,species)
◦Trainer(name)
◦Belongs(p_name,t_name)
◦Fought(attacker,defender)
CombiningRelations
Youcan"combine"relationstogether:
◦Pokémon(name,species)+Belongs(p_name,t_name)
becomes:
◦Pokémon(name,species,trainer)
Rule:Youcancombineintherelationshipsetswith
anentityset,ifthatsetisthe"many"partofamanyto-onerelationship.
CanwecombineTrainersandFought?
1.No,cyclicalrelationshipssetscan'tbe
merged
2.Yes,yieldingTrainer(name,badges,
defender)
3.No,the"fought"relationisnotmanyto-one
4.ThePokémonreferencesaresilly
name
badges
Trainer
attacker
fought
defender
Many-to-ManyCombination
Combining"Fought"with"Trainers"leadstoredundancy.
Trainers
Fought
Combined
name
badges
attacker
defender
name
badges
defender
Ash
8
Ash
Brock
Ash
8
Brock
Brock
2
Brock
Ash
Brock
2
Ash
Misty
3
Ash
Misty
Ash
8
Misty
Oak
0
Ash
Ash
Ash
8
Ash
Misty
3
NULL
Oak
0
NULL
Redundancy!Seebadges.
WeakEntitySetstoRelations
Relationforaweakentitysetmustinclude
attributesforitscompletekey(including
thosebelongingtootherentitysets),aswell
asitsown,non-keyattributes.
Asupportingrelationshipisredundantand
yieldsnorelation(unlessithasattributes).
position
name
has
Pokémon
Relations:
◦ Pokémon(name, position)
◦ Status(effect,duration, position)
◦ Has(effect,position) Redundant
Status
effect
duration
Subclasses:3methods
1. Nulls:Onerelation;entitieshaveNULLinattributesthatdon't
belongtothem
2. Object-oriented:Onerelationpersubsetofsubclasses,withall
relevantattributes
3. E/Rstyle:Onerelationforeachsubclass:
◦
◦
Keyattributes
Attributesofthatsubclass
Example:Nulls
Pokémon
type
name
name
type
owner
run_away
Abra
psychic
NULL
True
Pikachu
electric
Red
False
Onix
rock
Brock
NULL
Mew
psychic
NULL
NULL
Pokémon
owner
isa
isa
Caught
Pokémon
Wild
Pokémon
run_away
Example:Object-oriented
Pokémon
name
type
Mew
psychic
Pokémon
WildPokémon
name
type
Abra
psychic
CaughtPokémon
type
name
run_away
True
name
type
owner
Onix
rock
Brock
CaughtandWildPokémon
name
type
owner
run_away
Pikachu
electric
Red
False
owner
isa
isa
Caught
Pokémon
Wild
Pokémon
run_away
Example:E/Rstyle
Pokémon
CaughtPokémon
name
type
name
owner
Abra
psychic
Onix
Brock
Pikachu
electric
Pikachu
Red
Onix
rock
Mew
psychic
Pokémon
WildPokémon
name
run_away
Abra
True
Pikachu
False
type
name
owner
isa
isa
Caught
Pokémon
Wild
Pokémon
run_away
Whichmethodtohandlesubclassesis
best?
1.Object-oriented
2.Nulls
3.E/Rstyle
4.Depends