database systems i - datenbanksysteme...

33
Database Systems I Foundations of Databases Summer term 2010 Melanie Herschel [email protected] Database Systems Group, University of Tübingen 1 Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen Chapter 2 The Entity-Relationship Model 1. Introduction 2. ER-Modeling 3. Relational model(ing) 4. Relational algebra 5. SQL 6. Programming • After completing this chapter, you should be able to: Motivate and explain the three phases of database design, Evaluate the significance of the Entity-Relationship Model (ER model) for DB design, Enumerate the basic constructs of the ER model, Develop ER diagrams (schemas in the ER model) for a given application. 2

Upload: ngodieu

Post on 05-Apr-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Database Systems IFoundations of Databases

Summer term 2010

Melanie [email protected]

Database Systems Group, University of Tübingen

1

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Chapter 2The Entity-Relationship Model

1. Introduction2. ER-Modeling3. Relational model(ing)4. Relational algebra5. SQL6. Programming

• After completing this chapter, you should be able to:

‣Motivate and explain the three phases of database design,

‣ Evaluate the significance of the Entity-Relationship Model (ER model) for DB design,

‣ Enumerate the basic constructs of the ER model,

‣ Develop ER diagrams (schemas in the ER model) for a given application.

2

Page 2: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 Melanie Herschel | University of Tübingen

4/18/10 12:07 PMNcis_ER_diagram.gif 1631×1448 pixels

Page 1 of 1http://www.meds-sdmm.dfo-mpo.gc.ca/MEDS/Prog_Nat/NCIS/Definition/Ncis_ER_diagram.gif

Chapter 2The ER-Model

• Database Design Overview

• Basic ER Constructs

• Cardinalities

• Keys, Weak Entities

• Design Principles

3

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Database Design

•Overall goal of DBMS usage: Efficiently develop programs to support given real-world tasks.

•These programs need to store data persistently.

•To develop these programs, apply proven methods of software engineering—specialized to support data-intensive programs.

Database design

• Database design is the process of developing a database schema for a given application.

• Database design is a subtask of the overall software engineering effort.

4

Page 3: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Database Design

•The specification of programs and data is intertwined:

‣ The schema should contain the data needed by the programs.

‣ Programs are often easy to develop once the structure of the data to be manipulated has been specified.

•Data, however, is an independent resource:

‣ Typically, additional programs will be developed later based on the collected data.

‣ Also, ad-hoc queries will be posed against the DB.

5

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Database Design

•During DB design, a formal model of the relevant aspects of the real world (“mini world”, “domain of discourse”) must be built.

• Once the DB is up and running, questions will be posed against the DB. Knowledge of these questions beforehand is important input to the DB design process and helps to identify the relevant parts of the real world.

•In some sense, the real world is the measure of correctness for the DB schema: the possible DB states should correspond to the states of the real world.

6

Page 4: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Database Design

•DB design is not easy for a variety of reasons:

‣ Expertise: The designer must become an expert for the application domain or needs to talk to such experts. Depending on the domain, these experts might not be used to give a complete and formal account of their field.

‣ Flexibility: The real world typically permits exceptions and corner cases. Does the DB schema need to reflect these?

‣ Size: Database schemas may become huge (in terms of number of relations, attributes, constraints).

•Due to this complexity, DB design is a multi-step process.

7

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Database Design

Three phases of DB design

Conceptual Database Design

Logical Database Design

Physical Database Design

Produces the initial model of the mini world in a conceptual data model (e.g., in the ER model).

Transforms the conceptual schema into the data model supported by the DBMS (e.g., the relational model).

Design indexes, table distribution, buffer size, etc. to maximize performance of the final system (subject of “DBSII - Implementation of database systems”).

8

Page 5: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Database Design

Why multiple design phases?

• Partition the problem, attack one sub-problem after the other.For example, during conceptual design there is no need to worry about performance aspects or limitations of the specific SQL dialect of the RDBMSs.

•DBMS features do not influence the conceptual design and only partially influence the logical design.

!Thus, the conceptual design work is not invalidated if a different DBMS is used later on.

9

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

The ER Model

•Proposed by Peter Chen (1976), today at Louisiana State University.

•The ER model comes with a graphical notation which helps to establish quick overviews of database application schemas.

•Such ER diagrams are also a great way to communicate schemas to non-expert/future DB users.

