Mermaid ER diagrams explained with an order database

Build a Mermaid ER diagram for customers, orders, and line items. Learn keys, cardinality, optional relationships, and what to check before sharing.

Updated November 19, 2026 · 2 min read

An entity relationship diagram explains what a database stores and how records relate. In Mermaid, you write those entities and relationships as text, making the model easy to review alongside a schema change.

Start with one business question: what must exist for an order to be valid? The answer determines the relationships more reliably than the arrangement of boxes.

Draw a small order model

Paste this example into a Mermaid editor:

erDiagram
  CUSTOMER ||--o{ PURCHASE_ORDER : places
  PURCHASE_ORDER ||--|{ ORDER_ITEM : contains
  PRODUCT ||--o{ ORDER_ITEM : appears_in
  CUSTOMER {
    int id PK
    string email UK
  }
  PURCHASE_ORDER {
    int id PK
    int customer_id FK
    string status
  }
  ORDER_ITEM {
    int id PK
    int order_id FK
    int product_id FK
    int quantity
  }
  PRODUCT {
    int id PK
    string sku UK
  }

There are four entities. ORDER_ITEM connects an order to its products while also storing quantity. That connecting entity matters: putting a product list in the order box would hide the place where quantity belongs.

Read the relationship ends

|| means exactly one. o{ means zero or more, and |{ means one or more. Read both ends of every line in plain language.

In this example, each order belongs to exactly one customer, while a customer can have no orders or many orders. Each order contains at least one item. That last choice describes submitted orders; a shopping cart might allow zero items and deserve a different model.

The labels places, contains, and appears_in make the diagram readable, but they do not enforce database rules. Likewise, an ER relationship cannot replace a foreign-key constraint or application validation. Mermaid also distinguishes identifying and non-identifying relationships through line styles; choose those deliberately rather than treating them as decoration. See the official ER syntax reference.

Separate keys from business assumptions

PK, FK, and UK identify primary, foreign, and unique keys in the displayed attributes. They tell a reviewer how to interpret a field. Cardinality tells a different story: how many records may participate.

Before sharing, ask whether guest checkout is allowed. If it is, the mandatory customer relationship may be wrong. Ask whether deleting a product should affect historical orders. The diagram should explain the chosen policy, even if a note outside the diagram carries the detail.

Keep the model useful

Show the fields needed for the discussion, not every timestamp and audit column. Split a large database into subject areas, with consistent names for shared entities. A diagram that fits on one screen often supports review better than an unreadable inventory of every table.

Open Flowgraph Studio to render the example. In Manual mode you can move entities, draw a relationship and pick the cardinality at each end, edit attributes, and mark keys; the Mermaid code updates as you go. For an existing database, New diagram → Start from → From a SQL schema turns CREATE TABLE statements into an ER diagram (see converting SQL to an ER diagram). Review the generated cardinality the same way: the import reads constraints, not business rules.

If the discussion is about which service calls which endpoint rather than stored relationships, use a sequence diagram instead. Pick the diagram that answers the reader's question.

More guides