The Goal
Design and query a small blog. Posts have an author, a body that people can search, tags stored as a JSON array, and comments. You will use foreign keys, JSON, a full-text index, a multi-valued index and aggregates.
Step 1: Schema
sql
CREATE TABLE authors (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(40) NOT NULL, email VARCHAR(80) NOT NULL UNIQUE);
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
author_id INT NOT NULL,
title VARCHAR(120) NOT NULL,
body TEXT NOT NULL,
tags JSON NOT NULL,
published DATE NOT NULL,
CONSTRAINT fk_posts_author FOREIGN KEY (author_id) REFERENCES authors(id),
CONSTRAINT ck_posts_tags CHECK (JSON_TYPE(tags) = 'ARRAY'),
FULLTEXT KEY ft_posts (title, body),
INDEX idx_posts_tags ((CAST(tags AS CHAR(20) ARRAY))),
INDEX idx_posts_published (published)
);
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
post_id INT NOT NULL,
commenter VARCHAR(40) NOT NULL,
body VARCHAR(200) NOT NULL,
created DATE NOT NULL,
CONSTRAINT fk_comments_post FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE
);
Design choices: a CHECK makes sure tags is an array, comments are deleted with their post (ON DELETE CASCADE), the unique email prevents duplicate authors, and the full-text and multi-valued indexes serve search and tag filters.
sql
SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() ORDER BY TABLE_NAME;
Result:
| TABLE_NAME |
| authors |
| comments |
| posts |
Step 2: Seed Data
sql
INSERT INTO authors (name, email) VALUES ('Asha', 'asha@blog.dev'), ('Ben', 'ben@blog.dev'), ('Chen', 'chen@blog.dev');
INSERT INTO posts (author_id, title, body, tags, published) VALUES
(1, 'Getting started with MySQL', 'Install MySQL, create a database and write your first queries.', '["mysql", "beginner"]', '2025-01-10'),
(1, 'Indexes in plain words', 'An index speeds up searches. Learn composite and covering indexes.', '["mysql", "performance"]', '2025-02-02'),
(2, 'JSON columns in practice', 'Store flexible attributes in a JSON column and query them.', '["mysql", "json"]', '2025-02-20'),
(2, 'Backup basics', 'Dump your database and test the restore regularly.', '["backup", "beginner"]', '2025-03-05'),
(3, 'Python and databases', 'Connect Python to a database with a driver.', '["python", "beginner"]', '2025-03-18');
INSERT INTO comments (post_id, commenter, body, created) VALUES
(1, 'Dev', 'Great intro', '2025-01-11'), (1, 'Eli', 'Thanks', '2025-01-12'),
(3, 'Fay', 'JSON is handy', '2025-02-21'), (2, 'Gus', 'Clear explanation', '2025-02-03'),
(2, 'Hana', 'More examples please', '2025-02-05'), (2, 'Ivo', 'Helpful', '2025-02-06');
sql
SELECT (SELECT COUNT(*) FROM authors) AS authors, (SELECT COUNT(*) FROM posts) AS posts, (SELECT COUNT(*) FROM comments) AS comments;
Result:
Step 3: Tests of the Rules
sql
INSERT INTO posts (author_id, title, body, tags, published) VALUES (1, 'Bad', 'x', '"just-a-string"', '2025-04-01');
Tags must be an array
ERROR 3819: Check constraint 'ck_posts_tags' is violated.
sql
INSERT INTO posts (author_id, title, body, tags, published) VALUES (99, 'Orphan', 'x', '[]', '2025-04-01');
Author must exist
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails ('shop'.'posts', CONSTRAINT 'fk_posts_author' FOREIGN KEY ('author_id') REFERENCES 'authors' ('id'))
Query 1: Posts with Author and Comment Count
sql
SELECT p.id, p.title, a.name AS author, COUNT(c.id) AS comments FROM posts p JOIN authors a ON a.id = p.author_id LEFT JOIN comments c ON c.post_id = p.id GROUP BY p.id, p.title, a.name ORDER BY comments DESC, p.id;
Result:
| id | title | author | comments |
| 2 | Indexes in plain words | Asha | 3 |
| 1 | Getting started with MySQL | Asha | 2 |
| 3 | JSON columns in practice | Ben | 1 |
| 4 | Backup basics | Ben | 0 |
| 5 | Python and databases | Chen | 0 |
Query 2: Posts with a Tag (uses the multi-valued index)
sql
SELECT id, title FROM posts WHERE 'beginner' MEMBER OF (tags) ORDER BY published;
Result:
| id | title |
| 1 | Getting started with MySQL |
| 4 | Backup basics |
| 5 | Python and databases |
Query 3: How Many Posts per Tag?
sql
SELECT jt.tag, COUNT(*) AS posts FROM posts p, JSON_TABLE(p.tags, '$[*]' COLUMNS (tag VARCHAR(20) PATH '$')) AS jt GROUP BY jt.tag ORDER BY posts DESC, jt.tag;
Result:
| tag | posts |
| beginner | 3 |
| mysql | 3 |
| backup | 1 |
| json | 1 |
| performance | 1 |
| python | 1 |
Query 4: Full-Text Search
sql
SELECT id, title FROM posts WHERE MATCH(title, body) AGAINST ('database' IN NATURAL LANGUAGE MODE) ORDER BY id;
Result:
| id | title |
| 1 | Getting started with MySQL |
| 4 | Backup basics |
| 5 | Python and databases |
sql
SELECT id, title FROM posts WHERE MATCH(title, body) AGAINST ('+mysql -index*' IN BOOLEAN MODE) ORDER BY id;
Result:
| id | title |
| 1 | Getting started with MySQL |
Query 5: Authors Ranked by Comments Received
sql
SELECT a.name, COUNT(c.id) AS comments_received, RANK() OVER (ORDER BY COUNT(c.id) DESC) AS position FROM authors a LEFT JOIN posts p ON p.author_id = a.id LEFT JOIN comments c ON c.post_id = p.id GROUP BY a.id, a.name ORDER BY position, a.name;
Result:
| name | comments_received | position |
| Asha | 5 | 1 |
| Ben | 1 | 2 |
| Chen | 0 | 3 |
Query 6: Posts without Comments
sql
SELECT p.title FROM posts p WHERE NOT EXISTS (SELECT 1 FROM comments c WHERE c.post_id = p.id) ORDER BY p.id;
Result:
| title |
| Backup basics |
| Python and databases |
Query 7: Add and Remove a Tag
sql
UPDATE posts SET tags = JSON_ARRAY_APPEND(tags, '$', 'tutorial') WHERE id = 1;
UPDATE posts SET tags = JSON_REMOVE(tags, '$[1]') WHERE id = 1;
SELECT id, tags FROM posts WHERE id = 1;
Result:
| id | tags |
| 1 | ["mysql", "tutorial"] |
$[1] is the second element (arrays count from 0), so beginner was removed and tutorial was added at the end.
Query 8: Cascading Delete
sql
DELETE FROM posts WHERE id = 2;
SELECT (SELECT COUNT(*) FROM posts) AS posts_left, (SELECT COUNT(*) FROM comments) AS comments_left;
Result:
| posts_left | comments_left |
| 4 | 3 |
Deleting post 2 also removed its three comments, as designed.
Extra Challenges
- Add a
views column and an index to find the 3 most viewed posts of each author.
- Find authors who have never been commented on.
- Create a view
post_summary with title, author, tags and comment count.
- Add a
FULLTEXT search on comments and combine it with posts using UNION ALL.
Conclusion
You designed a schema with keys and checks, stored tags in JSON, indexed them with a multi-valued index, searched with full text and wrote ranking, anti-join and cascade queries.
Quick Summary: schema design, JSON tags, multi-valued index, FULLTEXT, JSON_TABLE, cascade.