Class Diagram vs ER Diagram: Key Differences and When to Use Each
Table of Contents
Both class diagrams and ER diagrams (entity-relationship diagrams) show things and their relationships. Both have boxes, connecting lines, and notation for attributes. So what is the difference — and which one should you use?
The short answer: class diagrams are for object-oriented code. ER diagrams are for relational databases. They solve related but distinct problems, and understanding the distinction helps you pick the right tool for the right conversation.
What Each Diagram Represents
UML Class Diagram: Represents the structure of an object-oriented system — classes in code, their attributes and methods, and the relationships between class instances at runtime (inheritance, composition, association). Class diagrams describe behavior (methods) as well as data.
ER Diagram: Represents the structure of a relational database — entities (tables), their attributes (columns), and the relationships between them (foreign key relationships). ER diagrams describe data only — no behavior, no methods, no inheritance in the traditional sense.
The conceptual overlap is real: both diagrams show "things" (classes vs entities) with "properties" (attributes) and "connections" (relationships). But they target different layers of a system.
Key Notation Differences
| Feature | UML Class Diagram | ER Diagram |
|---|---|---|
| Box represents | A class (code blueprint) | An entity (table) |
| Methods shown? | Yes | No |
| Inheritance arrows? | Yes (<|--) | No (not in relational DBs) |
| Relationship lines | 6 types (inheritance, composition, etc.) | Crow's foot / Chen notation |
| Cardinality notation | Quoted numbers ("1", "0..*") | Crow's foot symbols on lines |
| Primary key | Not specified | Underlined attribute |
| Common tools | Mermaid, draw.io, PlantUML | dbdiagram.io, Lucidchart, MySQL Workbench |
When to Use a Class Diagram
Use a class diagram when you are designing or documenting object-oriented code:
- Planning a class hierarchy before writing code (inheritance relationships)
- Documenting a service layer for other engineers
- Explaining how a library or framework's objects relate
- CS homework or system design interviews where UML is expected
- Showing the domain model of a business system (Customer, Order, Product)
Class diagrams work at the application layer — how your code is organized in memory at runtime.
When to Use an ER Diagram
Use an ER diagram when you are designing or documenting a relational database:
- Designing the schema before creating tables
- Documenting which tables relate via foreign keys
- Planning data migrations between schemas
- Communicating database structure to a DBA or data team
- Showing how normalized tables map to each other
ER diagrams work at the persistence layer — how data is stored in tables on disk.
When You Might Use Both in the Same Project
Many projects use both diagrams for different audiences:
- The class diagram documents the domain model for the backend engineering team
- The ER diagram documents the database schema for the data team and DBA
The two are related but not identical. A class diagram might have an Order class with methods like calculateTotal(). The ER diagram has an orders table with columns like total_amount. Same concept, different views.
In object-relational mapping (ORM) frameworks, the class diagram often closely mirrors the ER diagram — but the class diagram still adds methods and inheritance that have no ER equivalent.
Try It Free — No Signup Required
Runs 100% in your browser. No data is collected, stored, or sent anywhere.
Open Free Class Diagram ToolFrequently Asked Questions
Can I use a UML class diagram to design a database?
You can use a class diagram as a starting point for database design, especially the entity/attribute structure. But you will need to translate it to an ER diagram to capture primary keys, foreign keys, normalization, and join tables — concepts that class diagrams do not represent.
What is the main thing ER diagrams have that class diagrams do not?
Primary keys and foreign key relationships are natively expressed in ER diagrams. Class diagrams focus on object structure and behavior; they do not have a standard notation for primary keys.
Is Mermaid good for ER diagrams too?
Yes, Mermaid supports ER diagrams with erDiagram syntax. For class diagrams, use classDiagram. Both are text-based and supported in GitHub and other tools.
Which diagram should I draw first when designing a new system?
Start with a class diagram to model the domain objects and their relationships — this captures the business logic. Then create an ER diagram when you get to database design. The class diagram often informs the ER diagram structure.

