Part 33: Creating Tables: CREATE TABLE Syntax & Best Practices
A clean, normalized table definition is the bedrock of database stability.
1. Production Table Example
CREATE TABLE IF NOT EXISTS users (
id BIGSERIAL PRIMARY KEY,
uuid UUID NOT NULL DEFAULT gen_random_uuid() UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
2. Table Creation Best Practices
- Explicit Nullability: Always declare
NOT NULLunless null is explicitly meaningful. - Singular vs Plural Naming: Standardize across your project (e.g.
usersoruser). - Always Include Audit Columns:
created_atandupdated_at.
Key Takeaways
- Use explicit data types and non-null constraints.
- Provide sensible default values.
In Part 34, we explore Primary Keys: Natural vs Surrogate Keys and UUIDs vs Auto-Increment!

