You are on page 1of 103

CSE 303 Database Ashikur Rahman

CSE 303: Database

Lecture 12

2022
Chapter 4: E-R diagram
CSE 303 Database Ashikur Rahman

Outline

E/R Basics: Entities & Relations

E/R Design considerations

Advanced E/R Concepts


CSE 303 Database Ashikur Rahman

1. E/R Basics: Entities & Relations

3
CSE 303 Database Ashikur Rahman

What you will learn about in this section

High-level motivation for the E/R model

Entities

Relationships

4
CSE 303 Database Ashikur Rahman

Database Design
• Database design: Why do we need it?
• Agree on structure of the database before deciding on a particular implementation

• Consider issues such as:


• What entities to model
• How entities are related
• What constraints exist in the domain
• How to achieve good designs

• Several formalisms exist


• We discuss one flavor of E/R diagrams

5
CSE 303 Database Ashikur Rahman

Database Design Process


1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.

1. Requirements analysis Technical and non-


technical people are
• What is going to be stored? involved

• How is it going to be used?

• What are we going to do with the data?


Taught in course on system
• Who should access the data? analysis and design
6
CSE 303 Database Ashikur Rahman

Database Design Process


1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.

2. Conceptual Design

• A high-level description of the database

• Sufficiently precise that technical people can understand it

• But, not so precise that non-technical people can’t participate

This is where E/R diagram fits in. 7


CSE 303 Database Ashikur Rahman

Database Design Process


1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.

3. More:
• Logical Database Design
• Translation of ER diagram to a relational database schema (description of tables)

• Physical Database Design

• Security Design

8
CSE 303 Database Ashikur Rahman

Database Design Process


1. Requirements Analysis 2. Conceptual Design 3. Logical, Physical, Security, etc.

This process is
E/R Model & Diagrams used
iterated many
name times
name category
price
Product Makes Company

E/R diagram is a visual syntax for DB design which is precise enough for
technical points, but abstracted enough for non-technical people
9
CSE 303 Database Ashikur Rahman

Requirement Engineering
 Tasks and techniques that lead to an understanding of
requirements is called requirement engineering.
 Requirement engineering provides appropriate mechanism for understanding

• What customer wants


• Analyzing needs
• Assessing feasibility
• Negotiating a reasonable solution
• Specifying solution unambiguously
• Validating the specification
• Managing requirements
CSE 303 Database Ashikur Rahman

Requirements Engineering is Hard


“The seeds of major software
“The hardest single part of building a disasters are usually shown in the
software system is deciding what to build. No first three months of commencing
part of the work so cripples the resulting the software project.”
system if done wrong.”

"Fred" Brooks Jr. is an American


computer architect, software
engineer, and computer scientist, Capers Jones is an
best known for managing the American specialist
development of IBM's System/360 in software
family of computers engineering
methodologies
CSE 303 Database Ashikur Rahman

Failures Requirements Definition/Importance Requirements Types Development Process

What is the right system to build ?


CSE 303 Database Ashikur Rahman

Failures Requirements Definition/Importance Requirements Types Development Process

What is the right system to build ?

1
Prof. Ashikur Rahman
3
CSE 303 Database Ashikur Rahman

Requirement Analysis: Example


• For actors and directors, we want to store their name, a unique identification
number, address and birthday (why not age?)
• For actors, we also want to store a photograph
• For films, we want to store the title, year of production and type (thriller,
comedy, etc.)
• We want to know who directed and who acted in each film. Every film has one
director. We store the salary of each actor for each film
• Etc…
CSE 303 Database Ashikur Rahman

Next Step: E/R modeling


• Goal of modeling is to translate informal requirements to a precise diagram.
This diagram can then be translated into the desired data model, to allow
data to be stored in a database

Requirem Relational
Relational
ent E/R Diagram Database
DBMS
Analysis Schema

Gather informal Translate to Translate to Store data in a


requirements (from precise diagram relational database!
user) database schema
CSE 303 Database Ashikur Rahman

Interlude: Impact of the ER model


• The E/R model is one of the most cited articles in Computer
Science
• “The Entity-Relationship model – toward a unified view of data” Peter Chen,
1976

