Database relationship modeling

Mermaid ER Diagram Online Editor

Model database entities, keys, and relationships with the Mermaid ER diagram editor above. Use explicit cardinality to show whether related records are required or optional, then export the ERD for a schema review.

Build a Mermaid ER diagram online

Use the customer, order, and line-item example below to inspect both ends of each relationship. Your existing workspace draft takes priority over the example.

OnUML
Loading diagram preview...

Read the constraints between records

An ER diagram helps answer how records relate, which keys identify them, and whether a relationship is optional. It describes the model; it does not connect to or inspect a database.

Entities and attributes

Group the fields needed for a design discussion under the entity that owns them.

Required or optional records

Use relationship endpoints to distinguish exactly one, zero-or-one, one-or-more, and zero-or-more.

Primary, foreign, and unique keys

Mark PK, FK, and UK so a reviewer can understand identity and references in the model.

Meaningful relationship labels

Use a verb such as places or contains to explain why the entities are linked.

Create an ERD for a schema review

Choose one part of the schema and verify both ends of every relationship.

  1. 01

    Start with erDiagram, list the entities, and add attributes using type name and optional key markers.

  2. 02

    Connect entities with endpoint symbols that match requiredness and multiplicity, then label each relationship with a verb.

  3. 03

    Compare the preview with your actual schema rules. Export SVG or PNG and keep the source alongside the schema documentation.

ER syntax for cardinality, keys, and relationship identity

Cardinality and identifying relationships answer different questions. A key marker documents a field; it does not automatically create or validate a database constraint.

Mermaid ER diagram syntax reference →

Read both cardinality endpoints

|| means exactly one. o| or |o means zero-or-one; }| or |{ means one-or-more; }o or o{ means zero-or-more. The symbol orientation depends on the endpoint.

erDiagram
    CUSTOMER ||..o{ ORDER : places
    ORDER ||--|{ LINE_ITEM : contains

Attributes and key markers

Write the type first, then the attribute name, then PK, FK, or UK when relevant. An attribute may have multiple key roles, separated by commas.

erDiagram
    ORDER {
        string id PK
        string customer_id FK
        date created_at
    }

Identifying and non-identifying relationships

Use -- for an identifying relationship and .. for a non-identifying relationship. In this example an order has its own ID and a customer reference.

erDiagram
    CUSTOMER ||..o{ ORDER : places
    CUSTOMER {
        string id PK
    }
    ORDER {
        string id PK
        string customer_id FK
    }

A join entity for many-to-many data

Mermaid can draw a direct many-to-many relationship. For a relational schema, an associative entity is often more useful because it can hold the references and attributes of that relationship.

erDiagram
    STUDENT ||--o{ ENROLLMENT : has
    COURSE ||--o{ ENROLLMENT : includes
    ENROLLMENT {
        string student_id PK, FK
        string course_id PK, FK
        date enrolled_at
    }

Mermaid ER diagram example

The customer–order–line-item model shows how to read optional orders and required line items while keeping selected key annotations visible.

erDiagram
    CUSTOMER ||--o{ ORDER : places
    ORDER ||--|{ LINE_ITEM : contains
    CUSTOMER {
        string id PK
        string email UK
    }
    ORDER {
        string id PK
        string customer_id FK
        date created_at
    }
    LINE_ITEM {
        string order_id FK
        string sku
        int quantity
    }
  • CUSTOMER ||--o{ ORDER states that an order has one customer and a customer may have zero or more orders.
  • ORDER ||--|{ LINE_ITEM states that an order contains at least one line item.
  • Check identifying versus non-identifying lines against your chosen keys. Try changing |{ to o{ if a draft order may have no line items.

Frequently asked questions

What do ||, o{, and |{ mean?

|| means exactly one at that endpoint. o{ means zero-or-more; |{ means one-or-more. Read the other endpoint as well to understand the full relationship.

Do PK and FK markers check the database?

No. PK, FK, and UK are annotations in the diagram. This editor does not connect to a database, inspect its schema, or check foreign-key values.

How do I model a many-to-many relationship?

You can draw it directly in Mermaid. When documenting a relational schema, consider a join entity with the two references and any relationship attributes, such as an enrollment date.

What is the difference between -- and ..?

They distinguish identifying from non-identifying relationships. This is separate from the endpoint symbols that express how many records may participate.

Should I include every database column?

Include columns that explain identity, relationships, or the design being reviewed. Leave unrelated columns out when they make the ERD harder to read.

When is a class diagram a better choice?

Use a class diagram for methods, interfaces, inheritance, and software responsibilities. Use an ER diagram for persisted entities, keys, and database cardinality.

Compare ER notations

Mermaid vs PlantUML for ER diagrams

Compare syntax, relationship notation, database modeling depth, and maintainability for text-based ERDs.

Read the ER diagram comparison →