43
Entity-Relationship Model Lecture 1 By Marina Barsky

Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

  • Upload
    doandat

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Entity-Relationship ModelLecture 1

By Marina Barsky

Page 2: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

• A data model is a collection of concepts for describing data.

• A schema is a description of a particular collection of data, using a given data model.

• A database instance is a collection of data compliant with the schema

Page 3: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Databases model the real world

• “Data Model” allows us to translate real world things into structures computers can store

• Many models exist: • Network• Hierarchical • Relational• Object-Oriented• XML

• etc.

Page 4: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

In relational model

• Database = set of named relations (or tables)

• Each relation has a set of named attributes (or columns)

• Each tuple (or row) has a value for each attribute

• Each attribute has a type (or domain)

• Relations are connected using keys

sid name login age gpa

53666 Jones jones@cs 18 3.4

53688 Smith smith@eecs 18 3.2

53650 Smith smith@math 19 3.8

sid cid grade

53666 Carnatic101 C

53666 Reggae203 B

53650 Topology112 A53666 History105 B

EnrolledStudents

Page 5: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Example: relation Movies

This relation describes movies:• Title

• Year when the movie was made

• Movie’s length

• Whether the movie was in color

Page 6: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Steps in Database Design

• Requirements Analysis• user needs; what must database do?

• Conceptual Design• high level modeling (often done w/ER diagrams)

• Logical Design• translate ER into DBMS data model

• Schema Refinement • consistency, normalization

• Physical refinement

• indexes, disk layout

• Security Design• who accesses what, and how

Page 7: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

ER Model: entity

• Entity: Real-world object, distinguishable from other objects. An entity is described using a set of attributes.

• Entity Set: A collection of similar entities. E.g., all employees.

• All entities in an entity set have the same set of attributes• Each entity set has a key (underlined).• Each attribute has a domain.

Employees

sinname

dept

Page 8: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

ER Model: relationship

• Relationship: Association among two or more entities. E.g., John works in Pharmacy department.

• relationships can have their own attributes.

• Relationship Set: Collection of similar relationships.

lot

name

Employees

sin

Works_In

sincedname

budgetdid

Departments

Page 9: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Conceptual design with entities and relationships

• What are the entities and relationships in the enterprise?

• What information about these entities and relationships

should we store in the database?

• What integrity constraints or business rules hold?