• Used by companies big and small


• You’ll know it soon enough

16
CSE 303 Database Ashikur Rahman

Basic Concepts:
Entities, Attributes, Relationships
CSE 303 Database Ashikur Rahman

Entities and Entity Sets


• Entity: An object in the world that can be distinguished from other objects

• Entity set: A set of similar entities.

Examples: Entity sets

PERSON Example : Student, Employee


PLACE Example : Country, Branch
OBJECT Example : Product, Building
EVENT Example : Student Registration, Payment
CONCEPT Example : Course, Order

18
CSE 303 Database Ashikur Rahman

Entities and Entity Sets

• Entity set  Class or type


• Nouns in the problem domain  entity sets
Product
• Entity  Object of a class Person

• Only Entity sets are modeled and not the entities


These represent entity sets
• Entity sets are drawn as rectangles in the E/R diagram

19
CSE 303 Database Ashikur Rahman

Attributes
• Attributes  Used to describe an entity
• All entities in the set have the same attributes
• An attribute contains single piece of information (and not a list of data)
• Attributes are represented by ovals attached to an entity set

name category Shapes are important. Colors


are not.
price
Product
20
CSE 303 Database Ashikur Rahman

Entities, Entity Sets and Attributes


Entities are not explicitly
Example:
represented in E/R diagrams!

Entity
name category

Name: Xbox
price
Category: Total Name: Lightning Mcqueen Entity
Category: Toy Product
Multimedia System
Price: $250 Price: $25
Attribute

Product

Entity Set

21
CSE 303 Database Ashikur Rahman

Keys
• A key is a minimal set of attributes that uniquely identifies an entity.

Denote elements of the primary Here, {name, category} is not a key (it
key by underlining. is not minimal).

name category
price
Product

The E/R model forces us to designate a single primary key, though there
may be multiple candidate keys
22
CSE 303 Database Ashikur Rahman

The R in E/R Diagram: Relationships


• A relationship is association among two or more entities
• Relationships are drawn using diamond

name name
category
price
Product Makes Company

• Verbs in the problem domain  relationship or relationship set

23
CSE 303 Database Ashikur Rahman

name category
name

price
makes Company
Product

stockprice

buys employs

Person

address name ssn


24
CSE 303 Database Ashikur Rahman

What is a Relationship?
• A mathematical definition:
B=
• Let A, B be sets A= 1 a
• A={1,2,3}, B={a,b,c,d} b
2
c
3
d

25
CSE 303 Database Ashikur Rahman

What is a Relationship?
• A mathematical definition:
B=
• Let A, B be sets A= 1 a
• A={1,2,3}, B={a,b,c,d} b
2

• A x B (the cross-product) is the set of all pairs (a,b) c


3
• A  B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d), d
(3,a), (3,b), (3,c), (3,d)}

26
CSE 303 Database Ashikur Rahman

What is a Relationship?
• A mathematical definition:
B=
• Let A, B be sets A= 1 a
• A={1,2,3}, B={a,b,c,d} b
2

• A x B (the cross-product) is the set of all pairs (a,b) c


3
• A  B = {(1,a), (1,b), (1,c), (1,d), (2,a), (2,b), (2,c), (2,d), d
(3,a), (3,b), (3,c), (3,d)}

• We define a relationship to be a subset of A x B


• R = {(1,a), (2,c), (2,d), (3,b)}

27
CSE 303 Database Ashikur Rahman

What is a Relationship?
• A mathematical definition:
• Let A, B be sets B=
• A x B (the cross-product) is the set of all pairs A= 1 a
• A relationship is a subset of A x B 2 b
c
• Makes is a relationship-it is a subset of 3
Product  Company: d

makes Company
Product

28
CSE 303 Database Ashikur Rahman

What is a Relationship?

name name
category
price
Product Makes Company

A relationship between entity sets P and C is a subset of all


possible pairs of entities in P and C, with tuples uniquely
identified by P and C’s keys

29
CSE 303 Database Ashikur Rahman

What is a Relationship?
Company Product
name name category price
GizmoWorks Gizmo Electronics $9.99
GadgetCorp GizmoLite Electronics $7.50
Gadget Toys $5.50

name category name


