Diagrams / Mermaid ER Diagram: Syntax & Examples

Mermaid ER Diagram: Syntax & Examples

Explore Mermaid ER syntax with entities, key markers and cardinality. Read a customer-order example, then edit and preview the data model.

When to use ER Diagram

Describe an entity relationship model with Mermaid ER syntax. This view helps readers understand entities, attributes, and how records can relate. Use it for conceptual data design or a physical schema review, and make clear which level the diagram represents.

Read the ER Diagram example

Diagram source

erDiagram
  CUSTOMER ||..o{ ORDER : places
  ORDER ||--|{ ORDER_ITEM : contains
  CUSTOMER {
    int id PK
    string name
  }
  ORDER {
    int id PK
    int customer_id FK
  }
  ORDER_ITEM {
    int order_id PK,FK
    int line_number PK
    int quantity
  }
A customer can place zero or many orders; each order contains one or more items. ORDER has its own key, so the customer link is non-identifying. ORDER_ITEM uses order_id as part of its composite primary key, making the order-to-item relationship identifying. PK and FK mark the roles of the attributes.

How to use this tool

  1. Start with the example in the editor and identify the elements that answer your question.
  2. Replace the sample labels and values, previewing after each structural change.
  3. Review the diagram at its intended reading size, then keep the source and export the format your document accepts.

Syntax and interpretation

ConceptHow to apply it
CardinalityCrow’s foot markers describe one or many instances, while optionality distinguishes zero from a required instance.
KeysPK, FK, and UK can mark primary, foreign, and unique keys. Include them when implementation detail helps the reader.
Relationship styleSolid and dashed lines distinguish identifying and non-identifying relationships. They are semantic choices, not decoration.

Common questions

Can the diagram enforce database constraints?

No. It documents the model. Verify keys, nullability, and constraints in the actual schema; an attractive diagram does not prove data integrity.