0% found this document useful (0 votes)
30 views41 pages

01 01 EntityRelationship

The document provides an overview of the Entity-Relationship (E/R) model used for database design, including definitions of data models, schemas, and database instances. It discusses the process of creating relational databases, the structure of relations, and the importance of identifying entities and relationships. Various examples illustrate the design choices and complexities involved in modeling real-world scenarios using E/R diagrams.

Uploaded by

merntechwizard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views41 pages

01 01 EntityRelationship

The document provides an overview of the Entity-Relationship (E/R) model used for database design, including definitions of data models, schemas, and database instances. It discusses the process of creating relational databases, the structure of relations, and the importance of identifying entities and relationships. Various examples illustrate the design choices and complexities involved in modeling real-world scenarios using E/R diagrams.

Uploaded by

merntechwizard
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

By Marina Barsky

Entity-Relationship Model
Lecture 1
Useful definitions
• A data model is a collection of concepts for describing data.

• A schema is a description of a particular collection of data,


using concepts of a given data model.

• A database instance is a collection of data compliant with


the schema
Database Design
• Designing a database:
• what information the database must hold, and
• what relationships are there among components of
that information.

• Notation for expressing designs: Entity-Relationship (E/R)


model
Process of creating relational
database

Relational Physical
Ideas ER design
schema database

Abstract Concrete
design design
Databases model the real world
• “Data Model” allows us to translate real world things into
structures computers can store

• Many models exist:


• Relational
• Object-Oriented
• XML
• Semantic
• Etc.
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
Enrolled
sid cid grade Students
sid name login age gpa
53666 Carnatic101 C
53666 Jones jones@cs 18 3.4
53666 Reggae203 B
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8
ER Model: entity
• Entity: a single real-world
object, distinguishable from
other objects. An entity is name
described using a set of sin dept
attributes.
• Entity Set: A collection of similar Employees
entities. E.g., all employees.
• All entities in an entity set have
the same set of attributes
• Each entity set has a key
(underlined).
ER Model: relationship
since
name dname
sin lot did budget

Employees Works_In Departments

• 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.
Collecting ideas
• 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?


E/R for movies: elements
Entity – certain movie Movies

The set of all movies constitutes an Entity set

Attributes Title

Relationships Stars-in
Movies: Entity-Relationship diagram

title year length filmType name address

Movies Stars-In Stars

Owns

Studios name

address
Relationships

Movies Owns Studios

Studios Runs Presidents

Movies Stars-In Stars


Multiplicity of Relationships

many-one Movies Owns Studios


Arrow says:
“At most one”

one-one Studios Runs Presidents

many-many Movies Stars-In Stars


Sometimes binary relationships
aren’t enough!

Movies Stars-In Stars

Owns No arrow
now!

Studios

What could go wrong with this design?


Which stars a studio is paying for a given movie?
Solution: Three-way relationship

Stars Contracts Movies

Studios
Example: Attributes on Relationships

title year name addr


salary

Movies Stars
Contracts

length filmType

Studios

name addr
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.

Original

Sequel-of Movies

Sequel

o A movie may have many sequels, but for each sequel there is at most
one original movie.
Another design example.
“Bars-Beer-Drinkers” (BBD)
• Bars sell some beers.
• Drinkers like some beers.
• Drinkers frequent some bars.

What would be the E/R diagram?


BBD ER diagram
name addr name manf

Bars Sells Beers Bars sell some


beers.
license
Drinkers like
Note: Frequents Likes some beers.
license =
beer, full,
none Drinkers frequent
some bars.
Drinkers
name addr

Why we need it?


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?


BBD Multi-way Relationship
name addr name manf

license Bars Beers

Preferences

Drinkers

name addr
Example 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
Multiple relationships between two
entity sets

Drinkers Likes Beers

Favorite
“Exactly one” Multiplicity

Manfs Best- Beers


seller

‘At most one’ arrow ‘Exactly one’ arrow

• 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)


Roles

Married
Husband Wife
Bob Ann
husband wife Joe Sue
Drinkers … …
Another role

Buddies Buddy1 Buddy2


Bob Ann
Joe Sue
1 2 Ann Bob
Drinkers Joe Moe
… …
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?
Entity vs. Attribute
• Should address be an attribute of Employees or an entity
(related to Employees)?
• Depends upon the semantics of the data:
• If we have several addresses per employee, address
must be an entity (since attributes cannot be set-
valued).
• If the structure (city, street, etc.) is important,
address must 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).
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 SSN 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.
Exercise 1 solution

name addr phone SSN number type balance

Customers Owns Accounts


Exercise 1A. Bank database
• Modify your solution as follows:
a) Change your diagram so an account can belong to only
one customer.
Exercise 1A solution

name addr phone SSN number type balance

Customers Owns Accounts


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-state 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.
Exercise 1B solution
name SSN number type balance

Customers Owns Accounts

Located-
at
Reached-by

Addresses Phones

street city state number type


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-state 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.
Exercise 1C solution
name SSN number type balance

Customers Owns Accounts

Located-
at

Addresses Has Phones

street city state number type


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.
Exercise 2 solution (Variant I)

teamName

Teams

wears rootsFor
memberOf captainOf

Players
name
Colors name Fans

prefers
Exercise 2 solution (Variant II)
teamName

Teams

wears
memberOf captainOf

name
Players
Colors favors
name
Fans
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.
Exercise 2A solution
teamName

Teams
wears rootsFor

memberOf captainOf

leadBy period

Players
name

You might also like