Eroxl's Notes
Entity-Relationship Diagram
aliases
ER Diagram

An entity-relationship diagram is a method of conceptually describing the schema of a database in a human viewable format.

Components

Entity

The entities of an entity-relationship diagram describes a "thing" that is capable of an independent existence that can be uniquely identified. An entity is a thing that exists either physically or logically.

An entity may be a physical object such as a house or a car (they exist physically), an event such as a house sale or a car service, or a concept such as a customer transaction or order (they exist logically as a concept).

Entities can typically be thought of as nouns. Examples include a computer, an employee, a song, or a mathematical theorem.

Relationship

The relationships of an entity-relationship diagram captures how entities are related to each other. Relationships can be thought of as verbs linking two or more nouns.

Examples include an owns relationship between a company and a computer, a supervises relationship between an employee and a department, a performs relationship between an artist and a song, and a proves relationship between a mathematician and a conjecture.

Cardinality

The cardinality of a relationship describes the number of relationships that an entity can participate in. There are generally 4 types of cardinality:

  1. one-to-one - The entity A is associated with at most one entity B and vice versa
    • We could model an employees computer using a one-to-one relationship as every employee only has one computer and every computer is used by only one employee.
  2. one-to-many - The entity A is associated with any number of entities B but each B is only associated with at most one entity A
    • We can model a teacher and the classes they teach with a one-to-many relationship as a class can only have a single teacher but one teacher can teach many classes.
  3. many-to-one - The entity A is associated with at most one of the entities B but each B can be associated with any number of entities A (this is the inverse of one-to-many).
    • This can just model the inverse of one-to-many for example we can model a class having a single teacher but every teacher teaching multiple classes.
  4. many-to-many - The entity A is associated with any number of entities B and vice versa
    • We can model the relationships between students and classes using many-to-many relationships as each student is taking multiple classes and each class has multiple students enrolled in it.

Participation

The participation of a relationship describes whether an entity must be involved in that relationship in order to exist. There are generally 2 types of participation:

  1. total participation - Every instance of the entity must participate in at least one instance of the relationship.
    • We could model an employee belonging to a department using total participation as every employee must be assigned to a department.
  2. partial participation - Some instances of the entity may not participate in the relationship.
    • We could model an employee managing a department using partial participation as not every employee is a manager.

Notation

When designing an entity-relationship diagram, the entities are drawn as squares with their descriptive name (typically a noun) in the centre, these are then connected to the attributes using a line which are drawn as circles with their respective attribute keys in the middle.

Relationships are drawn as a vertical rhombus (resembling a kite) with a descriptive name in the centre (typically a verb) in the centre. These relationships are then linked to their constituting entities.

Line Notation

The lines to relationships can be "plain" or have arrows, these combinations of arrows an plane lines describes the cardinality of the relationship.

Cardinality

One-to-one

Two plane lines indicate a many-to-many relationship

Example of a many-to-many relationship between actors who work on movies.

One-to-many and Many-to-one

One plane line and an arrow indicate a one-to-many or a many-to-one relationship

Example of a one-to-many relationship between directors who direct movies.

One-to-one

Two arrows indicate a one-to-one relationship

Example of a one-to-one relationship between a country and their capital.

Participation

When an entity requires total participation it's line to the relationship is drawn bolded.

Example of a one-to-many relationship between directors who direct movies, where movies are required to have a relationship to a director.