EntityRelationship Modelling ER Diagrams and EER Diagrams Some

  • Slides: 59
Download presentation
Entity-Relationship Modelling ER Diagrams and EER Diagrams Some design techniques Elements and process: text

Entity-Relationship Modelling ER Diagrams and EER Diagrams Some design techniques Elements and process: text diagram Slides by Jeff Ullman (infolab. stanford. edu/~ullman/dscb/pslides/er. ppt), with ER syntax modified and some additions Bettina Berendt, ISI 2015 Last updated 2015 -10 -05 (changes only to last slide) 1

Purpose of ER Model u The ER model allows us to sketch database schema

Purpose of ER Model u The ER 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 ER designs to relational DB designs. 2

Framework for ER u Design is a serious business. u The “boss” knows they

Framework for ER 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

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

ER 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 BEER u Entity set BEER has two attributes, name and manf

Example: name manf BEER u Entity set BEER has two attributes, name and manf (manufacturer). u Each BEER 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 BAR name BEER SELLS → license FREQUENTS ↑ Note: license

Example: Relationships name addr BAR name BEER SELLS → license FREQUENTS ↑ Note: license = beer, full, none name DRINKER manf LIKES ↑ Bars sell some beers. Drinkers like some beers. Drinkers frequent some bars. addr 8

Note: Relationships and reading direction ! u The convention for ER diagrams is to

Note: Relationships and reading direction ! u The convention for ER diagrams is to place entities and/or name relationships such that you can read the relationship name w From top to bottom and/or w From left to right u It’s best practice to do that when possible. u But it doesn’t always work. Convention for us (similarly in some ER notations): add an arrow after the relationship name that indicates the reading direction. w This is not always possible / sensible / needed. w For example: SPOUSE (see below) or in ternary relationships such as PREFERS (see below). w Indicate the reading direction where possible / sensible, otherwise don’t. 9

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

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 11

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

Example: 3 -Way Relationship (aka ternary relationship) name license addr name BAR manf BEER

Example: 3 -Way Relationship (aka ternary relationship) name license addr name BAR manf BEER PREFERS ↑ DRINKER name addr 13

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 14

Many-Many Relationships u Focus: binary relationships, such as SELLS between BAR and BEER. u

Many-Many Relationships u Focus: binary relationships, such as SELLS between BAR and BEER. 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. 15

In Pictures: many-many 16

In Pictures: many-many 16

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

In Pictures: many-one 18

In Pictures: many-one 18

Example: Many-One Relationship u HAS-FAVORITE, from DRINKER to BEER is many-one. u A drinker

Example: Many-One Relationship u HAS-FAVORITE, from DRINKER to BEER 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. 19

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 HAS-BESTSELLER between entity sets MANF (manufacturer) and BEER. w A beer cannot be made by more than one manufacturer, and no manufacturer can have more than one best-seller (assume no ties). 20

In Pictures: one-one 21

In Pictures: one-one 21

Representing “Multiplicity” u There are different notations that have slightly different semantics. u We

Representing “Multiplicity” u There are different notations that have slightly different semantics. u We will use the min-max notation. u (x, y)= An entity at this side of the relationship can take part between x and y times in the relationship. w x : 0 or 1; or a specific minimal value w y : 1 or n; or a specific maximal value 22

