Coding
TEXT
SQL Schema Design Best Practices
October 2, 2025
Optimized for:
gpt-4o
Database design, schema planning, data modeling
Design database schema for [APPLICATION]: **Naming Conventions**: - Tables: plural, snake_case (users, order_items) - Columns: singular, snake_case (user_id, created_at) - Primary keys: id (integer, auto-increment) - Foreign keys: [table_singular]_id **Standard Columns** (all tables): - id: Primary key - created_at: Timestamp - updated_at: Timestamp - deleted_at: Soft deletes (optional) **Relationships**: - One-to-Many: Foreign key in child table - Many-to-Many: Junction table - One-to-One: Foreign key with UNIQUE constraint **Indexes**: - Primary key (automatic) - Foreign keys - Frequently queried columns - Composite indexes for multi-column queries **Data Types**: - Use appropriate sizes (VARCHAR vs TEXT) - ENUM for fixed options - JSON for flexible data (use sparingly) - UUID for distributed systems **Constraints**: - NOT NULL where appropriate - UNIQUE for natural keys - CHECK for validation - DEFAULT values **Normalization**: - 3NF for OLTP - Denormalize for read-heavy (with care) - Avoid EAV anti-pattern **Example Schema**: ```sql CREATE TABLE users ( id SERIAL PRIMARY KEY, email VARCHAR(255) UNIQUE NOT NULL, password_hash VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() ); CREATE INDEX idx_users_email ON users(email); ```
Database schema design with best practices, naming conventions, and indexing strategy.
Submit your own AI prompts to the community. The best ones get featured on TokenCalculator - and credited to you.