When to use it
Database design for shops and marketplaces, "online store" capstone projects and the data model section of API docs. Orders and order items are one-to-many; orders and payments are one to zero-or-one, because an unpaid order has no payment record.
Making it yours
- Add entities such as cart, coupon or review as
NAME { … }blocks and connect them. - Field names can be your real column names; Mermaid accepts them as they are.
- With many entities the diagram gets wide; split it into "customers and orders" and "products and categories".
Mermaid code
erDiagram
CUSTOMER ||--o{ ORDER : places
CUSTOMER ||--o{ ADDRESS : has
ORDER ||--|{ ORDER_ITEM : contains
PRODUCT ||--o{ ORDER_ITEM : "appears in"
CATEGORY ||--o{ PRODUCT : groups
ORDER ||--o| PAYMENT : "is paid by"
CUSTOMER {
string customer_id PK
string phone
string display_name
datetime created_at
}
ADDRESS {
string address_id PK
string customer_id FK
string recipient
string street_address
}
ORDER {
string order_no PK
string customer_id FK
string address_id FK
decimal total
string status
datetime created_at
}
ORDER_ITEM {
string item_id PK
string order_no FK
string product_id FK
int quantity
decimal unit_price
}
PRODUCT {
string product_id PK
string category_id FK
string name
decimal price
int stock
}
CATEGORY {
string category_id PK
string name
}
PAYMENT {
string transaction_id PK
string order_no FK
decimal amount
string channel
}Ask an AI for one with this structure
Send the prompt below to ChatGPT or Claude, then paste the whole reply into the editor; the code inside is detected automatically.
Draw an e-commerce database ER diagram in Mermaid erDiagram syntax with entities CUSTOMER, ADDRESS, ORDER, ORDER_ITEM, PRODUCT, CATEGORY and PAYMENT. A customer places many orders and has many addresses; an order contains one or more order items; a product appears in many order items; a category groups many products; an order is paid by zero or one payment. List the main fields of each entity with PK and FK markers. Output only the mermaid code block.
FAQ
- How do I write one to zero-or-one?
ORDER ||--o| PAYMENT:o|means zero or one. Unpaid orders have no payment, which is exactly this relationship.- Must many-to-many always go through a junction table?
In a relational database, yes. Mermaid lets you write
}o--o{directly, but the implementation still needs a junction table, so drawing the junction entity is closer to reality.- Too many fields to read after export
Export the PNG at 3x or use SVG; or keep only the key fields in the diagram and put the full list in a data dictionary table.
More in this category
Updated 2026-09-05