Diagram Desk

Blog ERD

User, post, comment and tag. Post-tag many-to-many goes through a join entity; a comment points to both its post and its author.

The diagram

UsersPK user_id intnickname varcharPostPK post_id intFK user_id inttitle varcharcreated_at datetimeCommentPK comment_id intFK post_id intFK user_id intTagPK tag_id inttag_name varcharPostTagPK post_id intFK tag_id intwriteshaswritestaggedused_in
ER diagram · 18 shapes

Reading it step by step

  • 'Users' has 2 attributes, primary key user_id.
  • 'Post' has 4 attributes, primary key post_id, foreign key user_id.
  • 'Comment' has 3 attributes, primary key comment_id, foreign key post_id, user_id.
  • 'Tag' has 2 attributes, primary key tag_id.
  • 'PostTag' has 2 attributes, primary key post_id, foreign key tag_id.
  • Each 'Users' relates to zero or more 'Post'; each 'Post' to exactly one 'Users' (writes).
  • Each 'Post' relates to zero or more 'Comment'; each 'Comment' to exactly one 'Post' (has).
  • Each 'Users' relates to zero or more 'Comment'; each 'Comment' to exactly one 'Users' (writes).
  • Each 'Post' relates to zero or more 'PostTag'; each 'PostTag' to exactly one 'Post' (tagged).
  • Each 'Tag' relates to zero or more 'PostTag'; each 'PostTag' to exactly one 'Tag' (used_in).

Shape by shape

ShapeSymbolConnections
UsersEntityholds user_id, nickname · → Post(writes), Comment(writes)
PostEntityholds post_id, user_id, title, created_at · → Comment(has), PostTag(tagged) · ← Users
CommentEntityholds comment_id, post_id, user_id · ← Post, Users
TagEntityholds tag_id, tag_name · → PostTag(used_in)
PostTagEntityholds post_id, tag_id · ← Post, Tag

Symbols used here

SymbolMeaning
EntityA thing to store: name on top, attributes below.
Primary key (PK)The attribute that identifies one row; underlined.
Foreign key (FK)An attribute pointing to another entity's key.
Exactly oneTwo short bars at the line end.
Zero or manyA crow's foot and a circle.

All er diagram symbols and how to draw

Diagram text

Paste this text into the editor to get the same picture, then change the names to fit your own work.

# Blog ERD
erDiagram
  Users {
    int user_id PK
    varchar nickname
  }
  Post {
    int post_id PK
    int user_id FK
    varchar title
    datetime created_at
  }
  Comment {
    int comment_id PK
    int post_id FK
    int user_id FK
  }
  Tag {
    int tag_id PK
    varchar tag_name
  }
  PostTag {
    int post_id PK
    int tag_id FK
  }
  Users ||--o{ Post : writes
  Post ||--o{ Comment : has
  Users ||--o{ Comment : writes
  Post ||--o{ PostTag : tagged
  Tag ||--o{ PostTag : used_in

More er diagram examples

All SeewolLabs tools