Entity relationship diagram (Mermaid)

An ER diagram is the fastest way to explain a schema to somebody who will write queries against it. Cardinality is the part that carries real information, so get it right.


#What this example shows

  • Crow's foot cardinality on every relationship.

  • Attributes with PK, FK and UK markers.

  • A join entity drawn explicitly rather than hidden behind a many-to-many line.


#A worked example

Open it, edit it, or copy the source below.

Entity relationship diagram (Mermaid)

The source, which you can paste into a new diagram and edit:

erDiagram
  CUSTOMER ||--o{ ORDER : places
  ORDER ||--|{ ORDER_ITEM : contains
  PRODUCT ||--o{ ORDER_ITEM : "appears in"
  ORDER ||--o| SHIPMENT : "fulfilled by"

  CUSTOMER {
    uuid id PK
    string email UK
    string name
    timestamp created_at
  }
  ORDER {
    uuid id PK
    uuid customer_id FK
    string status
    numeric total
    timestamp placed_at
  }
  ORDER_ITEM {
    uuid id PK
    uuid order_id FK
    uuid product_id FK
    int quantity
    numeric unit_price
  }
  PRODUCT {
    uuid id PK
    string sku UK
    string name
    numeric price
  }
  SHIPMENT {
    uuid id PK
    uuid order_id FK
    string carrier
    string tracking_ref
  }

#Making it your own

  • Cardinality symbols: || exactly one, |{ one or many, o{ zero or many, o| zero or one.

  • List the columns that matter, not all forty.

  • For types, defaults and indexes, DBML is the better tool.



Copy it. Change one line. See what happens.