FMTechnical notesPortfolio
Back to the blog

Backend · Intermediate

Database modeling and ER diagrams

A healthy database starts with business language. Tables come after understanding entities, identities, and relationships.

After reading this
  • Identify entities
  • Model cardinality
  • Avoid unnecessary duplication
01 / 03

From nouns to entities

Customer, order, and product can be entities. Address may be an attribute or an entity depending on its own identity and behavior.

  • Primary key: stable identity.
  • Foreign key: explicit relationship.
  • Cardinality: one-to-one, one-to-many, or many-to-many.
02 / 03

A many-to-many relationship

An order has many products and a product appears in many orders. The join table also stores quantity and historical price.

sql
CREATE TABLE order_items (
  order_id UUID REFERENCES orders(id),
  product_id UUID REFERENCES products(id),
  quantity INTEGER NOT NULL CHECK (quantity > 0),
  unit_price NUMERIC(12, 2) NOT NULL,
  PRIMARY KEY (order_id, product_id)
);
03 / 03

Normalize with judgment

Normalization reduces contradictions. Denormalization can speed known reads. The decision needs a real case and a consistency strategy.

Relation model● interactive
USERORDERITEMPRODUCT
Practice

Model a domain

Design entities and relationships for courses, students, and enrollments with date and status.

Show a hint

Enrollment is the many-to-many relationship and owns data.