knowledge-kitchen

Database Design - Entity-Relationship Diagrams (ERD)

Database Design

  1. Overview
  2. Entities
  3. Attributes
  4. Relationships
  5. Cardinality
  6. Physical Implementation of Relationships
  7. Perspectives
  8. Conclusions

Overview

Introduction

Entity-Relationship Diagrams (ERDs) provide a way to document the entities in a database along with the attributes that describe them.

In practice, there are several styles of ER diagrams:

We will [more-or-less] follow Chen notation.

Entities

Concept

An entity is something about which we store data.

Entity symbol

Example

An example of two entities: a dog and a person.

Dog entity

Person entity

Attributes

Concept

Entities have data that describe them… their attributes.

Attribute symbol

Example

An example of attributes of a dog:

Dog entity

Example

An example of attributes of a person:

Person entity

Primary Keys

In a logical model, some attributes are indicated as primary keys - attributes that can be used to uniquely identify each entity.

Key attribute symbol

Example

An example of attributes of a dog, with the primary key underlined:

Key attribute symbol

Example

An example of attributes of a person, with the primary key underlined:

Key attribute symbol

Foreign Keys

Entity Relationship Diagramming purists would say that foreign key fields should not be included in the diagrams.

Key attribute symbol

Relationships

Concept

Entities sometimes have relationships with one another.

Relationship symbol

Example

An example of relationship between a dog and a person:

Relationship symbol

Example

Another example of relationship between a dog and a person:

Relationship symbol

Example

Both relationships in one diagram.

Relationship symbol

Example

An ERD play on the phrase, “It’s a dog eat dog world.

Relationship symbol

Entity or Attribute?

Sometimes it may seem unclear whether a given term should be represented as an entity or as an attribute.

The following questions may help determine the answer to this question:

If the answer to any of these questions is yes, then the matter in question should be an entity.

Cardinality

Concept

Cardinality is a jargon term meaning how many entities of each type are involved in a given relationship.

Cardinality symbol

The possibilities are:

Cardinality is indicated by numbers or letters next to each entity in the relationship.

Numbers represent a distinct number of the given entity involved in the relationship.

The letters, M and N, indicate multiples of the given entity are involved in the relationship.

Example

A Cyclops has only one eye.

Cardinality example

Example

In the ancient Greek myth of the Graeae, three sisters named Pephredo, Enyo, and Dino shared one eye and one tooth.

Cardinality example

Example

Each painter paints many paintings.

Cardinality example

Cardinality example

Example

Of course painters and paintings most likely have attributes of interest to us.

Cardinality example

Example

A painter might also own one or more dogs.

Cardinality example

Example

But beware… a painter’s dog may tear up their painting.

Cardinality example

Physical Implementation of Relationships

Overview

While not all Entity-Relationship diagrams show how cardinality will be “physically” implemented in a real relational database, let us explore this for a minute.

One-to-one

A one-to-one relationship is implemented by having a foreign key in one table pointing to the other… it doesn’t matter which table has the foreign key.

Image a cyclopses table in a world where each cyclops has only one eye:

id name email
1 Brontes brontes@greek.io
2 Steropes steropes@jourrapide.com
3 Arges arges@mythology.org
4 Polyphemus polyphemus@bmoney.com

Related to an eyes table, where each eye belongs to one cyclops:

id color cyclops_id
1 blue 1
2 hazel 4
3 blue 3
4 brown 2

One-to-many

In a one-to-many relationship, it is always the “many” table that has the foreign key pointing to the other.

Image a cyclopses table in a world where multiple cyclopses share one eye:

id name email eye_id
1 Deino deino@greek.io 1
2 Enyo enyo@jourrapide.com 1
3 Pemphredo pemphredo@mythology.org 1

Related to an eyes table:

id color
1 blue
2 hazel
3 blue
4 brown

Many-to-many

A many-to-many relationship requires a new “linking” table that contains only foreign keys pointing to the two records involved in each relationship.

E.g. a cyclopses_eyes linking table:

cyclops_id eye_id
1 1
2 1
3 1
1 3
2 2

Perspectives

Concept

How you diagram a model depends on whether you’re designing the conceptual model, the logical model, or the physical model.

Conceptual model

Conceptual modeling involves creating a model of a real world problem that is independent of any physical considerations.

Conceptual modeling process

Building a conceptual model involves:

Logical model

Compared to conceptual modeling, a logical model shows more detail of all entities and relationships

Logical modeling process

Building a logical model involves:

Physical model

The physical model includes all the same properties as the conceptual and logical models, in addition to more details of how the database will actually be implemented in a particular type of relational database management system.

Physical modeling process

Building a physical modeling involves:

An index is an order or organization of the data in some fields of a table to make retrieval faster and more efficient.

Creating indices becomes increasingly important for larger data sets.

Conclusions

Thank you. Bye.