2-Lect-ERD (1)

Содержание

Слайд 2

The Road Ahead

E/R Diagram

Introduction to Entity-Relationship (ER) Diagrams
Entity
Relationship

The Road Ahead E/R Diagram Introduction to Entity-Relationship (ER) Diagrams Entity Relationship
Constraints
Subclasses
Weak Entity Sets
ER Design Principle
Translating an ER Diagram into a Relational Scheme Design

Слайд 3

A real database may have a large number of tables…

Imagine that you

A real database may have a large number of tables… Imagine that
are asked to design a database like this….
How would you approach the problem?

Слайд 4

Designing a Database for an Application

Conceptually model the requirements of the application
What

Designing a Database for an Application Conceptually model the requirements of the
are the things that need to be stored?
How do they interact with each other?
Tool to use: Entity-Relationship (ER) Diagrams
for modelling
Translate the conceptual model into a set of tables
Create the tables with a DBMS

Слайд 5

ER Diagram

Companies

country

name

Persons

addr

name

Products

name

category

price

Buy

Employ

Make

ER Diagram Companies country name Persons addr name Products name category price Buy Employ Make

Слайд 6

ER Diagram

Rectangle = Entity
Entity = Real-world object (e.g., a bar)
Entity Set

ER Diagram Rectangle = Entity Entity = Real-world object (e.g., a bar)
= Collection of similar objects (e.g., a set of bars)

Companies

Persons

Products

Слайд 7

ER Diagram

Oval = Attribute = Property of an entity set

Companies

country

name

Persons

addr

name

Products

name

category

price

ER Diagram Oval = Attribute = Property of an entity set Companies

Слайд 8

ER Diagram

Diamond = Relationship = Connection between two entity sets

Companies

country

name

Persons

addr

name

Products

name

category

price

Buy

Employ

Make

ER Diagram Diamond = Relationship = Connection between two entity sets Companies

Слайд 9

ER Diagram

Diamond = Relationship = Connection between two entity sets
Persons buy products

Persons

addr

name

Products

name

category

price

Buy

ER Diagram Diamond = Relationship = Connection between two entity sets Persons

Слайд 10

ER Diagram

Diamond = Relationship = Connection between two entity sets
Companies make products

Companies

country

name

Products

name

category

price

Make

Why

ER Diagram Diamond = Relationship = Connection between two entity sets Companies
is there an arrow?

Слайд 11

Types of Relationships

Many-to-Many Relationships
Many-to-One Relationships
One-to-One Relationships

Types of Relationships Many-to-Many Relationships Many-to-One Relationships One-to-One Relationships

Слайд 12

Many-to-Many Relationship

One person can buy multiple products
One product can be bought by

Many-to-Many Relationship One person can buy multiple products One product can be
multiple persons

Persons

addr

name

Products

name

category

price

Buy

Слайд 13

Many-to-One Relationship

One company can make multiple products
But one product can only be

Many-to-One Relationship One company can make multiple products But one product can
made by one company (at most)

Companies

country

name

Products

name

category

price

Make

Слайд 14

One-to-One Relationship

A city can be the capital of only one country
A country

One-to-One Relationship A city can be the capital of only one country
can have only one capital city

Countries

Cities

Capital of

Слайд 15

Multi-way Relationships

What if we want to record the store from which the

Multi-way Relationships What if we want to record the store from which
person buys the product?
We can use a 3-way relationship

Persons

Products

Buy

Stores

Слайд 16

From Multi-way to Binary

Persons

Products

Buy

Stores

Persons

Products

Buyer of

Stores

Transactions

Product of

Store of

Multiple many-to-1 binary relationship

From Multi-way to Binary Persons Products Buy Stores Persons Products Buyer of

Слайд 17

Exercise

Each player prefers only one game
One player can prefer one game
One game

Exercise Each player prefers only one game One player can prefer one
can be preferred by many players
P-G Many-to-One
Many-to-many?
Many-to-one?
One-to-one?

Games

