EntityRelationship Model ER Diagrams Weak Entity Sets Converting

  • Slides: 70
Download presentation
Entity-Relationship Model E/R Diagrams Weak Entity Sets Converting E/R Diagrams to Relations 1

Entity-Relationship Model E/R Diagrams Weak Entity Sets Converting E/R Diagrams to Relations 1

Purpose of E/R Model u. The E/R model allows us to sketch database schema

Purpose of E/R Model u. The E/R model allows us to sketch database schema designs. w Includes some constraints, but not operations. u. Designs are pictures called entityrelationship diagrams. u. Later: convert E/R designs to relational DB designs. 2

Framework for E/R u. Design is a serious business. u. The “boss” knows they

Framework for E/R u. Design is a serious business. u. The “boss” knows they want a database, but they don’t know what they want in it. u. Sketching the key components is an efficient way to develop a working database. 3

Entity Sets u. Entity = “thing” or object. u. Entity set = collection of

Entity Sets u. Entity = “thing” or object. u. Entity set = collection of similar entities. w Similar to a class in object-oriented languages. u. Attribute = property of (the entities of) an entity set. w Attributes are simple values, e. g. integers or character strings, not structs, sets, etc. 4

E/R Diagrams u. In an entity-relationship diagram: w Entity set = rectangle. w Attribute

E/R Diagrams u. In an entity-relationship diagram: w Entity set = rectangle. w Attribute = oval, with a line to the rectangle representing its entity set. 5

Example: name manf Beers u. Entity set Beers has two attributes, name and manf

Example: name manf Beers u. Entity set Beers has two attributes, name and manf (manufacturer). u. Each Beers entity has values for these two attributes, e. g. (Bud, Anheuser-Busch) 6

Relationships u. A relationship connects two or more entity sets. u. It is represented

Relationships u. A relationship connects two or more entity sets. u. It is represented by a diamond, with lines to each of the entity sets involved. 7

Example: Relationships name addr name Bars Beers Sells license Note: license = beer, full,

Example: Relationships name addr name Bars Beers Sells license Note: license = beer, full, none Frequents name Drinkers manf Likes Bars sell some beers. Drinkers like some beers. Drinkers frequent some bars. addr 8

Relationship Set u. The current “value” of an entity set is the set of

Relationship Set u. The current “value” of an entity set is the set of entities that belong to it. w Example: the set of all bars in our database. u. The “value” of a relationship is a relationship set, a set of tuples with one component for each related entity set. 9

Example: Relationship Set u. For the relationship Sells, we might have a relationship set

Example: Relationship Set u. For the relationship Sells, we might have a relationship set like: Bar Joe’s Bar Sue’s Bar Beer Bud Miller Bud Pete’s Ale Bud Lite 10

Multiway Relationships u. Sometimes, we need a relationship that connects more than two entity

Multiway Relationships u. Sometimes, we need a relationship that connects more than two entity sets. u. Suppose that drinkers will only drink certain beers at certain bars. w Our three binary relationships Likes, Sells, and Frequents do not allow us to make this distinction. w But a 3 -way relationship would. 11

Example: 3 -Way Relationship name license addr name Bars manf Beers Preferences Drinkers name

Example: 3 -Way Relationship name license addr name Bars manf Beers Preferences Drinkers name addr 12

A Typical Relationship Set Bar Joe’s Bar Sue’s Bar Joe’s Bar Sue’s Bar Drinker

A Typical Relationship Set Bar Joe’s Bar Sue’s Bar Joe’s Bar Sue’s Bar Drinker Ann Ann Bob Cal Beer Miller Bud Pete’s Ale Bud Miller Bud Lite 13

Many-Many Relationships u. Focus: binary relationships, such as Sells between Bars and Beers. u.

Many-Many Relationships u. Focus: binary relationships, such as Sells between Bars and Beers. u. In a many-many relationship, an entity of either set can be connected to many entities of the other set. w E. g. , a bar sells many beers; a beer is sold by many bars. 14

In Pictures: many-many 15

In Pictures: many-many 15

Many-One Relationships u. Some binary relationships are many one from one entity set to

Many-One Relationships u. Some binary relationships are many one from one entity set to another. u. Each entity of the first set is connected to at most one entity of the second set. u. But an entity of the second set can be connected to zero, one, or many entities of the first set. 16

In Pictures: many-one 17

In Pictures: many-one 17

Example: Many-One Relationship u. Favorite, from Drinkers to Beers is many-one. u. A drinker

Example: Many-One Relationship u. Favorite, from Drinkers to Beers is many-one. u. A drinker has at most one favorite beer. u. But a beer can be the favorite of any number of drinkers, including zero. 18

