Skip to content

5.2. Database Design Rules

5.2.1. Table Naming

RuleExample
Lowercase + snake_caseuser_profiles
Use plural formusers, orders
No prefixestbl_users (X) → users (O)
No reserved wordsuser (X) → users (O)

5.2.2. Column Naming

RuleExample
Lowercase + snake_casecreated_at, user_name
No table name prefixuser_user_name (X) → user_name (O)
Boolean columnsis_ or has_ prefix
Date/time columns_at suffix

5.2.3. Required Columns

All tables must include the following columns.

ColumnTypeDescription
idBIGSERIAL or UUIDPrimary key
created_atTIMESTAMP WITH TIME ZONECreation timestamp
updated_atTIMESTAMP WITH TIME ZONEModification timestamp
sql
CREATE TABLE users (
    id          BIGSERIAL PRIMARY KEY,
    name        VARCHAR(100) NOT NULL,
    email       VARCHAR(255) NOT NULL UNIQUE,
    is_active   BOOLEAN NOT NULL DEFAULT true,
    created_at  TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
    updated_at  TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);

5.2.4. Primary Key Strategy

StrategyTypeUsage Criteria
BIGSERIALAuto-incrementing integerGeneral business entities (default)
UUIDuuidDistributed environments, externally exposed identifiers

When using UUID:

sql
CREATE EXTENSION IF NOT EXISTS "pgcrypto";

CREATE TABLE documents (
    id          UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    title       VARCHAR(255) NOT NULL,
    created_at  TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
    updated_at  TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);

5.2.5. Index Naming

idx_{table_name}_{column_name}
TypeNaming ConventionExample
Regular indexidx_{table}_{column}idx_users_email
Composite indexidx_{table}_{col1}_{col2}idx_orders_user_id_status
Unique indexuniq_{table}_{column}uniq_users_email
sql
CREATE INDEX idx_users_email ON users (email);
CREATE UNIQUE INDEX uniq_users_email ON users (email);
CREATE INDEX idx_orders_user_id_status ON orders (user_id, status);

5.2.6. Foreign Key Naming

fk_{table_name}_{referenced_table_name}
sql
ALTER TABLE orders
    ADD CONSTRAINT fk_orders_users
    FOREIGN KEY (user_id) REFERENCES users (id);

5.2.7. Prohibited Practices

Prohibited ItemReason
Composite primary keysIncreases join complexity, complicates jOOQ code generation
Overuse of TEXT typeColumns without length limits should only be used when explicitly necessary
FLOAT / DOUBLEPrecision issues with financial data → use NUMERIC
Reserved word table/column namesSQL compatibility issues
Trigger-based business logicMust be handled in the application layer

TIENIPIA QUALIFIED STANDARD