price
Product Makes Company

A relationship between entity sets P and C is a subset of all


possible pairs of entities in P and C, with tuples uniquely
identified by P and C’s keys
30
CSE 303 Database Ashikur Rahman

What is a Relationship?
Company Product
name name category price C.name P.name P.category P.price

GizmoWorks Gizmo Electronics $9.99 GizmoWorks Gizmo Electronics $9.99

GadgetCorp GizmoLite Electronics $7.50 GizmoWorks GizmoLite Electronics $7.50


Gadget Toys $5.50 GizmoWorks Gadget Toys $5.50
GadgetCorp Gizmo Electronics $9.99
GadgetCorp GizmoLite Electronics $7.50
GadgetCorp Gadget Toys $5.50
name category name
price
Product Makes Company

A relationship between entity sets P and C is a subset of all


possible pairs of entities in P and C, with tuples uniquely
identified by P and C’s keys
31
CSE 303 Database Ashikur Rahman

What is a Relationship?
Company Product
name name category price C.name P.name P.category P.price

GizmoWorks Gizmo Electronics $9.99 GizmoWorks Gizmo Electronics $9.99

GadgetCorp GizmoLite Electronics $7.50 GizmoWorks GizmoLite Electronics $7.50


Gadget Toys $5.50 GizmoWorks Gadget Toys $5.50
GadgetCorp Gizmo Electronics $9.99
GadgetCorp GizmoLite Electronics $7.50
GadgetCorp Gadget Toys $5.50
name category name
price
Product Makes Company
Makes
C.name P.name
A relationship between entity sets P and C is a subset of all GizmoWorks Gizmo
possible pairs of entities in P and C, with tuples uniquely GizmoWorks GizmoLite
identified by P and C’s keys GadgetCorp Gadget
32
CSE 303 Database Ashikur Rahman

What is a Relationship?
• There can only be one relationship for every This follows from our
unique combination of entities mathematical definition
of a relationship- it’s a
SET!

• This also means that the relationship is uniquely


determined by the keys of its entities

• Example: the key for Makes (to right) is since name


{Product.name, Company.name} name category
price
Product Makes Company

Why does this make sense?


33
CSE 303 Database Ashikur Rahman

Relationships and Attributes


• Relationships may have attributes as well.

since
name
name category
price
Product Makes Company

For example: “since” records Note: “since” is Note #2: Why not
implicitly unique per “how long”?
when company started
pair here! Why?
making a product
34
CSE 303 Database Ashikur Rahman

Tools at a glance

Entities (‘entity sets’)

Relationships (‘rel. sets’) and mapping


constraints

Attributes
CSE 303 Database Ashikur Rahman

2. E/R Design Considerations

36
CSE 303 Database Ashikur Rahman

What you will learn in this section

Relationships cont’d: multiplicity, multi-way

Design considerations

Conversion to SQL

37
CSE 303 Database Ashikur Rahman

Multiplicity of E/R Relationships


CSE 303 Database Ashikur Rahman

Multiplicity of E/R Relationships


1 a
One-to-one: 2 b
c
3
d
Indicated using
1 a
arrows
Many-to-one: 2
3
b
c
d

1 a
2 b
One-to-many: 3 c
d

1 a
Many-to-many: 2
3
b
c
d

39
CSE 303 Database Ashikur Rahman

Example: One to One Relationships (1:1)


1 a
One-to-one: 2 b
c
3
d

Def : Maximum one X for each Y and one Y for each X

Manager_ID
Branch_ID

MANAGER manages BRANCH


X Y

An employee (manager) can manage only one branch at one time


and each branch has only one manager. 40
CSE 303 Database Ashikur Rahman

Example: One to Many Relationships (1:M)


1 a
2 b
One-to-many: 3 c
d

Def : Maximum one X for each Y but possibly many Y for each X

Faculty_Name Course_Code Course_Name

FACULTY offer COURSE

X Y

At university, one faculty offers many courses for students but one
course is offered by one faculty only. 41
CSE 303 Database Ashikur Rahman

Example: Many to Many Relationships (M:N)


1 a
Many-to-many: 2
3
b
c
d

Def : Possibly many X’s for each Y and many Y’s for each X