Players

Prefer

Слайд 18

Exercise

No two shops sell the same product
One product can be sold by

Exercise No two shops sell the same product One product can be
one shop
One shop can sell many products
P-G One-to-One
Many-to-many?
Many-to-one?
One-to-one?

Shops

Products

Sell

Слайд 19

Exercise

Any two players are from two different countries
One player can be from

Exercise Any two players are from two different countries One player can
one country
One country can have one player only
P-G One-to-One
Many-to-many?
Many-to-one?
One-to-one?

Countries

Players

from

Слайд 20

One More Thing about Relationships

A relationship can have its own attribute

Persons

addr

name

Products

name

category

price

Buy

One More Thing about Relationships A relationship can have its own attribute

Слайд 21

One More Thing about Relationships

A relationship can have its own attribute
If we

One More Thing about Relationships A relationship can have its own attribute
want to record the date of the purchase

Persons

addr

name

Products

name

category

price

Buy

date

Слайд 22

Roles

Sometimes an entity set may appear more than once in a relationship
Example:

Roles Sometimes an entity set may appear more than once in a
some persons are married to each other
The role of the person is specified on the edge connecting the entity set to the relationship

Persons

Married

Husband

Wife

Слайд 23

Roles

Question: A relationship connects an entity set to itself, does it have

Roles Question: A relationship connects an entity set to itself, does it
to be one-to-one?
Answer: No

Слайд 24

Roles

Question: A relationship connects an entity set to itself, does it have

Roles Question: A relationship connects an entity set to itself, does it
to be one-to-one?
Answer: No
Example above:
One employee has only one supervisor, but may have many supervisee

Employee

Supervise

Supervisor

Supervisee

Слайд 25

Roadmap

Constraints
Subclasses
Weak Entity Sets
ER Design Principle
Translating an ER Diagram into a Relational Scheme

Roadmap Constraints Subclasses Weak Entity Sets ER Design Principle Translating an ER
Design

Слайд 26

Constraints

Some conditions that entity sets and relationships should satisfy
We will focus on

Constraints Some conditions that entity sets and relationships should satisfy We will
three types of constraints
Key constraints
Referential integrity constraints
Degree constraints

Слайд 27

Key

One or more attributes that are underlined
Meaning: They uniquely represent each entity

Key One or more attributes that are underlined Meaning: They uniquely represent
in the entity set
Example: The names uniquely represent the persons
i.e., each person must have a unique name

Persons

addr

name

Слайд 28

Key

One or more attributes that are underlined
Meaning: They uniquely represent each entity

Key One or more attributes that are underlined Meaning: They uniquely represent
in the entity set
Example: Each product has a unique name

Products

name

category

price

Слайд 29

Key

One or more attributes that are underlined
What now?
Each product has a unique

Key One or more attributes that are underlined What now? Each product
combination
But there can be products with the same name, or the same category, but not both
Example
Name = “Apple”, Category = “Fruit”, Price = “1”
Name = “Apple”, Category = “Phone”, Price = “888”

Products

name

price

category

Слайд 30

Key

Rule: Every entity set should have a key
So that we can uniquely

Key Rule: Every entity set should have a key So that we
refer to each entity in the entity set

Products

name

price

category

Слайд 31

Referential Integrity

One company may make multiple products
One product is made by one

Referential Integrity One company may make multiple products One product is made
company
Can there be a product that is not made by any company?
No.
i.e., every product must be involved in the Make relationship
This is called a referential integrity constraint.
How do we specify this in an ER diagram?
Use a rounded arrow instead of a pointed arrow

Products

Companies

Make

Слайд 32

Referential Integrity

Products

Companies

Make

One company may make multiple products
One product is made by one

Referential Integrity Products Companies Make One company may make multiple products One
company
Can there be a product that is not made by any company?
No.
i.e., every product must be involved in the Make relationship
This is called a referential integrity constraint.
How do we specify this in an ER diagram?

Слайд 33

Referential Integrity: Exercise