Example: Many-One Relationship DRINKER (1, n) LIKES → (0, n) (1, 1) BEER (0,

Example: Many-One Relationship DRINKER (1, n) LIKES → (0, n) (1, 1) BEER (0, n) HAS-FAVORITE → Notice: two relationships connect the same entity sets, but are different. 23

Example: One-One Relationship u Consider HAS-BESTSELLER between MANF and BEER. u Some beers are

Example: One-One Relationship u Consider HAS-BESTSELLER between MANF and BEER. u Some beers are not the best-seller of any manufacturer, so a rounded arrow to MANF would be inappropriate. u But a beer manufacturer has to have a best-seller. 24

In the ER Diagram MANF (1, 1) HAS-BESTSELLER → A manufacturer has exactly one

In the ER Diagram MANF (1, 1) HAS-BESTSELLER → A manufacturer has exactly one best seller. (0, 1) BEER A beer is the bestseller for 0 or 1 manufacturer. 25

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

Example: Attribute on Relationship BAR SELLS → BEER price Price is a function of

Example: Attribute on Relationship BAR SELLS → BEER price Price is a function of both the bar and the beer, not of one alone. 27

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

Example: Removing an Attribute from a Relationship BAR SELLS → PRICE value BEER Note

Example: Removing an Attribute from a Relationship BAR SELLS → PRICE value BEER Note convention: arrow from multiway relationship = “all other entity sets together determine a unique one of these. ” 29

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

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

Example: Roles Relationship Set Husband Bob Joe … SPOUSE husband Wife Ann Sue … wife DRINKER 31

Example: Roles (with multiplicities) Relationship Set Husband Bob Joe … SPOUSE husband (0, 1)

Example: Roles (with multiplicities) Relationship Set Husband Bob Joe … SPOUSE husband (0, 1) Wife Ann Sue … wife DRINKER (0, 1) 32

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

Example: Roles Relationship Set BUDDY 2 1 Buddy 1 Bob Joe Ann Joe … Buddy 2 Ann Sue Bob Moe … DRINKER 33

Example: Roles (with multiplicities) Relationship Set BUDDY 1 2 Buddy 1 Bob Joe Ann

Example: Roles (with multiplicities) Relationship Set BUDDY 1 2 Buddy 1 Bob Joe Ann Joe … Buddy 2 Ann Sue Bob Moe … (0, n) DRINKER (0, n) 34

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

Subclasses in EER (extended ER) Diagrams (The only EER construct we will consider) u

Subclasses in EER (extended ER) Diagrams (The only EER construct we will consider) u Assume subclasses form a tree. w I. e. , no multiple inheritance. u Lines with “cups” indicate the subclass relationship (also known as “is. A”). w Cup is open to the superclass. 36

Example: Subclasses name BEER manf U color ALE 37

Example: Subclasses name BEER manf U color ALE 37

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

Keys in ER Diagrams u Underline the key attribute(s). u In an is. A

Keys in ER Diagrams u Underline the key attribute(s). u In an is. A hierarchy, only the root entity set has a key, and it must serve as the key for all entities in the hierarchy. 39

Example: name is Key for BEER name BEER manf U color ALE 40

Example: name is Key for BEER name BEER manf U color ALE 40

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

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

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

Example: Weak Entity Set u name is almost a key for football players, but

Example: Weak Entity Set u name is almost a key for football players, but there might be two with the same name. u number 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. 43

In ER Diagrams name number PLAYER name PLAYSON → (0, n) TEAM • Double

In ER Diagrams name number PLAYER name PLAYSON → (0, n) TEAM • Double diamond for supporting many-one relationship. • Double rectangle for the weak entity set. 44

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

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). 45

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 PLAYER in the previous example. 46

Design Techniques 1. Avoid redundancy. 2. Don’t use an entity set when an attribute

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

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

Example: Good name BEER name MANFBY → addr MANF This design gives the address

Example: Good name BEER name MANFBY → addr MANF This design gives the address of each manufacturer exactly once. 49

Example: Bad name BEER name MANFBY → addr MANF manf This design states the

Example: Bad name BEER name MANFBY → addr MANF manf This design states the manufacturer of a beer twice: as an attribute and as a related entity. 50

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

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

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

Example: Good name BEER name MANFBY → addr MANF • MANF deserves to be

Example: Good name BEER name MANFBY → addr MANF • MANF deserves to be an entity set because of the nonkey attribute addr. • BEER deserves to be an entity set because it is the “many” of the many-one relationship MANFBY. 53

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

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

Example: Bad name BEER name MANFBY → MANF Since the manufacturer is nothing but

Example: Bad name BEER name MANFBY → MANF 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. 55

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

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

Not covered in this course, but straightforward and sometimes useful

Not covered in this course, but straightforward and sometimes useful

From text to ER diagram: task The European Parliament organises its debates by sessions.

From text to ER diagram: task The European Parliament organises its debates by sessions. Sessions are organised by year and month. Sessions are made up of session days. On each session day, one or more agenda items are debated. Agenda items are numbered, and they have a title and a date. Speeches are made by a member of the European Parliament as contributions to an agenda item. Speeches are numbered, and they are spoken in a language, which is recorded as the spoken text. The texts are also translated into the other EU languages. (It is ok to only model the English version. ) In addition, the URI of a video of the speech is recorded. Members of Parliament have an MEP_ID, a given name and a family name, and a date of birth. They represent a country of the EU. They hold political functions (e. g. President or Vice President) in political institutions, which are identified uniquely by an acronym and also carry a label. Members of parliament hold a political function for a certain period, identified by its begin and end date. Political institutions include EU committees, EU parties and national parties. Countries are described by an acronym and a label. Different countries have different years in which they became members of the EU. 59