Author_ID
ISDN

BOOK Written by AUTHOR

A book may have more than one author and an author may write
more than one book. 42
CSE 303 Database Ashikur Rahman

name category
name

price
makes Company
Product

stockprice
What does
buys this say?
employs

Person

address name ssn


43
CSE 303 Database Ashikur Rahman

Activity in the class


• For actors and directors, we want to store their name, a unique identification
number, address and birthday (why not age?)
• For films, we want to store the title, year of production and type (thriller,
comedy, etc.)
• We want to know who directed and who acted in each film. Every film has one
director. We store the salary of each actor for each film
• For Studio we want to store name, address and name of its president. A movie
can be produced by only one studio.
CSE 303 Database Ashikur Rahman

name dateOfBirth
title year
address
type
id
actedIn Film
Actors

name president directs


produces
address

Studio Director
dateOfBirth

name id
address 45
CSE 303 Database Ashikur Rahman

Self Relationships or
Recursive Relationships
CSE 303 Database Ashikur Rahman

manager
manages

EMPLOYEE
worker
CSE 303 Database Ashikur Rahman

Multi-way Relationships
CSE 303 Database Ashikur Rahman

Multi-way Relationships
How do we model that a studio has made a contract with a particular actor
to act in a particular movie?

Actor

Contract Film

Studio

NB: Can still model as a mathematical set (how?)


49
CSE 303 Database Ashikur Rahman

Formal mathematical definition


• An n-ary relationship set R involves exactly n entity sets: E1, …, En.
• Each relationship in R involves exactly n entities: e1 in E1, …, en in En
• Formally, R E1x …x En
……….
E1

R E2

……….

En
CSE 303 Database Ashikur Rahman

Arrows in Multiway Relationships


For a particular star and
Q: What does the arrow mean ? movie there is only one
studio under the contract
Actor

Contract Film

A studio may contract with


A star may contract with a Studio many stars for a movie
studio for several movies
51
CSE 303 Database Ashikur Rahman

Attributes in Relationships
Q: Suppose we want to add the signing money information. Where do we put?
salary

Actor
salary

Contract Film
salary

Studio
52
CSE 303 Database Ashikur Rahman

Attributes in Relationships: The correct way


Q: Suppose we want to add the signing money information. Where do we put?

salary
Actor

Contract Film

Studio
53
CSE 303 Database Ashikur Rahman

Converting Multi-way Relationships to Binary Relations

salary From what we


signedBy Actor had on
previous slide
to this - what
did we do?
Contracts
forMaking Film

ownerOf Studio

54
CSE 303 Database Ashikur Rahman

Converting Multi-way Relationships to New Entity + Binary


Relationships

salary
signedBy Actor
For a particular
contract there can
Contracts be only one star,
forMaking Film one movie and
Side note: one studio. But an
What arrows actor or a movie
should be or a studio may
ownerOf Studio
added here? have several
Are these contracts
correct?
55
CSE 303 Database Ashikur Rahman

3. Design Principles
CSE 303 Database Ashikur Rahman

Design Principles
What’s wrong with these examples?

Product Purchase Person

Person isPresident Country

Moral: be faithful! 57
CSE 303 Database Ashikur Rahman

Design Principles: What’s Wrong?

Studio
Movie OwnedBy

name
studioName

Moral: should not model the same


information in multiple ways 58
CSE 303 Database Ashikur Rahman

Design Principles: What’s Wrong?

date
Product

Purchase Store

personAddr
personName
Moral: pick the right
kind of elements (entities/relationships). 59
CSE 303 Database Ashikur Rahman

Design Principles: What’s Wrong?

Dates date

Product

Purchase Store

Moral: don’t
complicate life, it
is already enough Person
complicated. 60
CSE 303 Database Ashikur Rahman

Design Principles: Entity vs. Attribute


How do we handle employees
with multiple addresses here?

Addr 1
How do we handle addresses
where internal structure of the
address (e.g. postal code, city,
Employee
division) is useful?

61
CSE 303 Database Ashikur Rahman

Design Principles: Entity vs. Attribute


Or (B) be an entity?
Should address (A)
be an attribute?
Street Addr Postal code

