Relational Modeling
Start by identifying entities (Users, Orders, Products) and the relationships between them. Normalize to reduce duplication and improve consistency.
Keys & Constraints
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id),
title TEXT NOT NULL,
body TEXT,
published BOOLEAN DEFAULT false,
created_at TIMESTAMP DEFAULT NOW()
);
Best Practices
- • Use
PRIMARY KEYfor stable identifiers - • Add
UNIQUEconstraints for emails, slugs, etc. - • Use
NOT NULLfor required fields - • Prefer
timestampcolumns for auditing