EntityRelationship Model Diagrams Class hierarchies Weak entity sets

  • Slides: 54
Download presentation
Entity-Relationship Model Diagrams Class hierarchies Weak entity sets 1

Entity-Relationship Model Diagrams Class hierarchies Weak entity sets 1

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

Purpose of E/R Model u. The E/R model allows us to sketch database designs. w Kinds of data and how they connect. w Not how data changes. u. Designs are pictures called entityrelationship diagrams. u. Later: convert E/R designs to relational DB designs. 2

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. 3

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. 4

Example model manf Cars u. Entity set Cars has two attributes, name and manf

Example model manf Cars u. Entity set Cars has two attributes, name and manf (manufacturer). u. Each Cars entity has values for these two attributes, e. g. (Mustang, Ford) 5

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. 6

Example name addr Dealers name Cars Sells license Note: license = Car, full, none

Example name addr Dealers name Cars Sells license Note: license = Car, full, none Frequents name Likes Drivers manf Dealers sell some Cars. Drivers like some Cars. Drivers frequent some Dealers. addr 7

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 Dealers in our database. u. The “value” of a relationship is a set of lists of currently related entities, one from each of the related entity sets. 8

Example u. For the relationship Sells, we might have a relationship set like: Dealer

Example u. For the relationship Sells, we might have a relationship set like: Dealer Joe’s Dealer Sue’s Dealer Car Mustang RX 8 Mustang Carolla Mustang 9

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 Drivers will only drink certain Cars at certain Dealers. w Our three binary relationships Likes, Sells, and Frequents do not allow us to make this distinction. w But a 3 -way relationship would. 10

Example name license addr model Dealers manf Cars Preferences Drivers name addr 11

Example name license addr model Dealers manf Cars Preferences Drivers name addr 11

A Typical Relationship Set Dealer Joe’s Dealer Sue’s Dealer Joe’s Dealer Sue’s Dealer Driver

A Typical Relationship Set Dealer Joe’s Dealer Sue’s Dealer Joe’s Dealer Sue’s Dealer Driver Car Ann Ann Bob Cal RX 8 Mustang Carolla Mustang RX 8 Mustang 12

Many-Many Relationships u. Focus: binary relationships, such as Sells between Dealers and Cars. u.

Many-Many Relationships u. Focus: binary relationships, such as Sells between Dealers and Cars. 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 Dealer sells many Cars; a Car is sold by many Dealers. 13

In Pictures: many-many 14

In Pictures: many-many 14

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. 15

In Pictures: many-one 16

In Pictures: many-one 16

Example u. Favorite, from Drivers to Cars is manyone. u. A Driver has at

Example u. Favorite, from Drivers to Cars is manyone. u. A Driver has at most one favorite Car. u. But a Car can be the favorite of any number of Drivers, including zero. 17

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 Cars. w A Car cannot be made by more than one manufacturer, and no manufacturer can have more than one best-seller (assume no ties). 18

In Pictures: one-one 19

In Pictures: one-one 19

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. 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. 20

Example Drivers Likes Cars Favorite 21

Example Drivers Likes Cars Favorite 21

Example u. Consider Best-seller between Manfs and Cars. u. Some Cars are not the

Example u. Consider Best-seller between Manfs and Cars. u. Some Cars are not the best-seller of any manufacturer, so a rounded arrow to Manfs would be inappropriate. u. But a Car manufacturer has to have a best-seller. 22

In the E/R Diagram Manfs Bestseller Cars 23

In the E/R Diagram Manfs Bestseller Cars 23

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. 24

Example Dealers Sells Cars price Price is a function of both the Dealer and

Example Dealers Sells Cars price Price is a function of both the Dealer and the Car, not of one alone. 25

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. 26

Example Dealers Sells Prices price Cars Note convention: arrow from multiway relationship = “all

Example Dealers Sells Prices price Cars Note convention: arrow from multiway relationship = “all other entity sets together determine a unique one of these. ” 27

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. 28

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

Example Relationship Set Husband Bob Joe … Married husband Wife Ann Sue … wife Drivers 29

Example Relationship Set Mustangdriver 1 Bob Joe Ann Joe Mustangdrivers … 1 Mustangdriver 2

Example Relationship Set Mustangdriver 1 Bob Joe Ann Joe Mustangdrivers … 1 Mustangdriver 2 Ann Sue Bob Moe … 2 Drivers 30

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

Subclasses u. Subclass = special case = fewer entities = more properties. u. Example: Sports Cars are a kind of Car. w Not every Car is an ale, but some are. w Let us suppose that in addition to all the properties (attributes and relationships) of Cars, Sports Cars also have the attribute top. 31

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. 32

Example name Cars manf isa top Sports Cars Top- convertible, hard top, etc. 33

Example name Cars manf isa top Sports Cars Top- convertible, hard top, etc. 33

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. 34

Example name Cars isa top manf Carolla Sports Cars 35

Example name Cars isa top manf Carolla Sports Cars 35

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. 36

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. 37

Example: name is Key for Cars name Cars manf isa top Sports Cars 38

Example: name is Key for Cars name Cars manf isa top Sports Cars 38

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. 39

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. 40

Example uname is almost a key for football players, but there might be two

Example 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. 41

In E/R Diagrams name number Players name Playson Teams • Double diamond for supporting

In E/R Diagrams name number Players name Playson Teams • Double diamond for supporting many-one relationship. • Double rectangle for the weak entity set. 42

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. 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. 43

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. 44

Avoiding Redundancy u. Redundancy occurs when we say the same thing in two or

Avoiding Redundancy u. Redundancy occurs when we say the same thing in two or more different ways. u. Redundancy wastes space and (more importantly) encourages inconsistency. w The two instances of the same fact may become inconsistent if we change one and forget to change the other. 45

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

Example: Good model Cars name Manf. By addr Manfs This design gives the address of each manufacturer exactly once. 46

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

Example: Bad model Cars name Manf. By addr Manfs manf This design states the manufacturer of a Car twice: as an attribute and as a related entity. 47

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

Example: Bad model manf. Addr Cars This design repeats the manufacturer’s address once for each Car and loses the address if there are temporarily no Cars for a manufacturer. 48

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. 49

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

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

Example: Good model manf Cars There is no need to make the manufacturer an

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

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

Example: Bad model Cars 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. 52

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. 53

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. 54