Home Backend Development PHP Tutorial Basic function database design of private message messages

Basic function database design of private message messages

Dec 21, 2017 pm 01:33 PM
database information Private letter

This article analyzes the implementation of the basic functions of private message messages through the database level.

Project requirements: Private message function, after sending a private message to the other party, the list of people who sent or received the message will be displayed on my private message list page. Each record in the list will only display the latest message of the conversation. . Click any item in the list to enter the message conversation details page, where the details of the conversation are displayed in reverse order. At the same time, you can delete conversations on these two pages. The private message list page deletes all conversations with the other party, and the private message details page deletes a certain conversation. The conversation records are deleted unilaterally without affecting the other party's viewing.

Software environment: mysql

Having said so much, in fact, there are only a few important points in summary. First, each record in the private message list only displays the last record, and second, unilateral deletion of the conversation Record without affecting the other party's viewing. First go to the data table, and then explain it one by one.

CREATE TABLE `private_message` (
  `id` bigint(20) NOT NULL auto_increment COMMENT '主键Id',
  `user_id` bigint(20) NOT NULL COMMENT '发送者Id',
  `friend_id` bigint(20) NOT NULL COMMENT '接受者Id',  
  `sender_id` bigint(20) NOT NULL COMMENT '发送者id',  
  `receiver_id` bigint(20) NOT NULL COMMENT '接受者Id',  
  `message_type` tinyint(4) NOT NULL COMMENT '消息类型,1:普通消息 2:系统消息',  
  `message_content` varchar(500) NOT NULL COMMENT '消息内容',  
  `send_time` datetime NOT NULL COMMENT '消息发送时间',  
  `status` tinyint(4) NOT NULL default '1' COMMENT '消息状态 1:未读 2:已读 3:删除',  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;123456789101112
Copy after login

Create a private_message table, field description:

id:主键,自增长 
user_id: 发送者id,非真实发送者id 
friend_id: 接受者id,非真实接受者id 
sender_id:发送者id,真实的发送者id 
receiver_id:接受者id,真实的接受者id 
message_type:消息类型,1:普通消息 2:系统消息,区分消息列表,可以发送不同类型的消息内容 
message_content:消息内容 
send_time:消息发送时间 
status:消息状态 1:未读 2:已读 3:删除,标记不同消息状态,可以实现统计未读消息数,逻辑删除用户恢复等
Copy after login

Everyone should be depressed after seeing this. How to get two sender ids and receiver ids?

Because we consider that unilateral deletion of records will not affect the other party's viewing function, we need to insert two pieces of data with the same content when sending a private message, but we have to do some tricks on user_id and friend_id. , when inserting data twice, the user_id and friend_id of the second inserted data are swapped with the first inserted data. That is:

INSERT INTO `private_message` VALUES ('1', '121', '127', '121', '127', '1', 'hello word', '2015-09-09 10:25:43', '2');INSERT INTO `private_message` VALUES ('2', '127', '121', '121', '127', '1', 'hello word', '2015-09-09 10:26:41', '1');INSERT INTO `private_message` VALUES ('3', '127', '121', '127', '121', '1', '你是程序猿吗?', '2015-09-11 10:30:16', '2');INSERT INTO `private_message` VALUES ('4', '121', '127', '127', '121', '1', '你是程序猿吗?', '2015-09-11 10:30:59', '2');1234
Copy after login

In this way, our needs can be met. The first and fourth records are for 121 users to see, and the second and third records are for 127 to see. When 121 deletes the first or fourth record, it will certainly not affect 127's viewing of the second record. The first and third records! ! !

Okay, now we can handle other functional requirements.
1. My private message list

SELECT p.id, COUNT(p.id) AS message_count,p.user_id,p.friend_id,p.sender_id,p.receiver_id,p.send_time,p.message_content, u.`name` AS receiver_name,u.img_url AS receiver_image FROM (SELECT * FROM private_message ORDER BY id DESC) p INNER JOIN user u on u.id=friend_id WHERE p.user_id=121 and p.`status` !=3 GROUP BY p.friend_id ORDER BY p.id DESC limit 0,101
Copy after login

2. My private message list details

SELECT p.id,p.message_content,p.sender_id,p.receiver_id,p.send_time,u.`name` AS sender_name,u.img_url AS sender_image,uu.`name` AS receiver_name FROM private_message p INNER JOIN user u on u.id=p.sender_id INNER JOIN user uu on uu.id=p.friend_id WHERE p.user_id=121 and p.friend_id=127 and p.`status` !=3 ORDER BY p.id DESC limit 0,101
Copy after login

3. Delete the entire conversation on my private message list page

UPDATE private_message SETstatus=3 WHERE user_id=121 AND friend_id=1271
Copy after login

4. Me Private message list details to delete a single conversation

UPDATE private_message SET status=3 WHERE id=11
Copy after login

5. Get the number of unread messages from the user

SELECT COUNT(*) FROM private_message WHERE user_id=121 AND receiver_id=127 AND status=11
Copy after login

Of course, you can also update unread messages as read and remove deleted users from the recycle bin Recovery, sending system messages, etc. can all be achieved. This is, some students will definitely say, the data redundancy in this table design, each record is inserted twice, if there is a lot of content or when sending system messages, the table data is too large. Of course, this is only for small private message functions, it is definitely It is different from large-scale social networking websites, but we can also split the content and create a new content table. Here, the ID can be associated to reduce data redundancy. Also, this design does not involve high concurrent access! When it comes to high concurrency, it requires more complex designs and methods to solve it!


Related reading:

php chat one-to-one chat function source code

How to make the use of database indexes more efficient?

General steps and examples of designing a database

The above is the entire content of this article. If you have any questions, please feel free to contact us Leave a message in the comment area!

The above is the detailed content of Basic function database design of private message messages. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

How to completely delete TikTok chat history How to completely delete TikTok chat history May 07, 2024 am 11:14 AM

1. Open the Douyin app, click [Message] at the bottom of the interface, and click the chat conversation entry that needs to be deleted. 2. Long press any chat record, click [Multiple Select], and check the chat records you want to delete. 3. Click the [Delete] button in the lower right corner and select [Confirm deletion] in the pop-up window to permanently delete these records.

How to recover deleted private message chat history on Douyin How to recover deleted private message chat history on Douyin May 06, 2024 pm 01:47 PM

1. When users delete Douyin private message chat records, usually these records cannot be recovered because it is a permanent deletion. 2. However, users can contact Douyin official and try to communicate to restore the chat history. 3. Open the Douyin app, click on the three horizontal bars in the upper right corner of the [Me] interface, select [Settings], slide down to the bottom, and click [About Douyin]. 4. Find and call the [Customer Service Hotline], or contact Douyin officials through the official email to learn about the possibility of restoring chat records.

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to turn off Kuaishou's private message reminder? How to close pop-up messages in private messages? How to turn off Kuaishou's private message reminder? How to close pop-up messages in private messages? Mar 27, 2024 pm 09:21 PM

As a short video platform, Kuaishou allows users to share their lives anytime and anywhere. Frequent private message reminders may interrupt our daily lives. So, how to turn off Kuaishou’s private message reminder? 1. How to turn off Kuaishou’s private message reminder? To turn off Kuaishou’s private message reminder, you need to enter Kuaishou’s settings menu. On the Kuaishou homepage, click the "My" button in the lower right corner, find the "Settings" option and click to enter. After entering the settings menu, browse to find the "Notification Settings" or "Message Settings" option. After clicking to enter, you will see the "Private Message Reminder" option. Click to enter private message reminders, and then turn the switch off to stop receiving private message reminders. 2. How to close pop-up messages in Kuaishou private messages? In addition to turning off private message reminders, if you also want to be able to turn off private message reminders,

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Where can I set the ringtone for TikTok private messages? How to solve the problem when there is no sound reminder when sending private messages? Where can I set the ringtone for TikTok private messages? How to solve the problem when there is no sound reminder when sending private messages? Apr 01, 2024 am 11:51 AM

As one of the most popular short video platforms in the world, Douyin has a large user base and rich functions. The private message function is an important way for users to interact with each other, and private message ringtones are an important way to remind users of new private messages. So, where can I set the ringtone for TikTok private messages? This article will introduce in detail how to set the ringtone for Douyin private messages and how to solve the problem of no sound reminder for incoming private messages. 1. Where can I set the ringtone for Douyin private messages? 1. Open the TikTok app: First, open your TikTok app. 2. Enter the private message interface: At the bottom of the Douyin homepage, click the "Message" icon to enter the private message interface. 3. Open settings: In the upper right corner of the private message interface, click the settings icon (usually a gear shape). 4. Select private message settings: In the settings menu, find and select

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

See all articles