One-One Relationships u. In a one-one relationship, each entity of either entity set is

One-One Relationships u. In a one-one relationship, each entity of either entity set is related to at most one entity of the other set. u. Example: Relationship Best-seller between entity sets Manfs (manufacturer) and Beers. w A beer cannot be made by more than one manufacturer, and no manufacturer can have more than one best-seller (assume no ties). 19

In Pictures: one-one 20

In Pictures: one-one 20

Representing “Multiplicity” u. Show a many-one relationship by an arrow entering the “one” side.

Representing “Multiplicity” u. Show a many-one relationship by an arrow entering the “one” side. w Remember: Like a functional dependency. u. Show a one-one relationship by arrows entering both entity sets. u. Rounded arrow = “exactly one, ” i. e. , each entity of the first set is related to exactly one entity of the target set. 21

Example: Many-One Relationship Drinkers Likes Favorite Beers Notice: two relationships connect the same entity

Example: Many-One Relationship Drinkers Likes Favorite Beers Notice: two relationships connect the same entity sets, but are different. 22

Example: One-One Relationship u. Consider Best-seller between Manfs and Beers. u. Some beers are

Example: One-One Relationship u. Consider Best-seller between Manfs and Beers. u. Some beers are not the best-seller of any manufacturer, so a rounded arrow to Manfs would be inappropriate. u. But a beer manufacturer has to have a best-seller. 23

In the E/R Diagram Manfs Bestseller A beer is the bestseller for 0 or

In the E/R Diagram Manfs Bestseller A beer is the bestseller for 0 or 1 manufacturer. Beers A manufacturer has exactly one best seller. 24

Attributes on Relationships u. Sometimes it is useful to attach an attribute to a

Attributes on Relationships u. Sometimes it is useful to attach an attribute to a relationship. u. Think of this attribute as a property of tuples in the relationship set. 25

Example: Attribute on Relationship Bars Sells Beers price Price is a function of both

Example: Attribute on Relationship Bars Sells Beers price Price is a function of both the bar and the beer, not of one alone. 26

Equivalent Diagrams Without Attributes on Relationships u. Create an entity set representing values of

Equivalent Diagrams Without Attributes on Relationships u. Create an entity set representing values of the attribute. u. Make that entity set participate in the relationship. 27

Example: Removing an Attribute from a Relationship Bars Sells Prices price Beers Note convention:

Example: Removing an Attribute from a Relationship Bars Sells Prices price Beers Note convention: arrow from multiway relationship = “all other entity sets together determine a unique one of these. ” 28

Roles u. Sometimes an entity set appears more than once in a relationship. u.

Roles u. Sometimes an entity set appears more than once in a relationship. u. Label the edges between the relationship and the entity set with names called roles. 29

Example: Roles Relationship Set Husband Bob Joe … Married husband Wife Ann Sue …

Example: Roles Relationship Set Husband Bob Joe … Married husband Wife Ann Sue … wife Drinkers 30

Example: Roles Relationship Set Buddies 1 2 Buddy 1 Bob Joe Ann Joe …

Example: Roles Relationship Set Buddies 1 2 Buddy 1 Bob Joe Ann Joe … Buddy 2 Ann Sue Bob Moe … Drinkers 31

Subclasses u. Subclass = special case = fewer entities = more properties. u. Example:

Subclasses u. Subclass = special case = fewer entities = more properties. u. Example: Ales are a kind of beer. w Not every beer is an ale, but some are. w Let us suppose that in addition to all the properties (attributes and relationships) of beers, ales also have the attribute color. 32

Subclasses in E/R Diagrams u. Assume subclasses form a tree. w I. e. ,

Subclasses in E/R Diagrams u. Assume subclasses form a tree. w I. e. , no multiple inheritance. u. Isa triangles indicate the subclass relationship. w Point to the superclass. 33

Example: Subclasses name Beers manf isa color Ales 34

Example: Subclasses name Beers manf isa color Ales 34

E/R Vs. Object-Oriented Subclasses u. In OO, objects are in one class only. w

E/R Vs. Object-Oriented Subclasses u. In OO, objects are in one class only. w Subclasses inherit from superclasses. u. In contrast, E/R entities have representatives in all subclasses to which they belong. w Rule: if entity e is represented in a subclass, then e is represented in the superclass (and recursively up the tree). 35

Example: Representatives of Entities name Beers isa color manf Pete’s Ales 36

Example: Representatives of Entities name Beers isa color manf Pete’s Ales 36

Keys u. A key is a set of attributes for one entity set such

Keys u. A key is a set of attributes for one entity set such that no two entities in this set agree on all the attributes of the key. w It is allowed for two entities to agree on some, but not all, of the key attributes. u. We must designate a key for every entity set. 37

Keys in E/R Diagrams u. Underline the key attribute(s). u. In an Isa hierarchy,

Keys in E/R Diagrams u. Underline the key attribute(s). u. In an Isa hierarchy, only the root entity set has a key, and it must serve as the key for all entities in the hierarchy. 38

Example: name is Key for Beers name Beers manf isa color Ales 39

Example: name is Key for Beers name Beers manf isa color Ales 39

Example: a Multi-attribute Key dept number hours room Courses • Note that hours and

Example: a Multi-attribute Key dept number hours room Courses • Note that hours and room could also serve as a key, but we must select only one key. 40

Weak Entity Sets u. Occasionally, entities of an entity set need “help” to identify

Weak Entity Sets u. Occasionally, entities of an entity set need “help” to identify them uniquely. u. Entity set E is said to be weak if in order to identify entities of E uniquely, we need to follow one or more manyone relationships from E and include the key of the related entities from the connected entity sets. 41

Example: Weak Entity Set uname is almost a key for football players, but there

Example: Weak Entity Set uname is almost a key for football players, but there might be two with the same name. unumber is certainly not a key, since players on two teams could have the same number. u. But number, together with the team name related to the player by Plays-on should be unique. 42

In E/R Diagrams name number Players name Playson Teams Note: must be rounded because

In E/R Diagrams name number Players name Playson Teams Note: must be rounded because each player needs a team to help with the key. • Double diamond for supporting many-one relationship. • Double rectangle for the weak entity set. 43

Weak Entity-Set Rules u. A weak entity set has one or more many-one relationships

Weak Entity-Set Rules u. A weak entity set has one or more many-one relationships to other (supporting) entity sets. w Not every many-one relationship from a weak entity set need be supporting. w But supporting relationships must have a rounded arrow (entity at the “one” end is guaranteed). 44

Weak Entity-Set Rules – (2) u. The key for a weak entity set is

Weak Entity-Set Rules – (2) u. The key for a weak entity set is its own underlined attributes and the keys for the supporting entity sets. w E. g. , (player) number and (team) name is a key for Players in the previous example. 45

Design Techniques 1. Avoid redundancy. 2. Limit the use of weak entity sets. 3.

Design Techniques 1. Avoid redundancy. 2. Limit the use of weak entity sets. 3. Don’t use an entity set when an attribute will do. 46

Avoiding Redundancy u. Redundancy = saying the same thing in two (or more) different

Avoiding Redundancy u. Redundancy = saying the same thing in two (or more) different ways. u. Wastes space and (more importantly) encourages inconsistency. w Two representations of the same fact become inconsistent if we change one and forget to change the other. w Recall anomalies due to FD’s. 47

Example: Good name Beers name Manf. By addr Manfs This design gives the address

Example: Good name Beers name Manf. By addr Manfs This design gives the address of each manufacturer exactly once. 48

Example: Bad name Beers name Manf. By addr Manfs manf This design states the

Example: Bad name Beers name Manf. By addr Manfs manf This design states the manufacturer of a beer twice: as an attribute and as a related entity. 49

Example: Bad name manf. Addr Beers This design repeats the manufacturer’s address once for

Example: Bad name manf. Addr Beers This design repeats the manufacturer’s address once for each beer and loses the address if there are temporarily no beers for a manufacturer. 50

Entity Sets Versus Attributes u An entity set should satisfy at least one of

Entity Sets Versus Attributes u An entity set should satisfy at least one of the following conditions: w It is more than the name of something; it has at least one nonkey attribute. or w It is the “many” in a many-one or many relationship. 51

Example: Good name Beers name Manf. By addr Manfs • Manfs deserves to be

Example: Good name Beers name Manf. By addr Manfs • Manfs deserves to be an entity set because of the nonkey attribute addr. • Beers deserves to be an entity set because it is the “many” of the many-one relationship Manf. By. 52

Example: Good name manf Beers There is no need to make the manufacturer an

Example: Good name manf Beers There is no need to make the manufacturer an entity set, because we record nothing about manufacturers besides their name. 53

Example: Bad name Beers name Manf. By Manfs Since the manufacturer is nothing but

Example: Bad name Beers name Manf. By Manfs Since the manufacturer is nothing but a name, and is not at the “many” end of any relationship, it should not be an entity set. 54

Don’t Overuse Weak Entity Sets u. Beginning database designers often doubt that anything could

