DEV Community

CharlesTechy
CharlesTechy

Posted on

4

Designing SQL Database for a blog application

With this tutorial we would have an understanding on how create a database schema for a blog application. It defines several tables and their relationships to store information about authors, blog posts, comments, replies, and post likes.

Here's a breakdown of the script:

  1. Creating the blog database:

CREATE DATABASE IF NOT EXISTS Blog;

  1. Creating the Author table:

CREATE TABLE IF NOT EXISTS Author (
author_id INT AUTO_INCREMENT,
full_name VARCHAR(255) NOT NULL,
about TEXT,
is_admin TINYINT(1),
status TINYINT(1),
date_registered DATETIME,
password VARCHAR(255),
PRIMARY KEY (author_id)
);

This table stores information about authors, including their ID, full name, about section, admin status, status, registration date, and password.

  1. Creating the Posts table:

CREATE TABLE IF NOT EXISTS Posts (
post_id INT AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
description TEXT,
is_published TINYINT(1),
date_published DATETIME,
date_edited DATETIME,
author_id INT NOT NULL,
thumbnail VARCHAR(255) DEFAULT('thumbnail.png'),
PRIMARY KEY (post_id),
FOREIGN KEY (author_id) REFERENCES Author(author_id)
);

This table stores information about blog posts, including their ID, title, description, published status, publication date, last edited date, author ID (foreign key referencing the Author table), and thumbnail image.

  1. Creating the Comments table:

CREATE TABLE IF NOT EXISTS Comments (
c_id INT AUTO_INCREMENT,
post_id INT NOT NULL,
comment TEXT,
is_published TINYINT(1),
date_published DATETIME,
date_edited DATETIME,
author_id INT NOT NULL,
PRIMARY KEY (c_id),
FOREIGN KEY (post_id) REFERENCES Posts(post_id)
);

This table stores information about comments on blog posts, including their ID, post ID (foreign key referencing the Posts table), comment text, published status, publication date, last edited date, and author ID (foreign key referencing the Author table).

  1. Creating the Replies table:

CREATE TABLE IF NOT EXISTS Replies (
r_id INT AUTO_INCREMENT,
c_id INT NOT NULL,
reply TEXT,
is_published TINYINT(1),
date_published DATETIME,
date_edited DATETIME,
author_id INT NOT NULL,
PRIMARY KEY (r_id),
FOREIGN KEY (c_id) REFERENCES Comments(c_id)
);

This table stores information about replies to comments, including their ID, comment ID (foreign key referencing the Comments table), reply text, published status, publication date, last edited date, and author ID (foreign key referencing the Author table).

  1. Creating the PostLikes table:

CREATE TABLE IF NOT EXISTS PostLikes (
like_id INT AUTO_INCREMENT,
post_id INT NOT NULL,
likes TINYINT(1),
is_published TINYINT(1) DEFAULT('1'),
date_published DATETIME,
date_edited DATETIME,
author_id INT NOT NULL,
PRIMARY KEY (like_id),
FOREIGN KEY (post_id) REFERENCES Posts(post_id)
);

This table stores information about post likes, including their ID, post ID (foreign key referencing the Posts table), number of likes, published status, publication date, last edited date, and author ID (foreign key referencing the Author table).

  1. Performing queries on the tables: The script includes sample queries to retrieve data from the tables. These queries include selecting all posts, retrieving a single post by its ID,

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay