MySQL Lab 2: A Blog Database with JSON Tags and Full-Text Search

MySQL 70 min min read Updated: Nov 27, 2026 Intermediate
MySQL Lab 2: A Blog Database with JSON Tags and Full-Text Search
Intermediate Topic 2 of 3

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:

authorspostscomments
356

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:

idtitleauthorcomments
2Indexes in plain wordsAsha3
1Getting started with MySQLAsha2
3JSON columns in practiceBen1
4Backup basicsBen0
5Python and databasesChen0

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:

idtitle
1Getting started with MySQL
4Backup basics
5Python 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:

tagposts
beginner3
mysql3
backup1
json1
performance1
python1

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:

idtitle
1Getting started with MySQL
4Backup basics
5Python and databases
sql SELECT id, title FROM posts WHERE MATCH(title, body) AGAINST ('+mysql -index*' IN BOOLEAN MODE) ORDER BY id;

Result:

idtitle
1Getting 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:

namecomments_receivedposition
Asha51
Ben12
Chen03

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:

idtags
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_leftcomments_left
43

Deleting post 2 also removed its three comments, as designed.

Extra Challenges

  1. Add a views column and an index to find the 3 most viewed posts of each author.
  2. Find authors who have never been commented on.
  3. Create a view post_summary with title, author, tags and comment count.
  4. 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators