Home > Database > Mysql Tutorial > body text

MySQL table design tutorial: Create a simple comment reply table

王林
Release: 2023-07-01 20:27:10
Original
2336 people have browsed it

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!