Labs ICT
⭐ Pro Login

ER Diagrams

How to design databases using Entity-Relationship modeling.

Designing Before Building

Before you create a single table, you need to design your database. ER diagrams (Entity-Relationship diagrams) are the standard way to visualize and plan database schemas. They help you identify entities, their attributes, and how they relate to each other.

Entities and Attributes

  • Entity β€” A thing or concept you want to store data about. Represented as a rectangle. Examples: Student, Course, Order.
  • Attribute β€” A property of an entity. Represented as an oval. Examples: Name, Email, Price.
  • Key Attribute β€” An attribute that uniquely identifies an entity. Underlined in the diagram.

ER Diagram Notation


  Chen Notation (classic):

  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Student  β”‚         β”‚ Course   β”‚
  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜
       β”‚                     β”‚
  β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”           β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”
  β”‚ StudentIDβ”‚           β”‚ CourseIDβ”‚
  β”‚ Name    β”‚           β”‚ Title   β”‚
  β”‚ Email   β”‚           β”‚ Credits β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

  Relationship: β—‡ Enrolls β—‡
  ──○ M ──◇── 1 ──○──
  Student       Enrolls     Course
  (Many)                   (Many)

Cardinality

Cardinality defines how many instances of one entity can relate to instances of another:


  One-to-One (1:1):       One-to-Many (1:N):     Many-to-Many (M:N):
  β”Œβ”€β”€β”€β”   β”Œβ”€β”€β”€β”          β”Œβ”€β”€β”€β”   β”Œβ”€β”€β”€β”          β”Œβ”€β”€β”€β”   β”Œβ”€β”€β”€β”
  β”‚ A │───│ B β”‚          β”‚ A │───│ B β”‚          β”‚ A │───│ B β”‚
  β””β”€β”€β”€β”˜   β””β”€β”€β”€β”˜          β””β”€β”€β”€β”˜ 1β”‚  β””β”€β”€β”€β”˜         β””β”€β”€β”€β”˜ Mβ”‚  β””β”€β”€β”€β”˜
  1:1                      1β”‚ N                    Mβ”‚ N
                           (deptsβ†’emps)         (students↔courses)

Example ER Diagram: University


  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚Student │──── M ───  Enrolls   β”œβ”€β”€ N ────│Course  β”‚
  β”‚--------β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚--------β”‚
  β”‚StdID(PK)β”‚         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚CrsID(PK)β”‚
  β”‚Name    │──── 1 ─── Belongs To β”œβ”€β”€ N ────│Title   β”‚
  β”‚Email   β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚Credits β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚                                        β”‚
       β”‚ N                                      β”‚ 1
       β–Ό                                        β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Department β”‚                         β”‚ Instructor β”‚
  β”‚------------β”‚                         β”‚------------β”‚
  β”‚DeptID (PK) β”‚                         β”‚InsID (PK)  β”‚
  β”‚DeptName    β”‚                         β”‚Name        β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                         β”‚DeptID (FK) β”‚
                                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

From ER Diagram to Tables

Once you have your ER diagram, converting it to tables is straightforward:

  • Each entity becomes a table.
  • Each attribute becomes a column.
  • 1:N relationships: put the primary key of the "1" side as a foreign key in the "N" side.
  • M:N relationships: create a junction table with foreign keys from both sides.