← All system designs

Data Modeling

ER modeling — entities, attributes, relationships, constraints, weak entities — and translating ER to relational tables.

5 items 2 Foundational 3 Intermediate

Data modeling is the part of database work that happens before any code. Get the entities and relationships right and the SQL is straightforward; get them wrong and you'll spend years migrating around it. The ER model is the canonical vocabulary — entity sets, attributes, primary keys, relationship types, cardinality constraints — and every relational schema is some translation of an ER diagram.

For interview prep, practice on a small business domain (e.g., 'design the schema for a library / e-commerce site / ticketing system'). The pattern is the same: name the entities, the attributes, the keys, then the relationships with their cardinalities, then translate to tables. Five entities, two relationships, twenty minutes — anything more is usually scope creep.

Key concepts

  • Entities are the nouns; relationships are the verbs; attributes are the adjectives
  • Primary key choice is a contract — surrogate keys default to the right answer
  • Cardinality constraints (1-1, 1-N, M-N) drive whether you need a junction table
  • Weak entities exist; their identifier is composite and depends on the parent
  • Translation rules: each entity → table; each M-N relationship → junction table; each 1-N relationship → FK on the many side

Reference template

// ER design walk-through
1. List the entities         (concrete real-world things)
2. List attributes per entity (note primary key)
3. List relationships        (degree, cardinality, participation)
4. Identify weak entities    (no independent identity)
5. Translate to tables       (apply the rules)
6. Add constraints           (NOT NULL, UNIQUE, FK ON DELETE)

Adapt to your problem; the structure is the load-bearing part.

Common pitfalls

  • Putting business logic in keys — natural keys break when business rules change
  • Premature denormalization — start in 3NF, denormalize only with a measurement
  • Missing the M-N case — two FKs and forgetting the junction table is a classic
  • Ignoring participation constraints — ON DELETE semantics are not optional

Related topics

Items (5)

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.