The Entity-Relationship Model
The Entity-Relationship Model
Overview
2. Basic ER Constructs
Database Design
Database Design is the process of developing a
database schema for a given application.
DB design is a subtask of the overall software engineering
effort.
224
Database Design (2)
O
1 Conceptual Database Design. Produces the initial
model of the mini world in a conceptual data model
(e.g., in the ER model).
O
2 Logical Database Design. Transforms the conceptual
schema into the data model supported by the DBMS
(e.g., the relational model).
O
3 Physical Database Design. Design indexes, table
distribution, buffer size, etc., to maximize performance of
the final system (subject of “Datenbanken II ”).
228
Database Design (6)
Instructor Course
Noggg3 42232
ggWgWgg
W
ll • WWWWWW
lll +
Grust kWWName
WWWWW lllll Title XML
W • lRll
ggg RRRR
sggggg RRR
RRR
gggg3
7111 Phone 20727
RR ggggNo
• WWWWWW
WWW+
Title DB1
231
The ER Model (1)
Overview
2. Basic ER Constructs
• Entities:
. An object in the mini world about which information is to
be stored. Examples: persons, books, courses.
Note: entities do not have to correspond to objects of
physical existence. Entities may also represent conceptual
objects like, e.g., vacations.
. The mini world that has to be modelled can contain only a
finite number of objects.
. It must be possible to distinguish entities from each other,
i.e., objects must have some identity.
Examples: entity book identified by ISBN number, entity
vacations identified by travel agency booking number.
235
Basic ER Model Concepts (2)
• Attribute:
• Relationship:
• Entity E:
E A
E1 R E2
238
ER Diagrams (2)
Course Precondition
requires knowledge of
points
Overview
2. Basic ER Constructs
• General ER relationship:
E1 R E2
aaaaaaa •
• V\aVa\Va\Va\Va\a\a\a\\\\\\\
VVVVV \
• VVVVV •
V
aaaaaaaf •
• aaaaaafafafffffff
ffff
• ff
•
•
• \\\\\\\\\\\\\\
\•
244
Cardinalities (2)
E1 (m1 , n1 ) R (m2 , n2 ) E2
• Extensions:
. “∗” may be used as maximum if there is no limit.
. (0, ∗) means no restriction at all (general relationship).
247
Cardinalities (5)
Marriage
Airport Locations
• In real applications, the cardinalities (0, 1), (1, 1), and (0, ∗)
are the most common and especially easy to enforce in the
relational model.
250
Common Cases (1)
. Thus, only the (0, 1), (1, 1), (0, ∗), (1, ∗) cardinalities are
common in practice.
• Many-to-many relationships:
. Both maximum cardinalities are ∗ (the minimum
cardinalities are 0 or 1):
Many-to-many relationship
• One-to-many relationships:
. Maximum cardinality 1 on the “many” side and ∗ on the
“one” side:
One-to-many relationship
• One-to-one relationships:
. Maximum cardinality 1 on both sides:
One-to-one relationship
One-to-many relationship
One-to-many relationship
Overview
2. Basic ER Constructs
ER Key
A key K of an entity type E a is an attribute of E which
uniquely identifies the entities of this type. No two
different entities share the same value for the key
attribute. Composite keys are allowed.
K
E A1
A2
O
1 there is a relationship with cardinality (1, 1) on the
detail entity side, and in addition
O
2 the key of the master is inherited and becomes part of
the key of the detail entity.
Inv No Inv No
Invoice (1, ∗) has (1, 1) Position
Date Pos
260
Weak Entities (2)
Inv No
Invoice (1, ∗) has (1, 1) Position Pos
Date
• For the weak entity, the inherited part of the key is not
shown.
3 Non-weak entities are also called strong entities.
263
Weak Entities (5)
Overview
2. Basic ER Constructs
CustNo
Customer Name
(0, ∗) Phone
places
(1, 1) Description
• Transforming an ER entity E:
O
1 Create a table for each entity.
The table name is E (conventionally: E + ’s’).
O
2 The columns of this table are the attributes of the
entity type.
O
3 The primary key of the table is the primary key of the
entity type.
If E’s key is composite, so will be the relational key. If E
has no key, add an artifical key to the table.
267
Step 1: Entities (2)
Customers Orders
CustNo Name Phone OrdNo Date
10 Jones 624-9404 200 2/15/04
11 Smith 201 2/16/04
Products
ProdNo Description Price
1 Apple 0.50
2 Kiwi 0.25
3 Orange 0.60
268
Step 1B: Weak Entities
• Transforming a relationship R:
O
1 If R has maximum cardinality 1 on one side, R is
one-to-many4 .
Example: Customer–(0, ∗)–places–(1, 1)–Order, “one
customer places many orders.”
O
2 In this case, add the key of the “one” side as a column
to the “many” table to implement R.
O
3 This column will be a foreign key referencing a row in
the table representing the related entity.
see below.
270
Step 2: One-To-Many Relationships (2)
Customers
CustNo Name Phone
10 Jones 624-9404
11 Smith
O
4 If the minimum cardinality is 1 on the “many” side (see
example), null values are not allowed in the foreign key
column (column placed by in example).
If the minimum cardinality is 0, null values are allowed
in the foreign key column.
The foreign key is null for those entities that do not
participate in R at all.
272
Step 2: Relationship Attributes
• The extra table holds the key values of the related entities
plus the relationship attributes.
• The key attributes of the side with the (0,1) cardinality
become the key of this relation.
“Each book can be borrowed only once at the same time.”
This does not model R correctly if the cardinality is (1,1)
Why?
274
Step 3: Many-To-Many Relationships (1)
O
2 R becomes its own table.
O
3 The columns of this table are the keys of both
participating entity types.
O
4 These columns act as foreign keys referencing the entities
and, at the same time, together form a composite key
for the extra table.
275
Step 3: Many-To-Many Relationships (2)
• Transforming a many-to-many-relationship R:
O
5 Relationship attributes are added as columns to the table
representing R.
Composite key?
Is it really necessary that both entity keys (here: OrdNo,
ProdNo) form a composite key for the relationship table?
276
Step 3: Many-To-Many Relationships (3)
Orders Products
OrdNo Date CustNo ProdNo Description Price
200 2/15/04 11 1 Apple 0.50
201 2/16/04 11 2 Kiwi 0.25
3 Orange 0.60
277
Step 3: Many-To-Many Relationships (4)
DName ID
O
1 If R has maximum cardinality 1 on both sides, R is
one-to-one.
O
2 We can essentially transform as if R were one-to-many,
but additional key constraints are generated.
279
Step 4: One-To-One Relationships (2)
• To which entity table shall we add the led by attribute to
represent the relationship?
DName ID
DName ID
• Two variants:
O
1 Any of the two (not both!) entity tables may host the
married foreign key (null values allowed).
O
2 Translate the relationship into a table of its own:
married (MName → Man, WName → Woman)
A one-to-one relationship in an extra table
What would be the correct key(s) for table married?
282
Step 4: One-To-One Relationships (5)
No ID