A city can be the capital of only one country
A

Referential Integrity: Exercise A city can be the capital of only one
country must have a capital

Countries

Cities

Capital of

Слайд 34

Referential Integrity: Exercise

A company must hire at least one person
A person must

Referential Integrity: Exercise A company must hire at least one person A
be hired by exactly one company

Companies

Persons

Hire

Слайд 35

Referential Integrity

Products

Companies

Make

What if every company should make at least one product?
In

Referential Integrity Products Companies Make What if every company should make at
general, a referential integrity constraint can only apply to the “one” side of
A many-to-one relationship, or
A one-to-one relationship
For the “many” side, there is another type of constraints to use

Слайд 36

Degree Constraint

Each company should make at least 1 product

Products

Companies

Make

≥ 1

Degree Constraint Each company should make at least 1 product Products Companies Make ≥ 1

Слайд 37

Degree Constraint

Each company can make at most 1000 product
Note
Not required in

Degree Constraint Each company can make at most 1000 product Note Not
the exam
Key and referential integrity constraints can be easily enforced in a DBMS
Degree constraints are not easy to enforce

Products

Companies

Make

≤ 1000

Слайд 38

Roadmap

Constraints
Subclasses
Weak Entity Sets
ER Design Principle
Translating an ER Diagram into a Relational Scheme

Roadmap Constraints Subclasses Weak Entity Sets ER Design Principle Translating an ER
Design

Слайд 39

Subclass

PhDs are a special type of Students
Subclass = Special type
The connection between

Subclass PhDs are a special type of Students Subclass = Special type
a subclass and its superclass is captured by the isa relationship, which is represented using a triangle
Key of a subclass = key of its superclass
Example: Key of Phds = Students.ID
Students is referred to as the superclass of PhDs

PhDs

advisor

Students

ID

name

isa

Слайд 40

Subclass

An entity set can have multiple subclasses
Example
Superclass: Computers
Subclass 1: Desktop
Subclass 2: Laptop

Desktops

Computers

name

price

Subclass An entity set can have multiple subclasses Example Superclass: Computers Subclass
isa

Laptops

isa

Слайд 41

Weak Entity Sets

Weak entity sets are a special type of entity sets

Weak Entity Sets Weak entity sets are a special type of entity
that
cannot be uniquely identified by their own attributes
needs attributes from other entities to identify themselves
Example: Cities in USA
Problem: there are cities with identical names

Cities

name

population

Слайд 43

Weak Entity Sets

Problem: there are cities with identical names
Observation: cities in the

Weak Entity Sets Problem: there are cities with identical names Observation: cities
same state would have different names
Solution: make Cities a weak entity set associated with the entity set States : Double-lined rectangle
The relationship In is called the supporting relationship of Cities : Double-lined diamond
The key of Cities = (State.name, Cities.name)
Zero or more of its own attributes
Key attributes from entity sets that are reached by supporting relationships to other entity sets

Cities

name

population

States

name

GDP

In

Слайд 44

Exercise

Consider two entity sets: Players and Teams
Each player has a name and

Exercise Consider two entity sets: Players and Teams Each player has a
a number
Each team has a name and a manager
Each player plays for exactly one team, and is uniquely identified within the team by his/her number
Each team is uniquely identified by its name
Different players may have the same name
Draw a ER diagram that captures the above statements
What is the key of Players?

Слайд 45

Road Map

Design Principle of ER Diagrams
Translating an ER Diagram into a Relational

Road Map Design Principle of ER Diagrams Translating an ER Diagram into a Relational Scheme Design
Scheme Design

Слайд 46

Design Principle 1: Be Faithful

Be faithful to the specifications of the application
Capture

Design Principle 1: Be Faithful Be faithful to the specifications of the
the requirements as much as possible

Слайд 47

Design Principle 2: Avoid Redundancy

Avoid repetition of information
Example
Problems that can be caused

Design Principle 2: Avoid Redundancy Avoid repetition of information Example Problems that
by redundancy
Waste of space
Possible inconsistency