Addr 1 Addr 2 Address

AddrOf

Employee
Employee

In general, when we want to record several values,


we choose new entity 62
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema


CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema

• Key concept:

Both Entity sets and Relationships become relations


(tables in RDBMS)

64
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema


price category

• An entity set becomes a relation name

Product
(collection of tuples / table)

– Each tuple is one entity


Product
– Each tuple is composed of the entity’s name price category
attributes, and has the same primary Gizmo1 99.99 Camera
key Gizmo2 19.99 Edible

65
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema


price category

name

Product
CREATE TABLE Product(
name CHAR(50) PRIMARY KEY,
price FLOAT,
category VARCHAR(30)
)
Product
name price category
Gizmo1 99.99 Camera
Gizmo2 19.99 Edible

66
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema

Converting Many-to-Many Relationships to


Relational Schema
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema


date firstname lastname
name
• A relationship between entity sets A1, …, AN category
price
also becomes a set of tuples / a table Product Purchased Person

– Each row/tuple is one relation, i.e. one


unique combination of entities (a1,…,aN)

Purchased
– Each row/tuple is name firstname lastname date
• composed of the union of the entity sets’ key Gizmo1 Bob Joe 01/01/15
attributes
Gizmo2 Joe Bob 01/03/15
• has the entities’ primary keys as foreign keys
Gizmo1 JoeBob Smith 01/05/15
• has the union of the entity sets’ keys as primary
key 68
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema


date firstname lastname
CREATE TABLE Purchased( name category
name CHAR(50), price
Product Purchased Person
firstname CHAR(50),
lastname CHAR(50),
date DATE,
PRIMARY KEY (name, firstname, lastname),
FOREIGN KEY (name)
REFERENCES Product(name),
FOREIGN KEY (firstname, lastname) Purchased
REFERENCES Person(firstname, lastname) name firstname lastname date
) Gizmo1 Bob Joe 01/01/15

Gizmo2 Joe Bob 01/03/15

Gizmo1 JoeBob Smith 01/05/15

69
CSE 303 Database Ashikur Rahman

From E/R Diagram to Relational Schema


Purchased (pname, sname, firstname,
How do we represent this as a
relational schema? lastname, date)

date
firstname lastname
name category

price

Product Purchased Person

Foreign key
name address
(1) pname  Product (name)
(2) sname  Store(name)
Store
(3) firstname, lastname  Person(firstname, lastname)
70
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema

Converting One-to-Many Relationships to


Relational Schema
CSE 303 Database Ashikur Rahman

From E/R Diagram to Relational Schema


Film (title, year, type)
• For many to one relations we have two
options: Director (id, name)
Option 1: Directed (title, year, id, salary)
Same as without key constraints (3 tables), except
that the primary key of Directed is now (title,
year) (why?)
type
id
Director Directed Film title

name
salary year

72
CSE 303 Database Ashikur Rahman

From E/R Diagram to Relational Schema


Film (title, year, type)
CREATE TABLE Film(
title CHAR(50), Director (id, name)
year INT,
type CHAR(40), Directed (title, year, id, salary)
PRIMARY KEY (title, year)
) CREATE TABLE Directed(
title CHAR(50),
year INT,
CREATE TABLE Director(
id INT,
id INT,
salary FLOAT,
name CHAR(40),
PRIMARY KEY (title, year),
PRIMARY KEY (id)
FOREIGN KEY (title, year)
)
REFERENCES Film (title, year),
FOREIGN KEY (id)
REFERENCES Director(id)
) 73
CSE 303 Database Ashikur Rahman

From E/R Diagram to Relational Schema


Film (title, year, type, id, salary)
Option 2:
• Do not create a table for the relationship Director (id, name)
• Add information columns that would have
been in the relationship's relation to the
relation of the entity which does not have the
incoming arrow
type
id
Director Directed Film title

name
salary year

74
CSE 303 Database Ashikur Rahman

From E/R Diagram to Relational Schema


Film (title, year, type, id, salary)
CREATE TABLE Film(
title CHAR(50), Director (id, name)
year INT,
type CHAR(40),
id INT,
salary FLOAT, CREATE TABLE Director(
PRIMARY KEY (title, year), id INT,
FOREIGN KEY (id) name CHAR(40),
REFERENCES Director(id) PRIMARY KEY (id)
) )

