Skip to main content

Lesson 6 of 7

Database Schema - Blog System Tables

A well-designed schema is the foundation of a reliable application. Primary key choice, foreign key constraints, index strategy, and data type selection all directly affect your application's performance, integrity, and safety. For the Blog System, we design tables that will be used across all remaining modules.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Blog System Schema Design

Blog System Schema Design
-- ── Blog System - Complete Schema ────────────────────────────

-- Users table
CREATE TABLE users (
    id           INT          UNSIGNED NOT NULL AUTO_INCREMENT,
    name         VARCHAR(100) NOT NULL,
    email        VARCHAR(255) NOT NULL,
    password     VARCHAR(255) NOT NULL,       -- bcrypt hash
    role         ENUM('guest','author','editor','admin') NOT NULL DEFAULT 'author',
    created_at   DATETIME     NOT NULL DEFAULT UTC_TIMESTAMP(),
    updated_at   DATETIME     NULL,
    deleted_at   DATETIME     NULL,            -- soft delete
    PRIMARY KEY (id),
    UNIQUE KEY uq_users_email (email),
    INDEX idx_users_role (role)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Posts table
CREATE TABLE posts (
    id           INT          UNSIGNED NOT NULL AUTO_INCREMENT,
    title        VARCHAR(255) NOT NULL,
    slug         VARCHAR(255) NOT NULL,
    body         LONGTEXT     NOT NULL,
    status       ENUM('draft','published','archived') NOT NULL DEFAULT 'draft',
    author_id    INT          UNSIGNED NOT NULL,
    published_at DATETIME     NULL,
    created_at   DATETIME     NOT NULL DEFAULT UTC_TIMESTAMP(),
    updated_at   DATETIME     NULL,
    views        INT          UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (id),
    UNIQUE KEY uq_posts_slug (slug),
    INDEX idx_posts_status_published (status, published_at DESC),
    INDEX idx_posts_author (author_id),
    CONSTRAINT fk_posts_author FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Comments table
CREATE TABLE comments (
    id         INT      UNSIGNED NOT NULL AUTO_INCREMENT,
    post_id    INT      UNSIGNED NOT NULL,
    user_id    INT      UNSIGNED NULL,         -- null for guest comments
    body       TEXT     NOT NULL,
    approved   TINYINT(1) NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT UTC_TIMESTAMP(),
    PRIMARY KEY (id),
    INDEX idx_comments_post (post_id, approved),
    CONSTRAINT fk_comments_post FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
    CONSTRAINT fk_comments_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Seed data for development
INSERT INTO users (name, email, password, role) VALUES
    ('Alice Johnson', 'alice@example.com', '$2y$12$hashed_bcrypt_here', 'admin'),
    ('Bob Smith',     'bob@example.com',   '$2y$12$hashed_bcrypt_here', 'author');

INSERT INTO posts (title, slug, body, status, author_id, published_at) VALUES
    ('PHP Basics', 'php-basics', 'PHP is great...', 'published', 1, UTC_TIMESTAMP()),
    ('PDO Guide',  'pdo-guide',  'PDO is better...', 'published', 1, UTC_TIMESTAMP()),
    ('Draft Post', 'draft-post', 'Working on it...', 'draft', 2, NULL);

Quick Quiz

Tip

Tip

Practice Database Schema Blog System Tables in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Database Schema - Blog System Tables

Practice Task

Note

Practice Task - (1) Write a working example of Database Schema Blog System Tables from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Database Schema Blog System Tables is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/7 done