Faculties

ID

name

Students

ID

name

mentor

Mentor

Слайд 48

Design Principle 3: Keep It Simple

Each student is mentored by one faculty
One

Design Principle 3: Keep It Simple Each student is mentored by one
faculty can mentor multiple students
We also record the number of times that a mentee meets with his/her mentor
Design below: Not wrong, but can be simplified

Faculties

Students

Mentor

Meet

number

Слайд 49

Design Principle 3: Keep It Simple

Each student is mentored by one faculty
One

Design Principle 3: Keep It Simple Each student is mentored by one
faculty can mentor multiple students
We also record the number of times that a mentee meets with his/her mentor
Better Design:

Faculties

Students

Mentor

number

Слайд 50

Design Principle 3: Keep It Simple

There are four types of students: Year

Design Principle 3: Keep It Simple There are four types of students:
1, Year 2, Year 3, Year 4
Design below: Not wrong, but can be simplified

Students

Year 4

isa

Year 3

isa

Year 2

isa

Year 1

isa

Слайд 51

Design Principle 3: Keep It Simple

There are four types of students: Year

Design Principle 3: Keep It Simple There are four types of students:
1, Year 2, Year 3, Year 4
Better Design

Students

year

Слайд 52

Tips: When to Use Subclasses

Case 1: When a subclass has some attribute

Tips: When to Use Subclasses Case 1: When a subclass has some
that is absent from the superclass

PhDs

advisor

Students

ID

name

isa

Case 2: When a subclass has its own relationship with some other entity sets

Part-Time

Students

isa

Companies

Employ

Слайд 53

Design Principle 4: Don’t Over-use Weak Entity Sets

Too many entity sets

Design Principle 4: Don’t Over-use Weak Entity Sets Too many entity sets
that should not be “weak”

States

Countries

In

Cities

In

Universities

In

Departments

In

Students

In

Слайд 54

Road Map

Design Principle of ER Diagrams
Translating an ER Diagram into a Relational

Road Map Design Principle of ER Diagrams Translating an ER Diagram into a Relational Scheme Design
Scheme Design

Слайд 55

ER Diagram ? Relational Schema

Products (name, price)
Persons (name, addr)
Buy (product_name, person_name)
Terminology
A relation

ER Diagram ? Relational Schema Products (name, price) Persons (name, addr) Buy
schema = the name of a table + names of its attributes
A database schema = a set of relation schemas

Persons

addr

name

Products

name

price

Buy

Слайд 56

ER Diagram ? Relational Schema

General rules:
Each entity set becomes a relation
Each many-to-many

ER Diagram ? Relational Schema General rules: Each entity set becomes a
relationship becomes a relation
Special treatment needed for:
Weak entity sets
Subclasses
Many-to-one and one-to-one relationships

Слайд 57

Entity Set ? Relation

Each entity set is converted into a relation that

Entity Set ? Relation Each entity set is converted into a relation
contains all its attributes
The key of the relation = the key of the entity set

Bars

name

license

addr

ID

Bars

Слайд 58

Many-to-Many Relationship ? Relation

Converted into a relation that contains
all keys of

Many-to-Many Relationship ? Relation Converted into a relation that contains all keys
the participating entity sets, and
the attributes of the relationship (if any)
Normally, Key of relation = Keys of the participating entity sets

Sell

Beers

Bars

Sell

ID

name

ID

name

price

Слайд 59

Many-to-Many Relationship ? Relation

If an entity is involved multiple times in a

Many-to-Many Relationship ? Relation If an entity is involved multiple times in
relationship
Its key will appear in the corresponding relation multiple times
The key is re-named according to the corresponding role

Follow

Persons

Follow

Followee

Follower

ID

name

Слайд 60

Weak Entity Set ? Relation

Each weak entity set is converted to a

Weak Entity Set ? Relation Each weak entity set is converted to
relation that contains
all of its attributes, and
the key attributes of the supporting entity set
The supporting relationship is ignored

