Home Database Mysql Tutorial MySQL table design tutorial: Create a simple comment reply table

MySQL table design tutorial: Create a simple comment reply table

Jul 01, 2023 pm 08:27 PM
Reply to comment Simple mysql table design

MySQL table design tutorial: Create a simple comment reply table

Comment functionality is a very common requirement when developing web applications. In order to implement the comment function, we need to design a table structure suitable for storing comments and their replies. This tutorial will show you how to create a simple comment reply form.

Requirements analysis:
Our comment reply table needs to store the following information:

  1. Comment ID
  2. Comment content
  3. Comment time
  4. Comment user ID
  5. Reply ID (if reply, otherwise empty)
  6. Reply content (if reply, otherwise empty)
  7. Reply time (If it is a reply, otherwise it is empty)
  8. Reply user ID (If it is a reply, otherwise it is empty)

According to the above requirements, we can create a file named comments table.

Table design:
First, we need to create a database to store our table. You can use the following SQL statement to create a database named comments_db:

CREATE DATABASE comments_db;
Copy after login

Next, we can use the following SQL statement to create a database named comments Table:

USE comments_db;

CREATE TABLE comments (
    comment_id INT AUTO_INCREMENT PRIMARY KEY,
    comment_text VARCHAR(255) NOT NULL,
    comment_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    user_id INT NOT NULL,
    reply_id INT,
    reply_text VARCHAR(255),
    reply_time TIMESTAMP,
    reply_user_id INT,
    FOREIGN KEY (reply_id) REFERENCES comments (comment_id),
    FOREIGN KEY (reply_user_id) REFERENCES users (user_id)
);
Copy after login

Analysis table design:

  • comment_id is the comment ID and is set as the primary key that grows automatically.
  • comment_text is the content of the comment, and the length is set to 255 characters.
  • comment_time The time when comments are stored, the default is the current time.
  • user_id is the user ID of the comment and cannot be empty.
  • reply_id is the reply ID. If it is a reply, it is the comment ID of the reply; otherwise, it is NULL.
  • reply_text is the content of the reply. If it is a reply, it is the content of the reply; otherwise, it is NULL.
  • reply_time Stores the time of reply. If it is a reply, it is the time of reply; otherwise, it is NULL.
  • reply_user_id is the user ID of the reply. If it is a reply, it is the user ID of the reply; otherwise, it is NULL.
  • FOREIGN KEY Constraints are used to create reply_id and reply_user_id and comments tables and users table association.

Usage Example:
Now that we have created a table named comments, we can start using it. Here are some code examples to demonstrate how to manipulate this table:

  1. Insert a new comment:
INSERT INTO comments (comment_text, user_id) VALUES ('这是一条新评论', 1);
Copy after login
  1. Reply to a comment:
INSERT INTO comments (comment_text, user_id, reply_id, reply_text, reply_user_id) 
VALUES ('这是一条回复', 2, 1, '这是回复的内容', 1);
Copy after login
  1. Query all comments and their replies:
SELECT c.comment_id, c.comment_text, c.comment_time, u1.username AS comment_username,
       r.reply_id, r.reply_text, r.reply_time, u2.username AS reply_username
FROM comments AS c
LEFT JOIN comments AS r ON c.comment_id = r.reply_id
LEFT JOIN users AS u1 ON c.user_id = u1.user_id
LEFT JOIN users AS u2 ON r.reply_user_id = u2.user_id;
Copy after login

Summary:
In this tutorial, we learned how to create a simple comment reply form. By designing an appropriate table structure and using appropriate SQL statements, we can easily store and manage the data of comments and their replies. Hope this tutorial is helpful!

The above is the detailed content of MySQL table design tutorial: Create a simple comment reply table. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The easiest way to query the hard drive serial number The easiest way to query the hard drive serial number Feb 26, 2024 pm 02:24 PM

The easiest way to query the hard drive serial number

How to write a simple online reservation system through PHP How to write a simple online reservation system through PHP Sep 26, 2023 pm 09:55 PM

How to write a simple online reservation system through PHP

How to write a simple student performance report generator using Java? How to write a simple student performance report generator using Java? Nov 03, 2023 pm 02:57 PM

How to write a simple student performance report generator using Java?

Quick Start: Use Go language functions to implement a simple library management system Quick Start: Use Go language functions to implement a simple library management system Jul 30, 2023 am 09:18 AM

Quick Start: Use Go language functions to implement a simple library management system

How to use PHP to develop simple file management functions How to use PHP to develop simple file management functions Sep 20, 2023 pm 01:09 PM

How to use PHP to develop simple file management functions

How to write a simple music recommendation system in C++? How to write a simple music recommendation system in C++? Nov 03, 2023 pm 06:45 PM

How to write a simple music recommendation system in C++?

MySQL Table Design Guide: How to Create Order Table and Product Table MySQL Table Design Guide: How to Create Order Table and Product Table Jul 02, 2023 pm 12:25 PM

MySQL Table Design Guide: How to Create Order Table and Product Table

Write a simple calculator C/C++ program Write a simple calculator C/C++ program Sep 02, 2023 pm 10:49 PM

Write a simple calculator C/C++ program

See all articles