Mermaid ER Diagram: Customers, Orders and Products
Write an entity relationship diagram in Mermaid for a small online shop: crow's foot relationships, a junction entity for many-to-many, and attributes with primary and foreign keys. Made in the free Mermaid live editor.
Step by step
Data as code
- ER diagram in code. An ER diagram shows what data a system keeps and how it connects. Here is one for a small online shop, with customers, orders and products, written as code in the free Mermaid live editor.
- Diagram type. Clear the editor and type erDiagram, as one word, on the first line. This tells Mermaid to draw an entity relationship diagram.
erDiagram
Relationships
- Relationships. Type CUSTOMER ||--o{ ORDER : places. A relationship has an entity on each side. The 2 bars next to CUSTOMER mean exactly one. The circle and crow's foot next to ORDER mean zero or many. So one customer places zero or many orders. The verb goes after the colon, so the line reads like a sentence: customer places order. The diagram shows places as the label on the line.
CUSTOMER ||--o{ ORDER : places - Junction entity. An order holds many products, and a product sits in many orders. That is many-to-many, so add ORDER_LINE in the middle: ORDER ||--|{ ORDER_LINE : contains and PRODUCT ||--o{ ORDER_LINE : "appears in". A bar and a crow's foot mean one or many. Now every relationship is one-to-many. The quotes around "appears in" let the label have a space.
ORDER ||--|{ ORDER_LINE : contains PRODUCT ||--o{ ORDER_LINE : "appears in"
Attributes
- Attributes. Next, add the attributes. Open curly brackets after the entity name and close them on their own line. Each line is a type, then a name: int customer_id PK and string email. PK marks the primary key. ORDER gets its own key, int order_id PK, plus int customer_id FK, a foreign key that points back to CUSTOMER, and date order_date. In a one-to-many relationship, the foreign key sits on the many side. ORDER_LINE holds 2 foreign keys, int order_id FK and int product_id FK, plus int quantity. PRODUCT gets its key, int product_id PK, then string name and decimal price.
CUSTOMER { int customer_id PK string email }ORDER { int order_id PK int customer_id FK date order_date }ORDER_LINE { int order_id FK int product_id FK int quantity }PRODUCT { int product_id PK string name decimal price }
Check it
- Read it aloud. The finished diagram shows every entity with its attributes. Read each line out loud, like "one customer places zero or many orders", to check it with your stakeholders. ORDER_LINE is the junction entity. It turns one many-to-many relationship between ORDER and PRODUCT into 2 simple one-to-many ones.
Recap
- The rules. The rules: write entity, cardinality, entity, colon, verb. 2 bars mean exactly one, a crow's foot means many, and a junction entity fixes many-to-many.
Common mistakes
- A label with a space but no quotes. PRODUCT ||--o{ ORDER_LINE : appears in will not render. Put quotes around any label longer than one word, like "appears in".
- Reading the cardinality from the wrong side. Each symbol describes the entity it sits next to. In CUSTOMER ||--o{ ORDER, the o{ is next to ORDER, so it means a customer has zero or many orders, not the other way round.
- Linking 2 entities many-to-many directly. Mermaid will draw ORDER }o--o{ PRODUCT, but there is nowhere to store the quantity. Add a junction entity like ORDER_LINE with a foreign key to each side.
- Putting the foreign key on the one side. customer_id FK belongs in ORDER, the many side, because each order has one customer. A customer can't hold one order_id for all of their orders.
When a business analyst uses this
In a requirements workshop for a new ordering system, a BA types the ER diagram in the Mermaid live editor while stakeholders watch. They read each line aloud, such as "one order contains one or many order lines", and fix the business rules on the spot. Then they paste the code into the requirements document so the model can be updated later.
Questions people ask
What is a Mermaid ER diagram?
It is an entity relationship diagram written as text in Mermaid. You start with erDiagram, list the relationships and attributes, and Mermaid draws the boxes and crow's foot lines for you.
What is the syntax for relationships in a Mermaid ER diagram?
Write entity, cardinality, entity, colon, label, like CUSTOMER ||--o{ ORDER : places. || means exactly one, o{ means zero or many and |{ means one or many. A label with a space needs quotes.
Can I make a Mermaid ER diagram online?
Yes. The free Mermaid live editor runs in your browser and shows the diagram next to your code, so you don't need to install anything.