!"#$%&'(')*+,-"'(.&#/(0*1.2,--!"#$%&'(')*+,-"'(.&#/(0*1.2,--

!"

3"4/$5,',6$57$8/,&$9:;<

=/,$,&'(')*6,-"'(.&#/(0 >.2,-$? '.@"62# "$A&(B(,2 C(,@ .B 2"'"7$D81$=E!FD81$=E!F

F'"&2"62>.2,--$(&$2,6$B6G/,&$%&'@A6B#0/"#,

H,-(I$3"A>"&&$J$!"',&K"&L#)#',>,$M$J$F.>>,6$NOO:

10

Page 6: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Example

•This mini world talks about actors and movies.

•Actors star in movies.

•Actors have a firstname, a lastname, and a date of birth (DOB)

•Movies have a title, a year, and a director.

ER schema in graphical notation

11

stars_inActor Movie

Firstname Lastname DOB Title Year Director

(1, m) (1, n)

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Example

A possible state of this mini world

Star Wars

Actor Movie

Harrison

Ford

13.7.1942

1977

Lucas

Raiders of the Lost Ark

1981

Spielberg

firstname

lastname

DOB

title

year

director

title

year

director

12

Page 7: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

The ER Model

•The Entity-Relationship Model is often referred to as a semantic data model, because it more closely resembles real world scenarios than, e.g., the relational model.

‣ In the ER model, we model the concept of “Actor”.In the relational model we deal with names and dates of birth.

‣ In the ER model, there is a distinction between entities (objects) and relationships between such entities.In the relational model, both concepts are represented by relations.

•There are no “ER Model DBMS”.

Instead, we will describe a translation of ER model concepts into the relational model.

13

Foundations of Databases | Summer term 2010 Melanie Herschel | University of Tübingen

4/18/10 12:07 PMNcis_ER_diagram.gif 1631×1448 pixels

Page 1 of 1http://www.meds-sdmm.dfo-mpo.gc.ca/MEDS/Prog_Nat/NCIS/Definition/Ncis_ER_diagram.gif

Chapter 2The ER-Model

• Database Design Overview

• Basic ER Constructs

• Cardinalities

• Keys, Weak Entities

• Design Principles

14

Page 8: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Basic ER Model ConceptsEntity

•An entity is an object in the mini world about which information is to be stored. Examples: actors, movies, studios, awards.

• Note: entities do not have to correspond to objects of physical existence. Entities may also represent conceptual objects like, e.g., vacation.

•The mini world that has to be modeled can contain only a finite number of objects.

•It must be possible to distinguish entities from each other, i.e., objects must have some identity.

Examples: entity book identified by ISBN number, entity vacation identified by travel agency booking number.

15

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Basic ER Model ConceptsAttribute

•An attribute is a property or feature of an entity (or relationship, see below).

Example: the title of this movie entity is “Star Wars”.

•The value of an attribute is an element of a data type like string, integer, date.

•These values have a printable representation (which entities have not).

16

Page 9: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Basic ER Model ConceptsRelationship

•A relationship represents a relation—not in the strict relational model sense—between pairs of entities (a binary relationship).

Example: Ford (an entity) stars in (a relationship) the movie “Star Wars” (an entity).

•“Relationship” is often used as an abbreviation for “Relationship type” (see next slide).

17

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Basic ER Model ConceptsEntity and Relationship Type

Entity Type:

A set of similar entities (similar with respect to the information which has to be stored about them), i.e., entities which have the same attributes.

Example: All actors vs. all directors.

Relationship Type:

A set of similar relationships, i.e., relationships which have the same attributes.

Example: X stars in movie Y.

18

Page 10: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

•Entity type E

•Attribute A of entity type E

•Binary relationship R between entity types E1 and E2

ER DiagramsBasic Graphical Syntax

E

RE1 E2

EA

19

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

•Relationships may also exist between entities of the same type (“recursive relationship”)

• In this case, role names have to be attached to the connecting edges.In the ER model, role names may be attached to any kind of relationship for documentation purposes.

Recursive relationship

ER DiagramsRecursive Relationships

is_sequel Movie

Original

Sequel

20

Page 11: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

•Relationships may have attributes, too:

Relationship attributes

This models the fact that a role is stored for every pair of actor X and Movie Y such that X starred in Y.

ER DiagramsAttributes of Relationships

stars_inActor Movie

Role

21

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• An ER diagram contains boxes, diamonds, ovals, plus interconnecting lines.

• Boxes, diamonds, and ovals are each labelled by a string.

• Box labels are unique in the entire diagram.

• Oval labels are unique for a single box or diamond.

• Diamond labels are unique for a pair of connected boxes.

• Interconnecting lines are only allowed between box—diamond, box—oval, diamond—oval.

• A diamond has exactly two connecting lines to boxes. There may be any number of connections to ovals.

• An oval has exactly one connecting line.

ER DiagramsGraphical Syntax

22

Page 12: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

ER Example

Modeling a mini world: define an ER diagram

• Information about movies is to be stored.

•For each movie, the title, release date, genre, and homepage URI are relevant.

•Movies may be granted awards (e.g., Oscars, Palme d’Or, ...)

•Relevant award information are the name, the category, and the year of the award.

•Each movie is produced by a studio (of a given name and address).

23

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

ER Schemas: Semantics

Definition

A DB state I interprets the symbols in the ER schema by defining

• A finite set I(E) for every entity type E,

• A mapping I(A):I(E) → val(D) for every attribute A of an entity type E, where D is the data type of A and val(D) is the domain of D,

• A relation I(R) ⊆ I(E1) ! I(E2) for every relationship R between entity types E1 and E2.

• A mapping I(A):I(R) → val(D) for every attribute A of relationship R (D is the data type of A).

24

Page 13: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

ER Schemas: Semantics

Example state corresponding to ER schema

•I(Actor) = {JD,HBC}•I(Firstname) = f1 with f1(JD) = “Jonny” and f1(HBC) = “Helena Bonham”•I(Lastname) = f2 with f2(JD) = “Depp” and f2(HBC) = “Carter”•I(DOB) = f3 with f3(JD) = “09.06.1963” and f3(HBC) = “26.05.1966”

•I(Movie) = {CB,ST, PC}•I(Title) = g1 with g1(CB) = “A Corpse Bride”, g1(ST) = “Sweeney Todd”, • and g1(PC) = “Pirates of the Caribbean”•I(Year) = g2 with g2(CB) = 2003, g2(ST) = 2007, and g2(PC) = 2006•I(Director) = g3 with g3(CB) = “Tim Burton”, g3(ST) = “Tim Burton”, g3(PC) = “Gore Verbinski”

stars_inActor Movie

Firstname Lastname DOB Title Year Director

25

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

ER Schemas: Semantics

Example state corresponding to ER schema (cont.)

• I(stars_in) = {(JD, CB), (JD, ST), (JD,PC), (HBC,CB), (HBC,ST) }

•Note: A relationship is interpreted by a set of entity pairs and thus is unique for two given entities.

Actor Actor Actor

JD HBC

MovieCB x x

MovieST x x

PC x

26

Page 14: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

ER Schemas: Semantics

Consequences of the ER semantics

•Consider the above ER diagram.

•Suppose a cinema shows the same movie twice a day (at 3pm and 6pm).

•Can this information be stored in the given schema?

showsCinema Movie

Time

27

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• We have seen that an ER model represents binary relationships only.

• However, in the real world, a relationship may relate more than two entities to each other.

An n-ary relationship

From n-ary Relationships to Binary Relationships

has_contract_withActor Movie

Studio

28

Page 15: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• To transform an n-ary relationship to a binary relationship, we

1.Create a new association entity type.

2.Introduce a relationship between the new entity type and the old entity types.

3.If there is an entity with multiple roles, create a relationship type per role.

4.Attributes of the old relationship type are appended to the new entity type.

From n-ary Relationships to Binary Relationships

29

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

From n-ary Relationships to Binary Relationships

An n-ary relationship

has_contract_withActor Movie

StudioProducing studioContracting studio

Actor Movie

Studio

Contract is_subject_ofsigned_contract

is_contracting_studio is_producing_studio

30

Page 16: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

From n-ary Relationships to Binary Relationships

Modeling that a cinema shows the same movie twice a day

showsCinema Movie

Time

31

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

IS-A: A Special Type of Relationship

•Subclass

‣ Special case of the (super)class

‣ Less entities fall into the subclass (compared to the entities falling in the superclass)

‣More (specific) attributes

‣ Possibly more relationships to other entities.

•The ER model allows to specify a subclass in the form of an IS-A relationship

‣ Represented as a triangle

‣ Tip is showing to the superclass

‣ The relationship implicitly is a 1:1 relationship (and is not explicitly represented)

32

Page 17: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

IS-A: A Special Type of Relationship

Subclasses to movies

Movie

Animation Movies

Crime Movies Weapons

Title Year Genre Length

is_voice_ofActor

IS-A IS-A

Subclasses of movies are• Animation movies‣ One additional attribute

• Crime movies‣ One additional relationship

33

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

IS-A: A Special Type of Relationship

•The IS-A relationship forms trees, a class may have multiple subclasses, but a class may not have multiple superclasses.

•An entity may consist of several components of the IS-A-tree

‣ “Star Wars” has four attributes.

‣ “Cindarella” has four attributes plus a “Voices” relationship.

‣ “Der Dritte Mann” has four attributes plus the “Weapons” attribute.

‣ “Roger Rabbit” has four attributes, plus the “Weapons”-Attribute and the “Voices”-Relationship.

34

Page 18: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 Melanie Herschel | University of Tübingen

4/18/10 12:07 PMNcis_ER_diagram.gif 1631×1448 pixels

Page 1 of 1http://www.meds-sdmm.dfo-mpo.gc.ca/MEDS/Prog_Nat/NCIS/Definition/Ncis_ER_diagram.gif

Chapter 2The ER-Model

• Database Design Overview

• Basic ER Constructs

• Cardinalities

• Keys, Weak Entities

• Design Principles

35

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• In general, an arbitrary number of entities of type A can relate to an arbitrary number of entities of type B via a binary relationship.

• An actor can star in numerous movies.

• A movie set comprises many actors.

• An entity of type A may not relate to any entity of type B, and vice versa.

• Meet the many “actors” sitting in LA cafes that have never played in any movie.

• National geographic movies do not have actors.

! Many-to-Many or m:n relationship

‣ Special cases areone-to-many (1:n) and one-to-one (1:1) relationships.

CardinalitiesM:N Relationships

A B

36

Page 19: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• An entity of type A can relate to any number of entities of type B.

• An entity of type B can relate to at most one entity of type A.

• Example

• A movie may win many awards.

• An award is granted to one movie.

• Again, an entity of type A may not relate to any entity of type B, and vice versa.

• A movie may not win any award.

Cardinalities1:N Relationships

A B

37

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• An entity of type A can relate to at most one entity of type B.

• An entity of type B can relate to at most one entity of type A.

• Example

• A studio may only have one President.

• One President may only head one studio.

• Again, an entity of type A may not relate to any entity of type B and vice versa.

• A studio may temporarily not have any president.

Cardinalities1:1 Relationships

A B

38

Page 20: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• The ER model introduces the (min, max) notation to specify an interval of possible participations in an relationship:

• An entity of type E1 may be related to at least m1 and at most n1 entities of type E2.

• Likewise, m2 is the minimum number and n2 is the maximum number of E1 entities to which an E2 entity is related.

CardinalitiesThe (min, max) notation

RE1 E2(m1, n1) (m2, n2)

39

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

CardinalitiesThe (min, max) notation

Formal Semantics

•For every DB state I and every entity e1 ∈ I(E1):m1 " | {e2 ∈ I(E2) | (e1, e2) ∈ I(R) } | " n1

•For every DB state I and every entity e2 ∈ I(E2):m2 " | {e1 ∈ I(E1) | (e1, e2) ∈ I(R) } | " n2

• Extensions:

‣ “ * ” may be used as maximum if there is no limit.

‣ (0, *) means no restriction at all (general relationship).

• Note that the maximum cardinalities on each side are used to distinguish between m:n, 1:n, and 1:1 relationships.

40

Page 21: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

CardinalitiesThe (min, max) notation

Studio Presidents (1:1 relationship)

“A studio can have at most one president and a president can preside at most one studio.”

is_president_ofPresident Studio(0,1) (0,1)

Movie awards (1:n relationship)

“A movie award goes exactly to one movie. A movie may be awarded multiple awards (but maybe none at all).”

awarded_toAward Movie(1,1) (0,*)

41

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

CardinalitiesThe (min, max) notation

Derive cardinalities from verbal specifications

“Besides normal customers, the database may contain customers who have not yet ordered anything.”

fromOrder Customer( , ) ( , )

Derive cardinalities from verbal specifications

“An order can contain several products.”

forOrder Product( , ) ( , )

42

Page 22: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

CardinalitiesAlternative notations

Movie awards (1:n relationship)

• Leave the minimum cardinality unspecified.

• This notation exactly represents m:n, 1:n, or 1:1 relationships.

awarded_toAward MovieN 1

43

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

• Sometimes found in software supporting visual ER diagram development (e.g., in Oracle DesignerTM):

‣ The “crow foot” indicates the “many” side.

‣Dashed lines indicate optional participiation.

‣Relationship roles are given (for both sides).

CardinalitiesAlternative notations

Movie awards (1:n relationship)

Award Movieawarded to

won

44

Page 23: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

CardinalitiesAlternative notations

!"#$%&"'%()(*+,&$-#-+./("(%/&-&!"#$%&"'%()(*+,&$-#-+./("(%/&-&

!"

#$%&'()*+',%&-.//'012'3+//4567

0-'%1+."23"&&+4+5"(-&6"&7898(-3-+:+4+;/33-#+<==>

* 0 7

45

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

CardinalitiesMovie Example

Keys in our movie scenario

Add cardinalities using the (min,max) notation

Actor Movie

Studio

Contract is_subject_ofsigned_contract

is_contracting_studio is_producing_studio

46

Page 24: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 Melanie Herschel | University of Tübingen

4/18/10 12:07 PMNcis_ER_diagram.gif 1631×1448 pixels

Page 1 of 1http://www.meds-sdmm.dfo-mpo.gc.ca/MEDS/Prog_Nat/NCIS/Definition/Ncis_ER_diagram.gif

Chapter 2The ER-Model

• Database Design Overview

• Basic ER Constructs

• Cardinalities

• Keys, Weak Entities

• Design Principles

47

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Keys

ER key

A key K of an entity type E is an attribute of E which uniquely identifies the entities of this type. No two different entities share the same value for the key attribute, i.e., I(K) is injective for all DB states I. Composite keys are allowed.

E

K A1 A2

48

Page 25: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Keys

Keys in our movie scenario

•Movies• Is title a good candidate key? (4 King Kong movies in IMDB)•How about Title + Year? (Still around 300 “duplicate keys”)

•Actor• Firstname+Lastname or• Firstname+Lastname+DOB

•Studio•Name

49

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Where is the Key?

50

Page 26: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Keys

Keys in our movie scenario

Underline keys

Actor Movie

Studio

Contract is_subject_ofsigned_contract

is_contracting_studio is_producing_studio

Firstname

Lastname

DOB

Title

Year

Type

Length

Contract_ID Date_Signed

Name Address

51

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Weak Entities

•In many schemas, some entities describe a kind of detail that cannot exist without a master (or owner) entity.

‣ If entities are part of an IS-A hierarchy

‣ If entities are association entities that have been created to eliminate n-ary relationships.

‣ If the existence of the detail entity (e.g., a room) requires the existence of the master entity (e.g., a building).

•In such a case,

1.There is a relationship with cardinality (1, 1) on the detail entity side, and in addition

2.The key of the master is inherited and becomes part of the key of the detail entity.

52

Page 27: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Weak Entities

•Without a specific ER construct for this case, we would require the following additional constraint:

‣ If two entities are in “has” relationship, e.g., an invoice and a position,

‣Then their attribute “Inv No” are required to have identical values. For example, invoice #12 cannot have position 2 in invoice #42 as detail.

•Such constraints occur if an entity does not have a key by itself, but it is only unique in the context of some other (master) entity.

53

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Weak Entities

•Note: In such cases, keys are always composite.

•Examples:

‣ A classroom is identified by a building and a room number.

‣ A section in a book is identified by a chapter and a section title.

‣ A web page URI is composed of a web server DNS address and a path on that server.

•There is also an existence dependency. For instance, if the building is pulled down, the classrooms automatically disappear. If the web server is shut down, all URIs on that server cease to function.

54

Page 28: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Weak Entities

• In the ER model, such scenarios are modeled via weak entities (non-weak entities are also called strong entities).

• In ER diagrams, weak entities and their identifying relationships are indicated by double lines.

•For the weak entity, the inherited part of the key is not shown.

Notation of weak entities

hasInvoice Position

Inv_No Date Pos

(1,*) (1,1)

55

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Weak Entities

•Usage of weak entities is required only if the key of one entity contains the key of a related entity. A weak entity adds further key attributes to the master key.

• Weak entities are normal entities except that their key is constructed in a special way.

•Weak entities may themselves be masters of other weak entities (building an entire hierarchy of dependencies).

56

Page 29: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Weak Entities

Modeling with weak entities

Model a set of online quizzies (multiple choice tests).

‣ Each quiz is identified by a title, each question within a quiz is numbered, and each possible answer to a given question is referenced by a letter.

‣ For each question and answer, the associated text is stored. Answers are classified into correct and incorrect ones.

•What is the complete key for each of the occurring entity types?

57

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Association Entities Revisited

•We saw that association entities are created in order to resolve n-ary relationships.

•For instance, we found that modeling a cinema that shows the same movie twice a day requires a ternary relationship.

•Use an association entity: turn relationship into weak entity with multiple owners. Inherits keys of all owners.

Association entity “Show”

does

Cinema MovieTime

Show of

Name Title DescAddress

58

Page 30: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Weak Entities in Movie Example

Weak entites in our movie scenario

Assuming keys and relationships identified earlier, do we have weak entities here? If not, what would need to be changed to obtain at least one?

Actor Movie

Studio

Contract is_subject_ofsigned_contract

is_contracting_studio is_producing_studio

Firstname

Lastname

DOB

Title

Year

Type

Length

Contract_ID Date_Signed

Name Address

59

Foundations of Databases | Summer term 2010 Melanie Herschel | University of Tübingen

4/18/10 12:07 PMNcis_ER_diagram.gif 1631×1448 pixels

Page 1 of 1http://www.meds-sdmm.dfo-mpo.gc.ca/MEDS/Prog_Nat/NCIS/Definition/Ncis_ER_diagram.gif

Chapter 2The ER-Model

• Database Design Overview

• Basic ER Constructs

• Cardinalities

• Keys, Weak Entities

• Design Principles

60

Page 31: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Basic Principles

•Design based on application need.

•Avoid redundancy.

•Keep it simple!

•Use relationships wisely (and sparingly).

•Use attributes wisely (and sparingly).

•Use weak entities wisely (and sparingly).

61

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Design Based on Application Need

•Entity types and attributes should reflect the reality and should be useful for the application.

‣When modeling movies, gender is not a sensible attribute.

‣ For applications like Amazon, storing the address of each actor does not make much sens.

•Relationship types should reflect relationships in the real-world or the semantics valid for the application

‣ Actors and movies are in an m:n relationship, 1:n, n:1 or 1:1 would not correctly reflect the real world.

‣ The relationship between courses and teachers depends on the semantics within the application:

- A course is taught by one teacher (1:n)

- A team of teachers teaches a course (m:n)62

Page 32: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Avoid Redundancy

•Redundant information occurs when the same information is expressed in multiple ways.

•Redundancy wastes space, both on paper and on disk.

•Redundancy eases the appearance of inconsistencies, as information has to be changed at all places where it is redundantly represented.

63

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Keep it Simple

KISS: Keep It Simple, St...!

• Avoid using unnecessary entity types.

‣ For instance, do you really care that a movie is represented by a holding and that the holding in turn owns a studio?

• Avoid using unnecessary relationship types.

‣ For instance, does your application really need the distinction between contracting studios and producing studios?

‣ Avoid redundancy when relationships can be derived.

• Avoid using unnecessary attributes.

‣ If your application does not use the actor nickname, there is no need to model it.

64

Page 33: Database Systems I - Datenbanksysteme Tübingendb.inf.uni-tuebingen.de/staticfiles/teaching/ss10/dbs1/02_er.pdf · Database Systems I Foundations of Databases Summer term 2010 Melanie

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Association entity “Show”

Attribute vs. Element

• Attributes are easier to implement than entities and relationships.

• An entity type is justified if

• It represents more than just the name of an object, or

• it is the “n-part” or a 1:n relationship.

ownsStudio Movie

Name Title

Movie

Title StudioName

• Studio only has a name as attribute.

• Studio is not the n-part of a 1:n relationship.

65

Foundations of Databases | Summer term 2010 | Melanie Herschel | University of Tübingen

Summary

• Three phases of database design: conceptual, logical, and physical DB design.

• Basic ER constructs: entity types, relationship types, attributes

• 1:1, 1:N, and M:N relationships and min-max-notation.

• Inheritance of attributes through IS-A relationship.

• Keys in ER-models uniquely identify entities

• Modeling Existence dependency using weak entities

• Keep it simple!

66