Don’t Overuse Weak Entity Sets u. Beginning database designers often doubt that anything could be a key by itself. w They make all entity sets weak, supported by all other entity sets to which they are linked. u. In reality, we usually create unique ID’s for entity sets. w Examples include social-security numbers, automobile VIN’s etc. 55

When Do We Need Weak Entity Sets? u. The usual reason is that there

When Do We Need Weak Entity Sets? u. The usual reason is that there is no global authority capable of creating unique ID’s. u. Example: it is unlikely that there could be an agreement to assign unique player numbers across all football teams in the world. 56

From E/R Diagrams to Relations u. Entity set -> relation. w Attributes -> attributes.

From E/R Diagrams to Relations u. Entity set -> relation. w Attributes -> attributes. u. Relationships -> relations whose attributes are only: w The keys of the connected entity sets. w Attributes of the relationship itself. 57

Entity Set -> Relation manf name Beers Relation: Beers(name, manf) 58

Entity Set -> Relation manf name Beers Relation: Beers(name, manf) 58

Relationship -> Relation name husband Drinkers 1 name addr Likes manf Beers 2 Buddies

Relationship -> Relation name husband Drinkers 1 name addr Likes manf Beers 2 Buddies Favorite wife Married Likes(drinker, beer) Favorite(drinker, beer) Buddies(name 1, name 2) Married(husband, wife) 59

Combining Relations u OK to combine into one relation: 1. The relation for an

Combining Relations u OK to combine into one relation: 1. The relation for an entity-set E 2. The relations for many-one relationships of which E is the “many. ” u Example: Drinkers(name, addr) and Favorite(drinker, beer) combine to make Drinker 1(name, addr, fav. Beer). 60

Risk with Many-Many Relationships u. Combining Drinkers with Likes would be a mistake. It

Risk with Many-Many Relationships u. Combining Drinkers with Likes would be a mistake. It leads to redundancy, as: name addr Sally 123 Maple beer Bud Miller Redundancy 61

Handling Weak Entity Sets u. Relation for a weak entity set must include attributes

Handling Weak Entity Sets u. 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, nonkey attributes. u. A supporting relationship is redundant and yields no relation (unless it has attributes). 62

Example: Weak Entity Set -> Relation name bill. To Logins name At Hosts location

Example: Weak Entity Set -> Relation name bill. To Logins name At Hosts location Hosts(host. Name, location) Logins(login. Name, host. Name, bill. To) At(login. Name, host. Name 2) At becomes part of Logins Must be the same 63

Subclasses: Three Approaches 1. Object-oriented : One relation per subset of subclasses, with all

Subclasses: Three Approaches 1. Object-oriented : One relation per subset of subclasses, with all relevant attributes. 2. Use nulls : One relation; entities have NULL in attributes that don’t belong to them. 3. E/R style : One relation for each subclass: w Key attribute(s). w Attributes of that subclass. 64

Example: Subclass -> Relations name Beers manf isa color Ales 65

Example: Subclass -> Relations name Beers manf isa color Ales 65

Object-Oriented name manf Bud Anheuser-Busch Beers name Summerbrew manf Pete’s Ales color dark Good

Object-Oriented name manf Bud Anheuser-Busch Beers name Summerbrew manf Pete’s Ales color dark Good for queries like “find the color of ales made by Pete’s. ” 66

E/R Style name manf Bud Anheuser-Busch Summerbrew Pete’s Beers name Summerbrew color dark Ales

E/R Style name manf Bud Anheuser-Busch Summerbrew Pete’s Beers name Summerbrew color dark Ales Good for queries like “find all beers (including ales) made by Pete’s. ” 67

Using Nulls name manf Bud Anheuser-Busch Summerbrew Pete’s Beers color NULL dark Saves space

Using Nulls name manf Bud Anheuser-Busch Summerbrew Pete’s Beers color NULL dark Saves space unless there are lots of attributes that are usually NULL. 68

References in your textbook u. Chapter 4 w 4. 1 w 4. 2 w

References in your textbook u. Chapter 4 w 4. 1 w 4. 2 w 4. 3 w 4. 4 w 4. 5 w 4. 6 69

Homework from your textbook u 4. 1. 1 (2. 1. 2 of second edition,

Homework from your textbook u 4. 1. 1 (2. 1. 2 of second edition, solution given in course homepage) u 4. 1. 3 u 4. 1. 9 u 4. 2. 1 u 4. 2. 5 u 4. 3. 1. b u 4. 4. 4 u 4. 5. 1 u 4. 5. 2 u 4. 6. 1 70