Cities

name

population

States

name

GDP

In

Cities

Слайд 61

Subclass ? Relation

There are three different ways
The ER approach
The OO approach
The NULL

Subclass ? Relation There are three different ways The ER approach The
approach

Cartoon

Is_3D

Movies

ID

name

isa

Sci-Fi

isa

Is_Adoption

Слайд 62

The ER approach

One relation for each entity set
Movies( ID, name )
Cartoon( ID,

The ER approach One relation for each entity set Movies( ID, name
Is_3D )
Sci-Fi( ID, Is_Adoption )
A record may appear in multiple relations

Cartoon

Is_3D

Movies

ID

name

isa

Sci-Fi

isa

Is_Adoption

Слайд 63

The OO approach

One relation for each entity set and each possible subclass

The OO approach One relation for each entity set and each possible
combination
Movies( ID, name )
Cartoon( ID, name, Is_3D )
Sci-Fi( ID, name, Is_Adoption )
Sci-Fi-Cartoon( ID, name, Is_3D, Is_Adoption )
Each record appears in only one relation

Cartoon

Is_3D

Movies

ID

name

isa

Sci-Fi

isa

Is_Adoption

Слайд 64

The NULL approach

One relation that includes everything
Movies( ID, name, Is_3D, Is_Adoption )
For

The NULL approach One relation that includes everything Movies( ID, name, Is_3D,
non-cartoon movies, its “Is_3D” is set to NULL
For non-sci-fi movies, its “Is_Adoption” is set to NULL

Cartoon

Is_3D

Movies

ID

name

isa

Sci-Fi

isa

Is_Adoption

Слайд 65

Which Approach is the Best?

It depends
The NULL approach
Advantage: Needs only one relation
Disadvantage:

Which Approach is the Best? It depends The NULL approach Advantage: Needs
May have many NULL values
The OO approach
Advantage: Good for searching subclass combinations
Disadvantage: May have too many tables
The ER approach
A middle ground between OO and NULL

Слайд 66

ER Diagram ? Relational Schema

General rules:
Each entity set becomes a relation
Each many-to-many

ER Diagram ? Relational Schema General rules: Each entity set becomes a
relationship becomes a relation
Special treatment needed for:
Weak entity sets
Subclasses
Many-to-one and one-to-one relationships

Слайд 67

Many-to-One Relationship ? Relation

Intuitive translation:
Products( Pname, price )
Companies( Cname, country )
Make( Pname,

Many-to-One Relationship ? Relation Intuitive translation: Products( Pname, price ) Companies( Cname,
Cname )
Observation: in “Make”, each Pname has only one Cname
Simplification: Merge “Make” and “Products”
Results:
Products( Pname, price, Cname )
Companies( Cname, country )

Companies

country

name

Products

name

price

Make

Слайд 68

Many-to-One Relationship ? Relation

In general, we do not need to create a

Many-to-One Relationship ? Relation In general, we do not need to create
relation for a many-to-one relationship
Instead, we only need to put the key of the “one” side into the relation of the “many” side

Companies

country

name

Products

name

price

Make

Слайд 69

Many-to-One Relationship ? Relation

Only need to put the key of the “one”

Many-to-One Relationship ? Relation Only need to put the key of the
side into the relation of the “many” side

Students

name

ID

School

name

addr

In

Translation:
School( Hname, addr )
Students( ID, Sname, Hname )

Слайд 70

One-to-One Relationship ? Relation

No need to create a relation for a one-to-one

One-to-One Relationship ? Relation No need to create a relation for a
relationship
Only need to put the key of one side into the relation of the other
Solution 1
Cities( TID, Tname )
Countries( Cname, pop, TID)
Solution 2
Cities( TID, Tname, Cname )
Countries( Cname, pop )

Countries

Cities

Capital of

ID

name

name

pop

Имя файла: 2-Lect-ERD-(1).pptx
Количество просмотров: 37
Количество скачиваний: 0