Deriving a Blog Database from Business Rules, Not Syntax
Many developers memorize CREATE TABLE syntax but freeze when facing a blank schema for a real project. This derivation-first approach — asking four questions per table and letting query patterns dictate indexes — replaces guesswork with a repeatable method that works for any relational model.
Database design starts with the business, not with CREATE TABLE syntax. This walkthrough derives three core tables — user, avatar, and post — by repeatedly asking what a table stores, what one row represents, how to uniquely find it, and how it relates to other tables. Each design decision (PRIMARY KEY, UNIQUE, FOREIGN KEY, INDEX) is traced back to a concrete query or integrity requirement: id as a stable identifier, username uniqueness for both constraint and login performance, userId with a foreign key to guarantee referential integrity, and an index on userId because the system frequently queries avatars and articles by owner.
The post table introduces the one-to-many pattern: store the "one" side's id on the "many" side. A user writes many articles, so post.userId points to user.id. The same reasoning applies to categories, departments, and orders. Constraints are reframed as business rules written into the database — NOT NULL, UNIQUE, FOREIGN KEY — that prevent bad data from entering even when application code misses a check.
The real takeaway is the derivation habit itself. When you can start from a business requirement and reason your way to fields, primary keys, indexes, and foreign keys without memorizing syntax, SQL becomes a tool rather than a barrier.
The article reframes database constraints not as technical overhead but as a second layer of business-logic enforcement that catches mistakes the application code misses.
UNIQUE is presented as a dual-purpose mechanism — constraint plus index — which is a mental model many tutorials skip, leaving developers to add redundant indexes later.
The one-to-many explanation avoids abstract ERD notation and instead uses a concrete rule: store the parent's id on the child row. This makes the pattern instantly transferable to any domain.
By splitting avatar into its own table, the article teaches foreign keys and indexing in a context where the design choice is debatable (small projects could inline avatar_url), making the pedagogical intent transparent rather than dogmatic.