Database schema design: get it right before you have users

Database schema design: get it right before you have users

Application code is easy to refactor. A schema with a million rows and foreign keys pointing at it is not. A few hours of design now saves a migration weekend later.

Model the domain, then the screens

Start from the real-world entities and their relationships. Build tables for those. Resist shaping the schema around the first UI — screens change, the domain rarely does.

Non-negotiables

  • A surrogate primary key on every table, plus unique constraints on the natural keys.
  • Foreign keys with explicit ON DELETE behaviour — the database should not allow orphans.
  • Money as decimal or integer minor units; timestamps in UTC; enums as lookup tables or checked columns, not free text.
  • created_at / updated_at everywhere. Add deleted_at only where you actually need soft deletes.

Index with the query plan in mind

Index the columns you filter and join on, and composite-index the common multi-column filters. Then read the query plan on realistic data volumes — not ten test rows.

Plan for history

If anyone will ever ask "what did this look like last month", design an audit table or an append-only event log now. Reconstructing history after the fact is guesswork.

Normalise first, denormalise on evidence

Start with a normalised design: each fact stored once. It keeps data consistent and makes changes safe. Denormalise only when you have measured a real performance problem — for example, a dashboard query that aggregates millions of rows — and then do it deliberately, with a clear process to keep the copies in sync.

Multi-tenant data

If one database serves many customers, put the tenant ID on every tenant-owned table, include it in indexes and unique constraints, and enforce it in a shared query layer. A missing tenant filter is one of the most damaging bugs a SaaS product can ship.

Migrations you can roll back

  • Make changes in small, reversible steps.
  • Add columns as nullable or with defaults, backfill, then add constraints.
  • Rename in two releases: add the new column, move code over, then drop the old one.
  • Test migrations against a copy of production-sized data — a migration that locks a big table can take the site down.

Data that needs special care

Personal data, payment references and identity documents need access controls, minimal retention and encryption where appropriate. Decide early which columns hold sensitive data and who may read them; retrofitting privacy is much harder than designing for it.

Backups are part of the schema

Automated backups with point-in-time recovery, stored separately, and a restore you have actually tested. A backup that has never been restored is a hope, not a backup.

Talk to us

Designing a system from scratch? See our custom software development, or get a free quote — we reply within one business day.

Need a team to build it?