Learn
Entity relationship syntax
Learn relationships, cardinality markers, and attribute blocks used in the Simple data model template.
On this page9 sections
Syntax cheatsheet
Quick reference for this diagram type
| Concept | Syntax |
|---|---|
| Declare | erDiagram |
| Exactly one | || |
| Zero or one | |o / o| |
| One or more | }| / |{ |
| Zero or more | }o / o{ |
| Identifying | ||--o{ |
| Non-identifying | }o..o{ |
| Relationship | USER ||--o{ FOLDER : owns |
| Attributes | ENTITY { type name PK } |
- 1
Start an ER diagram
Begin with erDiagram. Entities are usually UPPER_SNAKE or Pascal names connected by relationship lines.
Each relationship needs a left cardinality, a line, a right cardinality, and a label after the colon.
erDiagram USER ||--o{ FOLDER : ownsExpand - 2
Cardinality markers
Cardinality describes how many instances participate. || is exactly one, o| zero or one, |{ one or more, o{ zero or more.
Read USER ||--o{ FOLDER : owns as “one user owns zero or more folders.”
- Tip: }o and |o appear on the opposite side of the line for the same meanings — pick a consistent left/right style.
erDiagram USER ||--o{ DIAGRAM : owns FOLDER ||--o{ DIAGRAM : contains TEAM ||--|{ MEMBER : includesExpand - 3
Identifying vs non-identifying
A solid relationship line (--) is identifying: the child depends on the parent’s identity.
A dashed line (.. ) is non-identifying: entities are related but independently identified. Use solid for ownership/containment, dashed for looser references.
erDiagram FOLDER ||--o{ DIAGRAM : contains USER }o..o{ DIAGRAM : favoritesExpand - 4
Entity attributes
List fields inside curly braces under an entity. Write type then name, one field per line.
Keep types simple (string, int, date) unless you need domain-specific precision.
- Tip: Mark keys with PK, FK, or UK after the attribute name. Multiple keys can be comma-separated (PK,FK).
erDiagram USER { string id string email } FOLDER { string id string name }Expand - 5
Keys and foreign references
Attributes support key constraints: PK (primary), FK (foreign), and UK (unique). Separate multiple keys with commas.
Add an optional comment in double quotes after the keys to document the field.
- Tip: Optional types end with ? on the type (string? nickname). Primary-key names may start with *.
erDiagram FOLDER ||--o{ DIAGRAM : contains FOLDER { string id PK } DIAGRAM { string id PK string folderId FK string title }Expand - 6
Multiple relationships
One entity can participate in many relationships. USER may own folders and diagrams while FOLDER contains diagrams.
Draw each relationship on its own line; don’t try to compress them into one statement.
erDiagram USER ||--o{ FOLDER : owns USER ||--o{ DIAGRAM : owns FOLDER ||--o{ DIAGRAM : containsExpand - 7
Put it together: Simple data model
Combine ownership, containment, and attribute blocks into the examples template model.
erDiagram USER ||--o{ FOLDER : owns USER ||--o{ DIAGRAM : owns FOLDER ||--o{ DIAGRAM : contains USER }o..o{ DIAGRAM : favorites USER { string id PK string email UK } FOLDER { string id PK string name } DIAGRAM { string id PK string folderId FK string title string visibility }Expand
Check your level
80% or higher certifies this lesson. You'll get 3–5 random questions from a larger bank.