用于聊天的数据库架构?

发布于 2024-09-06 18:16:02 字数 80 浏览 2 评论 0原文

我需要将聊天对话存储在数据库模式中。我使用该数据库的方式是在网站上发布聊天内容。每次聊天不会超过 20 条左右的回复。有人可以为此建议一个架构吗?

I need to store chat conversations in a database schema. The way I would use this database is I would post chats on a website. Each chat would not be more than about 20 responses. Can someone please suggest a schema for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-09-13 18:16:02

下面开始使用 MySQL Workbench

Image

和创建脚本

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `chats` DEFAULT CHARACTER SET utf8 COLLATE default collation ;

-- -----------------------------------------------------
-- Table `chats`.`chat`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `chats`.`chat` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `chats`.`chat_user`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `chats`.`chat_user` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `handle` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `chats`.`chat_line`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `chats`.`chat_line` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `chat_id` INT UNSIGNED NOT NULL ,
  `user_id` INT UNSIGNED NOT NULL ,
  `line_text` TEXT NOT NULL ,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_chat_line_chat` (`chat_id` ASC) ,
  INDEX `fk_chat_line_chat_user1` (`user_id` ASC) ,
  CONSTRAINT `fk_chat_line_chat`
    FOREIGN KEY (`chat_id` )
    REFERENCES `chats`.`chat` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_chat_line_chat_user1`
    FOREIGN KEY (`user_id` )
    REFERENCES `chats`.`chat_user` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

欢迎您从 我的保管箱

Here's a start using MySQL Workbench

Image

and the create script

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `chats` DEFAULT CHARACTER SET utf8 COLLATE default collation ;

-- -----------------------------------------------------
-- Table `chats`.`chat`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `chats`.`chat` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `chats`.`chat_user`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `chats`.`chat_user` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `handle` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `chats`.`chat_line`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `chats`.`chat_line` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `chat_id` INT UNSIGNED NOT NULL ,
  `user_id` INT UNSIGNED NOT NULL ,
  `line_text` TEXT NOT NULL ,
  `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_chat_line_chat` (`chat_id` ASC) ,
  INDEX `fk_chat_line_chat_user1` (`user_id` ASC) ,
  CONSTRAINT `fk_chat_line_chat`
    FOREIGN KEY (`chat_id` )
    REFERENCES `chats`.`chat` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_chat_line_chat_user1`
    FOREIGN KEY (`user_id` )
    REFERENCES `chats`.`chat_user` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

And you are welcome to download the MWB file from my dropbox.

窝囊感情。 2024-09-13 18:16:02

对话 has_may 线路

线路属于_用户,有内容和内容时间

Conversation has_may Lines

Line belongs_to User, has content & time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文