75
CSE 303 Database Ashikur Rahman

From E/R Diagrams to Relational Schema

Converting (1:1) Relationships to Relational


Schema
CSE 303 Database Ashikur Rahman

Studio (name, address)


name
name President (name, sname, address)

address
address
leadBy
President
Studio

CREATE TABLE President(


name CHAR(40),
sname CHAR(40),
CREATE TABLE Studio(
address VARCHAR(50),
name CHAR(40),
PRIMARY KEY (name),
address CHAR(40),
FOREIGN KEY (sname)
PRIMARY KEY (name)
REFERENCES Studio(name)
)
)
CSE 303 Database Ashikur Rahman

Translating Recursive Relationship Sets


(First option)
manager

id
Employee Manages
worker
name
Employee (id, name, address)
address
Manages (wid, mid)

What are all the relations created for this diagram?


CSE 303 Database Ashikur Rahman

Translating Recursive Relationship Sets


(second option)
manager

id
Employee Manages
worker
name
Employee (id, mid, name, address)
address

What are all the relations created for this diagram?


CSE 303 Database Ashikur Rahman

3. Advanced E/R Concepts

80
CSE 303 Database Ashikur Rahman

What you will learn about in this section

Subclasses & connection to OOP

Constraints

Weak entity sets

81
CSE 303 Database Ashikur Rahman

Modeling Subclasses
• Some objects in a class may be special, i.e. worthy of their own class

• Define a new class?


• But what if we want to maintain connection to current class?

• Better: define a subclass


• Ex: Products

Software Educational
products products

We can define subclasses in E/R!


82
CSE 303 Database Ashikur Rahman

Modeling Subclasses
name

price Child subclasses contain


all the attributes of all of
Product their parent classes plus
the new attributes
shown attached to them
in the E/R diagram
isA

Software Product Educational Product

platforms ageGroup

83
CSE 303 Database Ashikur Rahman

Understanding Subclasses
• Think in terms of records; ex:
name Child subclasses contain
name
• Product all the attributes of all of
price price their parent classes plus
Product the new attributes
name shown attached to them
isA • SoftwareProduct in the E/R diagram
Software Product Educational Product
price
platforms
platforms ageGroup

name
• EducationalProduct
price
ageGroup
84
CSE 303 Database Ashikur Rahman

How to convert E/R to


tables…
Product
name price category platforms ageGroup
name
category Gizmo 99 gadget unix null
price
Camera 49 photo null null
Product
Toy 39 gadget null retired

isA

Software Product Educational Product Many null values!!


platforms ageGroup

85
CSE 303 Database Ashikur Rahman

Product
Better option: one table for
name price category
each subclass… Gizmo 99 gadget
name Camera 49 photo
category
price Toy 39 gadget

Product
Sw.Product name platforms

isA Gizmo unix


Ed.Product
Software Product Educational Product name ageGroup

platforms ageGroup Toy retired

86
CSE 303 Database Ashikur Rahman

Difference between OOP and E/R inheritance

• OOP: Classes are disjoint (same for Java, C++)


OO = Object Oriented.
E.g. classes as
fundamental building
block, etc…
Product
p3
p1 p2
EducationalProduct
sp1 ep1 ep2
SoftwareProduct ep3
sp2

87
CSE 303 Database Ashikur Rahman

Difference between OOP and E/R inheritance

• E/R: entity sets overlap

Product

p1 p2
p3 ep1
sp1 EducationalProduct
ep2
SoftwareProduct sp2 ep3

88
CSE 303 Database Ashikur Rahman

Difference between OO and E/R inheritance


We have three entity sets, but four different kinds
of objects
Product

p1 p2
p3 ep1
sp1 EducationalProduct
ep2
SoftwareProduct sp2
esp1 esp2 ep3

No need for multiple inheritance in E/R


89
CSE 303 Database Ashikur Rahman

Suppose we have a product KIDPIX which is an


educational software… Where do we keep the Product
information?
name price category
Gizmo 99 gadget
name Camera 49 photo
category
price Toy 39 gadget

