ER Diagram Basics (Entities, Attributes, Relationships)
An Entity-Relationship (ER) Diagram visually represents the structure of your database — what tables exist, what columns they have, and how they relate. Learning to read and draw ER diagrams is essential for database design and communication with teammates.
ER Diagram Components
- Entity — a table: users, products, orders, payments (rectangle)
- Attribute — a column: name, email, price (oval or listed in rectangle)
- Primary Key — underlined attribute or 🔑 icon
- Relationship — line connecting two entities (diamond or line)
- Cardinality — 1:1, 1:N (one-to-many), M:N (many-to-many)
- Crow's foot notation — most common modern ER diagram style
ecommerce_db ER Diagram
-- ecommerce_db Relationships:
--
-- users (1) ──────── (N) orders
-- id PK id PK
-- name user_id FK → users.id
-- email total_amount
-- created_at status
-- created_at
--
-- orders (1) ──────── (1) payments
-- id PK id PK
-- order_id FK → orders.id
-- amount
-- payment_method
-- paid_at
--
-- orders (M) ──────── (N) products
-- (via order_items junction table)
-- order_items:
-- order_id FK → orders.id ┐ Composite PK
-- product_id FK → products.id┘
-- quantity
-- Cardinality notation:
-- 1 user → MANY orders (one-to-many)
-- 1 order → 1 payment (one-to-one)
-- MANY orders ↔ MANY products (many-to-many via order_items)
-- Text ER notation (for documentation):
-- [users] 1──N [orders] 1──1 [payments]
-- N
-- |
-- M
-- [products]
-- (via order_items)Quick Quiz
Tip
Tip
Practice ER Diagram Basics Entities Attributes Relationships in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Relationships define how tables connect through foreign keys
Common Mistake
Warning
A common mistake with ER Diagram Basics Entities Attributes Relationships is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready sql code.
Practice Task
Note
Practice Task — (1) Write a working example of ER Diagram Basics Entities Attributes Relationships from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Key Takeaways
- An Entity-Relationship (ER) Diagram visually represents the structure of your database — what tables exist, what columns they have, and how they relate.
- Entity — a table: users, products, orders, payments (rectangle)
- Attribute — a column: name, email, price (oval or listed in rectangle)
- Primary Key — underlined attribute or 🔑 icon