ER to Schema
From E/R Diagrams to Relations Entity Set -> Relation. Attributes -> Attributes. Relationships -> Relations whose attributes are only: The keys of the connected entity sets. Attributes of the relationship itself.
Entity Set -> Relation Pokémon(name, species) name Pokémon species
Relationship -> Relation Pokémon(name, species) Trainer(name) Belongs(p_name, t_name) Fought(attacker, defender) name name belongs Trainer defender Pokémon attacker species fought
Combining Relations You can "combine" relations together: Pokémon(name, species) + Belongs(p_name, t_name) becomes: Pokémon(name, species, trainer) Rule: You can combine in the relationship sets with an entity set, if that set is the "many" part of a many- to-one relationship.
Can we combine Trainers and Fought? 1. No, cyclical relationships sets can't be merged 2. Yes, yielding Trainer(name, badges, defender) 3. No, the "fought" relation is not many- to-one 4. The Pokémon references are silly name badges Trainer defender attacker fought
Many-to-Many Combination Combining "Fought" with "Trainers" leads to redundancy. Trainers Fought Combined name badges Ash 8 Brock 2 Misty 3 Oak attacker defender Ash Brock Misty name badges defender Ash 8 Brock 2 Misty 3 NULL Oak Redundancy! See badges.
Weak Entity Sets to Relations Relation for a weak entity set must include attributes for its complete key (including those belonging to other entity sets), as well as its own, non-key attributes. A supporting relationship is redundant and yields no relation (unless it has attributes). Relations: Pokémon(name, position) Status(effect, duration, position) Has(effect, position) Redundant position name has Pokémon Status effect duration
Subclasses: 3 methods Nulls: One relation; entities have NULL in attributes that don't belong to them Object-oriented: One relation per subset of subclasses, with all relevant attributes E/R style: One relation for each subclass: Key attributes Attributes of that subclass
Example: Nulls isa isa name type Pokémon name type owner run_away Abra psychic NULL True Pikachu electric Red False Onix rock Brock Mew Pokémon isa isa owner Caught Pokémon Wild Pokémon run_away
Example: Object-oriented Pokémon name type name type Mew psychic Pokémon Wild Pokémon name type run_away Abra psychic True isa isa Caught Pokémon name type owner Onix rock Brock Caught and Wild Pokémon owner Caught Pokémon Wild Pokémon run_away name type owner run_away Pikachu electric Red False
Example: E/R style isa isa name type Pokémon Caught Pokémon name type Abra psychic Pikachu electric Onix rock Mew name owner Onix Brock Pikachu Red Pokémon isa isa Wild Pokémon name run_away Abra True Pikachu False owner Caught Pokémon Wild Pokémon run_away
Which method to handle subclasses is best? 1. Object-oriented 2. Nulls 3. E/R style 4. Depends