Entity-Relationship (ER) Model

CS 4750 Systems

[A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, Ch.6] [C.M. Ricardo and S.D. Urban, Database Illuminated, Ch.3]

Fall 2021 – University of Virginia © Praphamontripong 1 I’ve Got Great Ideas for An App

How the How the project How the analyst customer leader explained it understood it designed it

• In , communication is key

• Many people are involved in the design process

We need a way to communicate our ideas

How the sale What the How the executive customer really developer built it described it needed

[modified from image by Janetti, https://www.cleanpng.com/png-project-management-project-manager-architectural-e-5729993/download-png.html]

Fall 2021 – University of Virginia © Praphamontripong 2 Overview: Creating DB App

Design Ideas /

+

Design program that access High-level and update data design +

Design security schema to Database control access of

Database design = process of organizing data into a by considering data to be stored and the interrelationship of the data

Fall 2021 – University of Virginia © Praphamontripong 3 Database Design Process

Interact with users and domain experts to characterize the data

Translate requirements into (E-R diagrams)

Convert the model to (schema and constraints)

Normalize and develop conceptual (logical) schema of the database

Develop (partitioning and indexing)

Fall 2021 – University of Virginia © Praphamontripong 4 Entity-Relationship Model

• E-R diagram – high-level design model representing a database as a collection of entities and relationships among entities

name name address pid description

product makes company

buys works-for

person

address id name

Fall 2021 – University of Virginia © Praphamontripong 5 E-R Diagram: Building Blocks

(strong) Entity set

Attribute

Relationship

Weak entity

Subclass is a

Note: colors are not part of E-R Diagram. They simply are used to increase readability. Fall 2021 – University of Virginia © Praphamontripong 6 Entity Sets and Attributes

• Entity ~ an object (~ a in a ) • Entity set ~ a class (~ table, a collection of similar entities) • Attribute ~ a field (property of the entities in that set)

Every entity set An entity is an object that is should have a distinguishable from other primary key types of objects

person

address id name An attribute has Underline indicates that domain specifying the attribute is part of acceptable values the primary key

Fall 2021 – University of Virginia © Praphamontripong 7 Entity Sets and Attributes

• Entity ~ an object (~ a row in a table) • Entity set ~ a class (~ table, a collection of similar entities) • Attribute ~ a field (property of the entities in that set)

E-R model is a static concept, involving the structure of data and not the operations on data. Thus, no methods associated with an entity set

person

address id name

Fall 2021 – University of Virginia © Praphamontripong 8 Attributes

• Single-valued attribute – allows a single value • Multi-valued attribute – allows multiple values at the same time • Derived attribute –can be calculated from one or more attributes • Composite attribute – consists of multiple values

Multi-valued attribute

phone street Composite attribute city id person address state name age zipcode DOB Single-valued attribute Derived attribute

Fall 2021 – University of Virginia © Praphamontripong 9 Attributes

• Composite attribute – can have as many levels as needed

number

phone street name

city apt-number id person address state name age zipcode DOB

Fall 2021 – University of Virginia © Praphamontripong 10 Let’s try: Movie-Database Identify entity, entity sets, and attributes

Object Movies

Each movie has a title and year; title and year together uniquely identify the movie. Length and genre are maintained for each movie. Each movie is associated with a studioName which tells us the studio that owns the movie, and producerC# which is an integer that represents the producer of the movie.

Object MovieStars

MovieStars tells us something about stars. It maintains the name of the movie star, address, gender, and birthdate. The gender can be a single character (M or F). Birthday is of type “date,” which might be a character string of a special form.

Fall 2021 – University of Virginia © Praphamontripong 11 Let’s try: Movie-Database

Movies Movies Title Year Length Genre Mickey’s Club House 1939 231 drama

Awesome Minnie 1977 124 sciFi title genre Most wanted 1992 95 comedy year length

MovieStars name address gender birthdate Mickey 11 Somewhere, F 01/01/1911 MovieStars Charlottesville, VA, 22903 Minnie 22 Another place, M 02/02/1912 name birthdate Fairfax, VA, 22030 address gender Donald 33 Nowhere, F 03/03/1913 Charlottesville, 22911

Note: instances do not exist in E-R. These tables are only to help visualize the database being designed. Fall 2021 – University of Virginia © Praphamontripong 12 Note: Instances of E-R Diagram

E-R model is used to design a database. The database is not implemented. Therefore, the instance of E-R diagram never exists in the sense that a ’s instances exist in a relational model.

However, it is often useful to visualize the database being designed as if it existed.

Fall 2021 – University of Virginia © Praphamontripong 13 Relationships

• Connections among two or more entity sets

• Binary relationships – connections between two entity sets

• Multi-way relationships (u-ary) – connections involving more than two entity sets

Fall 2021 – University of Virginia © Praphamontripong 14 Binary Relationships

If A and B are sets, a relationship R is a subset of A X B

product company pid name description name address 11 Beyblade … Dreamwork … 22 Trolls … Hashbro … … … … Nyform …

name name address pid description

product makes company

Note: instances do not exist in E-R. These tables are only to help visualize the database being designed. Fall 2021 – University of Virginia © Praphamontripong 15 Cardinality (or Multiplicity)

A binary relationship can connect any member of one of its entity sets to any number of members of the other entity set

• One-to-one

• One-to-many

• Many-to-one

• Many-to-many

Fall 2021 – University of Virginia © Praphamontripong 16 Cardinality: One-to-One

product company pid name description name address 11 Beyblade … Dreamwork … 22 Trolls … Hashbro … … … … Nyform …

product makes company

Arrow pointing to the entity Arrow pointing to the entity that is constrained to one that is constrained to one

Each product can be made by at most one company. Each company can make at most one product.

“at most one” – Guarantee existence?

Note: instances do not exist in E-R. These tables are only to help visualize the database being designed. Fall 2021 – University of Virginia © Praphamontripong 17 Cardinality: One-to-Many

product company pid name description name address 11 Beyblade … Dreamwork … 22 Trolls … Hashbro … … … … Nyform …

product makes company

Arrow pointing to the entity that is constrained to one

Each product can be made by many companies. Each company can make at most one product.

“at most one” and “many” – Guarantee existence?

Note: instances do not exist in E-R. These tables are only to help visualize the database being designed. Fall 2021 – University of Virginia © Praphamontripong 18 Cardinality: Many-to-One

product company pid name description name address 11 Beyblade … Dreamwork … 22 Trolls … Hashbro … … … … Nyform …

product makes company

Arrow pointing to the entity that is constrained to one

Each product can be made by at most one company. Each company can make many products.

“at most one” and “many” – Guarantee existence?

Note: instances do not exist in E-R. These tables are only to help visualize the database being designed. Fall 2021 – University of Virginia © Praphamontripong 19 Cardinality: Many-to-Many

product company pid name description name address 11 Beyblade … Dreamwork … 22 Trolls … Hashbro … … … … Nyform …

product makes company

Each product can be made by many companies. Each company can make many products.

“many” – Guarantee existence?

Note: instances do not exist in E-R. These tables are only to help visualize the database being designed. Fall 2021 – University of Virginia © Praphamontripong 20 Let’s try: Cardinality (1) DrawKey an Constraints:E-R diagram Examples to model the given image sample • Many-to-one: An entity in A is associated with at most one entity in data.B. An entity Specify in B is associated the with cardinality.any number in A. Interpret the E-R diagram. Many people can be born in any county, but any individual is born in at most one country.

year name Capital

Was Bowling Born Country Club in was_born_in Note that we are not saying that the Sea Captain was not born in any country, he almost certainly was, we just dont know which country, or it is not in our Country entity set. Was born in Also note that we are not saying that no one was 12 born in Ireland, it is just that no one in the Bowling Club was.

(many-to-one)

Fall 2021 – University of Virginia © Praphamontripong 21 Participation Constraints Earlier, we saw an example of a one-to-one key constraint, noting that a man may be married to at most one woman, and a woman may be married to at most one man (both men and women can be unmarried). Suppose we Let’swant to build atry: database forCardinality the Springfield Married Persons (2) Association. In this case everyone must be married! In database terms their Draw an E-R diagram to model the given image sample participation must be total. (the previous case that allows unmarried people is data.said to have Specify partial participation.)the cardinality. Interpret the E-R diagram. How do we represent this with ER diagrams? (answer on next slide)

since name name

is_married_toIs Men Married Women to Is Married to 15

(one-to-one)

Fall 2021 – University of Virginia © Praphamontripong 22 Let’s try: Cardinality (3) Draw an E-R diagram to model the given image sample Key Constraints: Examples data. Specify the cardinality. Interpret the E-R diagram. • Many-to-many: Entities in A and B are associated with any number from each other.

Since name name

Is Girls Classmate Boys of is_clasmate_of

Is Classmate of

13

(many-to-many)

Fall 2021 – University of Virginia © Praphamontripong 23 Let’s try: Cardinality (4) DrawKey an Constraints:E-R diagram Examples to model the given image sample data.• One-to-many Specify: An entity in the A is associated cardinality. with any number Interpret in B. An the E-R diagram. entity in B is associated with at most one entity in A. A woman may be the mother of many (or no) children. A person may have at most one mother.

Born on name name

Is Women's Mother Low I.Q. Club of Club is_mother_of

Note that this example is not saying that Moe does not have a mother, since we know as a Is mother of biological fact that everyone has a mother. It is simply the case that Moe s mom11 is not a member of the Womens club.

(one-to-many)

Fall 2021 – University of Virginia © Praphamontripong 24 Total Participation

Total participation – all entities in an entity set must participate in the relationship

Double line indicates “total participation” (on the side that must participate)

product makes company

Every product must be made by at least one company. Each product can be made by many companies. Each company can make many products. Some companies may not make any product.

Fall 2021 – University of Virginia © Praphamontripong 25 Let’s try: Participation (1) Interpret the E-R diagram.

total participation Many-to-one

product makes company

Every product must be made by exactly one company. Each company can make many products. Some companies may not make any product.

Fall 2021 – University of Virginia © Praphamontripong 26 Let’s try: Participation (2) Interpret the E-R diagram.

total participation total participation

employee assigned to department

Every employee must be assigned to at least one department. Each employee can be assigned to many departments. Each department can have many employees. Every department must have at least one employee.

Fall 2021 – University of Virginia © Praphamontripong 27 Attributes on Relationships

• Relationships can have attributes

• The attributes have values only when the relationship occurs

name name address pid description

product makes company

Use dotted line to quantity connect an attribute to the relationship

Fall 2021 – University of Virginia © Praphamontripong 28 Let’s try: Attributes - Relationships

Interpret the E-R diagram.

sec_id semester name id total_credit cid year

course takes student

grade

Each student can take many courses. Each course can be taken by many students. A grade exists only when the student takes a course.

Fall 2021 – University of Virginia © Praphamontripong 29 Wrap-Up

• Database design process • Intro to E-R model • Entities and entity sets • Attributes: single-valued, multi-valued, derived, composite • Cardinality and participation

What’s next?

• Roles in relationships

• Relationships: binary, u-ary

• Weak entity

• Subclassing

• E-R to relational model

Fall 2021 – University of Virginia © Praphamontripong 30 Practice Activity: E-R Diagram

Pair any of the following entity sets. Come up with as many relationships as you can between the pairs. Specify the cardinality. Identify any mandatory relationships (total participation)

Person Car Student Office Manager Pizza Employee Laptop Cat Homework Dog Exam Te x t bo o k Hotdog Course Fruit

Fall 2021 – University of Virginia © Praphamontripong 31