• A database `schema’ in the ER Model can be represented

pictorially (ER diagrams).

• Can easily map an ER diagram into a relational schema.

Page 10: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

ER Model: entity role

• Same entity set can participate in different relationship sets, or in different “roles” in the same set.

subor-

dinate

super-

visor

Reports_To

since

Works_In

dname

budgetdid

Departments

lot

name

Employees

sin

Page 11: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

E/R for movies: elements

Movies

Title

Stars-in

Entity – certain movie

Attributes

Relationships

The set of all movies constitutes an Entity set

Page 12: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Movies Stars-In Stars

length filmTypetitle year

Owns

name address

Studios name

address

Movies: Entity-Relationship diagram

Page 13: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Relationships

Movies StudiosOwns

Studios PresidentsRuns

Movies StarsStars-In

Page 14: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Multiplicity of Relationships

Movies StudiosOwns

Studios PresidentsRuns

Movies StarsStars-In

many-one

one-one

many-many

Arrow says:

“At most one”

Page 15: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Sometimes binary relationships

aren’t enough!

What could go wrong with this design?Which stars a studio is paying for a given movie?

Movies Stars-In Stars

Owns

Studios

No arrow

now!

Page 16: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Solution: Three-way relationship

Stars MoviesContracts

Studios

Page 17: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Example: Attributes on Relationships

Movies

title year

length filmType

Contracts

Stars

name addr

Studios

name addr

Page 18: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Example: Attributes on Relationships

Movies

title year

length filmType

Contracts

Stars

name addr

Studios

name addr

salary

Page 19: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Example: Roles in a relationship

o An entity set can appear two or more times in a relationship.

o Each line to the entity set represents a different role.

o A movie may have many sequels, but for each sequel there is at most

one original movie.

MoviesSequel-of

Original

Sequel

Page 20: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Another Example

Drinkers

Married

husband wife

Husband Wife

Bob Ann

Joe Sue

… …

Page 21: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Another role

Buddy1 Buddy2

Bob Ann

Joe Sue

Ann Bob

Joe Moe

… …Drinkers

Buddies

1 2

Page 22: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Design example 1. “Bars-Beer-Drinkers” (BBD)

• Bars sell some beers.• Drinkers like some beers.• Drinkers frequent some bars.

What would be the E/R diagram?

Page 23: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Drinkers addrname

Beers

manfname

Bars

name

license

addr

Note:license =beer, full,none

Sells Bars sell somebeers.

Likes

Drinkers likesome beers.Frequents

Drinkers frequentsome bars.

Why we need it?

BBD ER diagram

Page 24: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

BBD Multiway Relationship

• Suppose that drinkers prefer drink certain beers at certain bars (many bear types at multiple bars).

• How do we reflect their preferences in ER diagram?

Page 25: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Bars Beers

Drinkers

name nameaddr manf

name addr

license

Preferences

BBD Multi-way Relationship

Page 26: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

A typical relationship table for ternary (three-way) relationships

Bar Drinker Beer

Joe’s Bar Ann Miller

Sue’s Bar Ann Bud

Sue’s Bar Ann Pete’s Ale

Joe’s Bar Bob Bud

Joe’s Bar Bob Miller

Joe’s Bar Cal Miller

Sue’s Bar Cal Bud Lite

Page 27: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Multiple relationships between two entity sets

Drinkers BeersLikes

Favorite

Page 28: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

“Exactly one” Multiplicity

Manfs BeersBest-

seller

• Some beers are not the best-seller of any manufacturer, so a rounded arrow to Manfs would be inappropriate.

• But a manufacturer has to have a best-seller (in our model)

‘Exactly one’ arrow ‘At most one’ arrow

Page 29: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Design choices

• Should a concept be modeled as an entity or an attribute?

• Should a concept be modeled as an entity or a relationship?

• Identifying relationships: binary or ternary?

Page 30: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Entity vs. Attribute

• Should address be an attribute of Employees or an entity (related to Employees)?

• Depends upon how we want to use address information, and the semantics of the data:

• If we have several addresses per employee, addressmust be an entity (since attributes cannot be set-valued).

• If the structure (city, street, etc.) is important, addressmust be modeled as an entity (since attribute values are atomic).

• If the lifetime of the address differs from the entity, address must be modeled as an entity (since attributes are deleted with their entity).

Page 31: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Exercise 1. Bank database

• Let us design a database for a bank, including information about customers and their accounts.

Information about a customer includes their name, address, phone, and SIN number. Accounts have numbers, types (e.g., savings, checking) and balances. We also need to record the customer(s) who own an account. Draw the E/R diagram for this database.

Page 32: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Customers Owns Accounts

phone SINname addr number type balance

Exercise 1 solution

Page 33: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Exercise 1A. Bank database

• Modify your solution as follows:a) Change your diagram so an account can belong to only

one customer.

Page 34: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Exercise 1A solution

Customers Owns Accounts

phone SINname addr number type balance

Page 35: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Exercise 1B. Bank database

• Modify your solution as follows:a) Change your diagram so an account can have only one

customer.b) Change your diagram so that a customer can have a set

of addresses (which are street-city-province triples) and a set of phones.

Remember that we do not allow attributes to have non-atomic types, such as sets, in the E/R model.

Page 36: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Customers Owns Accounts

SINname

Located-

at

number type

Addresses

citystreet

balance

province

Phones

typenumber

Reached-by

Exercise 1B solution

Page 37: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Exercise 1C. Bank database

• Modify your solution as follows:a) Change your diagram so an account can have only one

customer.b) Change your diagram so that a customer can have a set

of addresses (which are street-city-province triples) and a set of phones.

c) Further modify your diagram so that customers can have a set of addresses, and at each address there is a set of phones.

Page 38: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Customers Owns Accounts

SINname

Located-

at

number type

Addresses

citystreet

balance

province

Phones

typenumber

Has

Exercise 1C solution

Page 39: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Exercise 2. Teams and fans database• Give an E/R diagram for a database recording information

about teams, players, and their fans, including:

• For each team, its name, its players, its team captain (one of its players), and the colors of its uniform.

• For each player, his/her name.• For each fan, his/her name, favorite teams, favorite

players, and favorite color.

Page 40: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Teams

teamName

Colors

Players

Fansname

memberOf captainOf

wears rootsFor

prefers

name

Exercise 2 solution (Variant I)

Page 41: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Teams

teamName

Colors

Players

Fans

name

memberOf captainOf

wears

favors

name

Exercise 2 solution (Variant II)

Page 42: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Exercise 2A. Teams and fans database• Modification A:

• Suppose we wish to add to the schema a relationship “Led-by” among two players and a team. The intention is that this relationship set consists of triples

(player1, player2, team)

such that player 1 played on the team at a time when some

other player 2 was the team captain.

Draw the modification to the E/R diagram.

Page 43: Entity-Relationship Model - University of Toronto Model Lecture 1 By Marina Barsky •A data model is a collection of concepts for describing data. •A schema is a description of

Teams

teamName

Players

name

memberOf captainOf

wears rootsFor

leadBy period

Exercise 2A solution