Product
Sw.Product name platforms

isA Gizmo unix


Ed.Product
Software Product Educational Product name ageGroup

platforms ageGroup Gizmo todler


Toy retired
CSE 303 Database Ashikur Rahman

Suppose we have a product KIDPIX which is an


educational software… Where do we keep the Product
information?
name price category
Gizmo 99 gadget
name Camera 49 photo
category
price Toy 39 gadget
KidPix 100 gadget
Product

name platforms name ageGroup


isA
Gizmo unix Toy retired

Software Product Educational Product KidPix Macintosh KidPix todler

platforms ageGroup Sw.Product Ed.Product


CSE 303 Database Ashikur Rahman

IsA Review
• If we declare A IsA B then every A is a B

• We use IsA to

• Add descriptive attributes to a subclass

• To identify entities that participate in a relationship

• No table for IsA relationship

• No need for multiple inheritance 92


CSE 303 Database Ashikur Rahman

Constraints in E/R Diagrams


• Finding constraints is part of the E/R modeling process. Commonly used
constraints are:

• Keys: Implicit constraints on uniqueness of entities


• Ex: An SSN uniquely identifies a person

• Single-value constraints:
• Ex: a product can be made by only one company

• Referential integrity constraints: Referenced entities must exist Recall


• Ex: if you work for a company, it must exist in the database FOREIGN
KEYs!
• Degree constraints:
• Ex: A movie may not have more than 10 actors 93
CSE 303 Database Ashikur Rahman

Keys in E/R Diagrams

Underline keys: name category

price
Note: no formal
way to specify
Product multiple keys in
E/R diagrams…

Person

address name ssn


94
CSE 303 Database Ashikur Rahman

Single Value Constraints

makes

v. s.

makes

95
CSE 303 Database Ashikur Rahman

Referential Integrity Constraints

Product makes Company

Each product made by at most one company.


Some products made by no company?

Product makes Company

Each product is made by exactly one company.


96
CSE 303 Database Ashikur Rahman

Degree Constraints

Actor
<=10 Movie
StarsIn

• We can attach a bounding number to edges to indicate limits on


the number of entities that can be connected to a single entity via
a relationship set
• In the example above, a movie has at most 10 stars
• Note: a regular arrow is the constraint <=1
• Note: a rounded arrow is the constraint =1

97
CSE 303 Database Ashikur Rahman

Weak Entity Sets


The entity set that can not exist without another entity set

a ROOM can only exist in a BUILDING

a TIRE is considered as a strong entity because it can exist without being attached to
a CAR

Question is strong entity type and answer is weak. Question is always


there, but an answer requires a question to exist.
CSE 303 Database Ashikur Rahman

Weak Entity Sets


Entity sets are weak when their key comes from other
classes to which they are related.
address

Team affiliation University


coach

tname number name total team

“Programming contest team” v. “BUET


Programming contest team” (E.g., Dhaka
university has a programming contest team
99
too)
CSE 303 Database Ashikur Rahman

Weak Entity Sets


Only 2 tables are required, no table for the relationship is required
University (name, totalTeam, address)
Team (uname, number, tname, coach)
address

Team affiliation University


coach

tname number name totalteam

• number is a partial key. (denote with dashed underline).


• University is called the identifying owner. 100
CSE 303 Database Ashikur Rahman

Weak Entity Sets


Only 2 tables are required, no table for the relationship is required
Person (nid, name, address)
Phone (nid, phoneno,)
address

Phone contacted Person

phoneno nid name

• Phone no is a partial key. (denote with dashed underline).


• Person is called the identifying owner. 101
CSE 303 Database Ashikur Rahman

E/R Summary
• E/R diagrams are a visual syntax that allows
technical and non-technical people to talk
• For conceptual design

• Basic constructs: entity, relationship, and


attributes

• A good design is faithful to the constraints of the


application
102
CSE 303 Database Ashikur Rahman

The bigger picture of what we have learned

name

Conceptual Model: Product buys Person

price name ssn

Relational Model (in 1NF)


plus FD’s

Normalization:
Eliminates anomalies
CSE 303: Ashikur Rahman 103

You might also like