Skip to main content

Entity Relationship Diagram

An entity–relationship model (or ER model) describes interrelated things of interest in a specific domain of knowledge. A basic ER model is composed of entity types (which classify the things of interest) and specifies relationships that can exist between entities (instances of those entity types). Wikipedia

The syntax is based on Mermaid 8.9. This diagram should start with erDiagram.

Basic syntax

<entity-a> [<relationship> <entity-b> : <relationship-label> [<key>] ["<comment>"]]
  • <entity-a> and <entity-b> are the names of entities.
  • <relationship> shows how the two entities are related.
  • <relationship-label> is a label that describes the relationship from entity-a's perspective.
  • <key> is a decoration field that - if specified - appears in the first row, pretty useful for database diagram.
  • <comment> is a double-quoted string for comments.
erDiagram
  title: Entity Relationship Example
  CUSTOMER {
    int id PK
    int address FK
  }
  CUSTOMER ||--o{ ORDER : places
  ORDER ||--|{ LINE-ITEM : contains
  CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
  ORDER {
    int orderNumber PK
    int customer FK "customer id"
    string deliveryAddress
  }

Relationship Syntax

CUSTOMER ||--o{ ORDER : places

This can be read as a CUSTOMER can places zero or more ORDERs, where as an ORDER belongs to only one CUSTOMER.

The relationship part of each statement can be broken down into three sub-components:

  1. the cardinality of the first entity with respect to the second,
  2. whether the relationship confers identity on a 'child' entity
  3. the cardinality of the second entity with respect to the first

Cardinality

Cardinality defines the numerical attributes of the relationship between two entities. In the above example an CUSTOMER can have zero or more ORDER instances associated to it, whereas a ORDER can only be associated with one CUSTOMER.

In each cardinality marker there are two characters. The outermost character represents a maximum value, and the innermost character represents a minimum value. The table below summarises possible cardinalities.

Value (left)Value (right)Meaning
|oo|Zero or one
||||Exactly one
}oo{Zero or more (no upper limit)
}||{One or more (no upper limit)

Identification

Relationships may be classified as either identifying or non-identifying and these are rendered with either solid or dashed lines respectively.

This is relevant when one of the entities in question can not have independent existence without the other.

Inheritance

You can specificy specialization - or inheritance in the other word - with the inherit keyword.

This is a useful feature for expressing your design in extended entity-relationship model.

erDiagram
  person {
    int age
    string phone_number
  }

  customer inherit person
  deliverer inherit person

  %% some extra attributes for customer
  customer {
    string address
  }

Override config

You can override diagarm config through @param directive.

All available configs can be seen in the Config page.

erDiagram
  @param fill #aabb00
  @param stroke #aacc00
  @param textColor #cc4488
  @param attributeFill #fcfff2
  @param labelBackground #white
  @param fontSize 16
  @param borderRadius 4
  @param edgeType ortho

  artists {
    INTEGER ArtistId
    NVARCHAR Name
  }
  albums

  artists ||--o{ albums : "publishes"
  artists }|--|{ labels : "a member of"