Example
erDiagram
USERS {
text id PK
text email
text plan
int created_at
}
DIAGRAMS {
text id PK
text code
text r2_key
int view_count
int last_viewed_at
text user_id FK
}
USERS ||--o{ DIAGRAMS : "owns" Syntax
Entities and attributes
erDiagram
USER {
uuid id PK
text email UK
timestamp created_at
}
POST {
uuid id PK
uuid user_id FK
text title
text body "markdown"
} Attribute lines are type name [PK|FK|UK] ["comment"]. Types are free text; Mermaid does not validate them.
Relationships
erDiagram
USER ||--o{ POST : writes
POST ||--|{ COMMENT : has
USER }o--o{ ROLE : assigned
CART ||..|| ORDER : becomes Left token, line, right token. || exactly one, |o zero or one, }o zero or more, }| one or more. -- identifying, .. non-identifying.
Aliases and styling
erDiagram
"Order Line" {
int qty
}
ORDER ||--|{ "Order Line" : contains Quote an entity name to use spaces. Relationship labels are free text and appear on the line.
Try this snippet →Common mistakes
These are the parse errors that come up most, including in code written by ChatGPT and Claude. Paste any failing diagram into the editor and it marks the offending line.
-
Cardinality token reversed:
POST o{--|| USER.Fix: The symbol closest to each entity describes that entity's side:
USER ||--o{ POSTmeans one user, zero-or-more posts. -
Attribute type and name swapped (
id uuid PK).Fix: Type comes first:
uuid id PK. Swapping them renders but labels the column "uuid". -
Lowercase entity names mixed with uppercase ones.
Fix: Mermaid accepts both, but the convention is uppercase. Pick one style; AI tools follow whatever your example uses.
Real-world examples
Complete, copy-ready entity relationships for common situations. Each one opens in the editor with a click.
-
E-commerce Database ER Diagram with Orders and Payments
A Mermaid ER diagram for an online store: customers, addresses, orders, order items, products, categories, and payments, with typed keys and cardinality.
-
Blog CMS Database ER Diagram: Posts, Tags, and Comments
A Mermaid ER diagram for a blog or CMS: users, posts, comments, tags through a post_tags join table, and media attachments, with typed keys and cardinality.
FAQ
Can I generate an ER diagram from an existing schema?
Not inside Mermaid, but it is a good prompt: paste your CREATE TABLE statements into ChatGPT or Claude and ask for a Mermaid erDiagram with PK/FK markers. Then paste the result into MermanDraw to check it renders.
Does the ER diagram support composite keys?
Mark each column with PK. There is no syntax for a combined constraint, so note it in a comment string on the attribute.