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
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
| Shape | Symbol | Connections |
|---|---|---|
| Users | Entity | holds user_id, nickname · → Post(writes), Comment(writes) |
| Post | Entity | holds post_id, user_id, title, created_at · → Comment(has), PostTag(tagged) · ← Users |
| Comment | Entity | holds comment_id, post_id, user_id · ← Post, Users |
| Tag | Entity | holds tag_id, tag_name · → PostTag(used_in) |
| PostTag | Entity | holds post_id, tag_id · ← Post, Tag |
Symbols used here
| Symbol | Meaning |
|---|---|
| Entity | A 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 one | Two short bars at the line end. |
| Zero or many | A crow's foot and a circle. |
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