← Back to SQL, page 11

sql-0205 · Medium mid

Design a schema for a blog where posts have many comments, comments can reply to other comments, and posts have many tags.

Use posts, comments with post_id and a nullable parent_id referencing comments, tags, and a post_tags junction table. parent_id NULL means a top-level comment. Index comments(post_id) and comments(parent_id), and read threads with a recursive CTE.

Full answer
sql
CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE comments (
  id        INTEGER PRIMARY KEY,
  post_id   INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
  parent_id INTEGER REFERENCES comments(id),
  body      TEXT NOT NULL
);
CREATE INDEX comments_post ON comments(post_id);
CREATE INDEX comments_parent ON comments(parent_id);
CREATE TABLE tags (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE);
CREATE TABLE post_tags (post_id INTEGER REFERENCES posts(id), tag_id INTEGER REFERENCES tags(id), PRIMARY KEY (post_id, tag_id));
INSERT INTO posts VALUES (1, 'Window functions');
INSERT INTO comments VALUES (1, 1, NULL, 'Great post'), (2, 1, 1, 'Agreed'), (3, 1, 2, 'Same');
WITH RECURSIVE t(id, depth) AS (
  SELECT id, 0 FROM comments WHERE post_id = 1 AND parent_id IS NULL
  UNION ALL SELECT c.id, t.depth + 1 FROM comments c JOIN t ON c.parent_id = t.id
)
SELECT id, depth FROM t ORDER BY id;
iddepth
10
21
32

Design notes:

  • comments.parent_id references the same table, which models any depth of replies. A recursive CTE reads a thread, as in the hierarchy questions in this bank.
  • post_id is stored on every comment, including replies, so "all comments on a post" needs no recursion.
  • tags.name is UNIQUE, and post_tags uses a composite primary key.
  • ON DELETE CASCADE from posts to comments fits, because comments mean nothing without the post. For replies, many products keep the reply and show "comment deleted" instead, which suggests a soft delete flag rather than a cascade.

Questions worth asking: are comments moderated (a status column), can authors edit (an edited_at column or history), and how deep can threads go in the interface?

sql
CREATE TABLE posts (id INTEGER PRIMARY KEY, title TEXT NOT NULL);
CREATE TABLE comments (
  id        INTEGER PRIMARY KEY,
  post_id   INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
  parent_id INTEGER REFERENCES comments(id),
  body      TEXT NOT NULL
);
CREATE INDEX comments_post ON comments(post_id);
CREATE INDEX comments_parent ON comments(parent_id);
CREATE TABLE tags (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE);
CREATE TABLE post_tags (post_id INTEGER REFERENCES posts(id), tag_id INTEGER REFERENCES tags(id), PRIMARY KEY (post_id, tag_id));
INSERT INTO posts VALUES (1, 'Window functions');
INSERT INTO comments VALUES (1, 1, NULL, 'Great post'), (2, 1, 1, 'Agreed'), (3, 1, 2, 'Same');
WITH RECURSIVE t(id, depth) AS (
  SELECT id, 0 FROM comments WHERE post_id = 1 AND parent_id IS NULL
  UNION ALL SELECT c.id, t.depth + 1 FROM comments c JOIN t ON c.parent_id = t.id
)
SELECT id, depth FROM t ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Self-referencing parent_id models reply trees
  • Store post_id on every comment for flat reads
  • Tags use a junction table
  • Decide between cascade and soft delete for replies

Common mistakes

  • Storing replies in a separate table per level
  • Comma-separated tags on the post

Follow-ups

  • How would you fetch a thread in display order?
  • How would you count comments per post efficiently?

Tips

  • State the delete behaviour you chose and why