search
HomeBackend DevelopmentPHP Tutorial聊天记录数据库表结构怎么设计

聊天记录数据库表结构怎么设计

Jun 06, 2016 pm 08:23 PM
database

聊天记录数据库表结构怎么设计

聊天记录数据库表结构怎么设计?

关于聊天记录数据库表结构设计

1、首先表结构设计针对单个用户,然后拓展到n个用用户记录的存储。

2、这里会用msql数据库给出数据库表脚本,但是实际生产环境应该是在APP端生成sqlite数据库文件,把sqlite文件上传到server端作为聊天记录存储。

有【联系人表】、【群组表】、【会话表】、【用户表】、【聊天记录表】

# 聊天记录表

CREATE TABLE `message` (
  `msg_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '消息id,自增长',
  `msg_type` int(11) DEFAULT NULL COMMENT '类型 1:文字;47:emoji;43:音频;436207665:红包;49:文件;48:位置;3:图片',
  `is_send` int(11) DEFAULT NULL COMMENT '是否是自己发送 0:不是;1:是',
  `create_time` datetime DEFAULT NULL COMMENT '消息发送时间',
  `content` text COMMENT '消息格式【发信人id:内容】',
  `talker` varchar(55) DEFAULT NULL COMMENT '聊天对象。群聊,则是群id(xxx@chatroom);一对一,聊天对象的唯一标识。',
  PRIMARY KEY (`msg_id`),
  KEY `index_chat_id` (`talker`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

注意⚠️ content:不是自己发送的消息,并且是群聊,才会带有发信人id标记。

msg_id在同一个账户切换了设备后产生的msg_id可能是重复的,所以在聊天记录存储在server端时需要注意到这一点。

 

#联系人表

CREATE TABLE `recontact` (
  `username` varchar(45) NOT NULL COMMENT '联系人唯一标识',
  `nickname` varchar(45) DEFAULT NULL COMMENT '联系人昵称',
  `remark` varchar(45) DEFAULT NULL COMMENT '备注名称',
  `avatar` varchar(200) DEFAULT NULL COMMENT '联系人头像地址',
  `is_friend` varchar(45) DEFAULT NULL COMMENT '是否是好友。1,3 好友 4;群里非好友',
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='联系人,非好友';

联系人表存储了好友以及非好友。

#群组表

CREATE TABLE `chat_room` (
  `chat_room_name` varchar(30) CHARACTER SET utf8mb4 NOT NULL,
  `member_list` text CHARACTER SET utf8mb4 COMMENT '群组成员id列表,分号分割。a53255001;nan1242;jiabailo002',
  `display_name_list` text CHARACTER SET utf8mb4 COMMENT '群成员昵称列表【中文顿号分割】海、二、老僧、刘伟、齐彬、毛、Echo、曹',
  `room_owner` varchar(45) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '群主id',
  `self_display_name` varchar(45) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '自己在群里的自定义群昵称',
  `chat_room_nick` varchar(45) CHARACTER SET utf8mb4 DEFAULT NULL COMMENT '群昵称,没有自定义群昵称则从display_name中截取20个字符作为群昵称。',
  PRIMARY KEY (`chat_room_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='群组';
chat_room_name为群id,格式为xxx@chatroom

#用户表

CREATE TABLE `users` (
  `id` varchar(45) NOT NULL COMMENT '用户id',
  `nickname` varchar(45) DEFAULT NULL,
  `email` varchar(45) DEFAULT NULL,
  `sign` varchar(200) DEFAULT NULL COMMENT '签名',
  `city` varchar(45) DEFAULT NULL,
  `provincial` varchar(45) DEFAULT NULL COMMENT '省份',
  `avatar_url` varchar(500) DEFAULT NULL COMMENT '头像',
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id_UNIQUE` (`id`),
  KEY `index_updated_at` (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户';

更多相关知识,请访问PHP中文网

Statement
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version