Professional Documents
Culture Documents
Lecture 12
2022
Chapter 4: E-R diagram
CSE 303 Database Ashikur Rahman
Outline
3
CSE 303 Database Ashikur Rahman
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
5
CSE 303 Database Ashikur Rahman
2. Conceptual Design
3. More:
• Logical Database Design
• Translation of ER diagram to a relational database schema (description of tables)
• Security Design
8
CSE 303 Database Ashikur Rahman
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
1
Prof. Ashikur Rahman
3
CSE 303 Database Ashikur Rahman
Requirem Relational
Relational
ent E/R Diagram Database
DBMS
Analysis Schema
16
CSE 303 Database Ashikur Rahman
Basic Concepts:
Entities, Attributes, Relationships
CSE 303 Database Ashikur Rahman
18
CSE 303 Database Ashikur Rahman
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
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
name name
category
price
Product Makes Company
23
CSE 303 Database Ashikur Rahman
name category
name
price
makes Company
Product
stockprice
buys employs
Person
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
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
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
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
What is a Relationship?
Company Product
name name category price C.name P.name P.category P.price
What is a Relationship?
Company Product
name name category price C.name P.name P.category P.price
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!
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
Attributes
CSE 303 Database Ashikur Rahman
36
CSE 303 Database Ashikur Rahman
Design considerations
Conversion to SQL
37
CSE 303 Database Ashikur Rahman
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
Manager_ID
Branch_ID
Def : Maximum one X for each Y but possibly many Y for each X
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
Def : Possibly many X’s for each Y and many Y’s for each X
Author_ID
ISDN
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
name dateOfBirth
title year
address
type
id
actedIn Film
Actors
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
R E2
……….
En
CSE 303 Database Ashikur Rahman
Contract Film
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
salary
Actor
Contract Film
Studio
53
CSE 303 Database Ashikur Rahman
ownerOf Studio
54
CSE 303 Database Ashikur Rahman
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?
Moral: be faithful! 57
CSE 303 Database Ashikur Rahman
Studio
Movie OwnedBy
name
studioName
date
Product
Purchase Store
personAddr
personName
Moral: pick the right
kind of elements (entities/relationships). 59
CSE 303 Database Ashikur Rahman
Dates date
Product
Purchase Store
Moral: don’t
complicate life, it
is already enough Person
complicated. 60
CSE 303 Database Ashikur Rahman
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
AddrOf
Employee
Employee
• Key concept:
64
CSE 303 Database Ashikur Rahman
Product
(collection of tuples / table)
65
CSE 303 Database Ashikur Rahman
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
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
69
CSE 303 Database Ashikur Rahman
date
firstname lastname
name category
price
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
name
salary year
72
CSE 303 Database Ashikur Rahman
name
salary year
74
CSE 303 Database Ashikur Rahman
75
CSE 303 Database Ashikur Rahman
address
address
leadBy
President
Studio
id
Employee Manages
worker
name
Employee (id, name, address)
address
Manages (wid, mid)
id
Employee Manages
worker
name
Employee (id, mid, name, address)
address
80
CSE 303 Database Ashikur Rahman
Constraints
81
CSE 303 Database Ashikur Rahman
Modeling Subclasses
• Some objects in a class may be special, i.e. worthy of their own class
Software Educational
products products
Modeling Subclasses
name
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
isA
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
86
CSE 303 Database Ashikur Rahman
87
CSE 303 Database Ashikur Rahman
Product
p1 p2
p3 ep1
sp1 EducationalProduct
ep2
SoftwareProduct sp2 ep3
88
CSE 303 Database Ashikur Rahman
p1 p2
p3 ep1
sp1 EducationalProduct
ep2
SoftwareProduct sp2
esp1 esp2 ep3
Product
Sw.Product name platforms
IsA Review
• If we declare A IsA B then every A is a B
• We use IsA to
• Single-value constraints:
• Ex: a product can be made by only one company
price
Note: no formal
way to specify
Product multiple keys in
E/R diagrams…
Person
makes
v. s.
makes
95
CSE 303 Database Ashikur Rahman
Degree Constraints
Actor
<=10 Movie
StarsIn
97
CSE 303 Database Ashikur Rahman
a TIRE is considered as a strong entity because it can exist without being attached to
a CAR
E/R Summary
• E/R diagrams are a visual syntax that allows
technical and non-technical people to talk
• For conceptual design
name
Normalization:
Eliminates anomalies
CSE 303: Ashikur Rahman 103