数据库架构,1 个表或 2 个表

发布于 2024-10-21 18:29:41 字数 1559 浏览 1 评论 0原文

我的应用程序将允许用户拥有联系人列表。这是我当前的架构:

CREATE TABLE IF NOT EXISTS `contact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  `create_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`person_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `contact_request` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  `create_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`person_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email_address` varchar(50) NOT NULL,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email_address` (`email_address`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB;

当用户尝试添加另一个用户作为联系人时,会在 contact_request 表中创建一条记录。如果接收请求的用户拒绝该请求,则 contact_request 记录将被删除。如果用户决定接受请求,则 contact_request 表中的数据将添加到 contact 表中,然后从 contact_request 表中删除。

我意识到我可以用另一种方式来做到这一点,我删除 contact_request 表并向联系人表添加另一个字段,例如:表示联系人是否刚刚被请求或是否已接受的请求的状态。

CREATE TABLE IF NOT EXISTS `contact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  `status` tinyint(1) not null,
  `create_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`person_id`)
) ENGINE=InnoDB;

我看到的好处是我可以少一张桌子。我目前没有发现此更改会导致问题。值得改变这个吗?这两种方法还有我可能不知道的其他优点吗?推荐哪个?

My application will allow users to have a contact list. This is my current schema:

CREATE TABLE IF NOT EXISTS `contact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  `create_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`person_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `contact_request` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  `create_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`person_id`)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email_address` varchar(50) NOT NULL,
  `username` varchar(32) NOT NULL,
  `password` varchar(32) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email_address` (`email_address`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB;

When a user tries to add another user as a contact, a record is created in the contact_request table. If the user receiving the request rejects the request, the contact_request record is deleted. If the user decides to accept the request, the data from the contact_request table is added to the contact table then deleted from the contact_request table.

I realized that I could do this in another way where I drop the contact_request table and add another field to the contact table e.g: status that signifies whether a contact was just requested or if it is an accepted request.

CREATE TABLE IF NOT EXISTS `contact` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `person_id` int(11) NOT NULL,
  `status` tinyint(1) not null,
  `create_time` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `user_id` (`user_id`,`person_id`)
) ENGINE=InnoDB;

The advantage I see is that I would have 1 less table. I currently do not see a problem occurring as a result of this change. Is it worth changing this? Are there any other advantages to either method that I might not be aware of. Which is recommended?

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

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

发布评论

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

评论(3

灼疼热情 2024-10-28 18:29:41

另一项优势可能是拥有此状态INTCHAR)、记录请求 (Q )、接受联系人 (C)、拒绝请求 (J)、拒绝并重新请求 (R)、列入黑名单 (B)以及可能的其他状态,以便您可以更轻松地应用更复杂的逻辑,例如“用户在被拒绝两次后无法再次请求联系人”等。

One other advantage might be to have this status (either as INT or as CHAR), record requests (Q), accepted contacts (C), rejected requests (J), rejected and re-requested (R), blacklisted (B) and possibly other statuses so you could more easily apply more complicated logics, like "a user cannot request a contact again when it has been rejected twice", etc.

薄暮涼年 2024-10-28 18:29:41

值得改变这一点的原因不止一个;正如你所说,它会让你少一张桌子。但更重要的是,它可以让您避免人们请求与他们已经添加的某人联系,而无需查询额外的表。

It is worth changing this for more than one reason; as you stated, it will allow you to have one less table. More importantly however, it will allow you to avoid people from requesting contact with someone they've already added without having to query an extra table.

爱的那么颓废 2024-10-28 18:29:41

从某种意义上说,将它们保留为两张桌子会更干净。您可以清除并保持队列表较小,而不必不断过滤掉非真实联系人。听起来您永远不需要在同一个表中查看联系人和请求,因此没有理由仅仅为了它而将它们混在一起。

另一方面,我能看到的唯一优点是,嗯,数据库中少了一个表?还有一个非常模糊的问题,即不能意外地在联系人表和请求表中同时存在联系人(计时错误或其他原因)。

It would be cleaner in a sense to keep them as two tables. You could purge and keep the queue table small while not having to keep filtering out the not-real-contacts. It sounds like you will never really need to view contacts and requests within the same table, so there is no reason to mash them together just for the sake of it.

On the other hand, the only plus that I can see is that you, umm, have one less table in the db? And a very vague one of not being able to accidentally have a contact exist both in the contact table proper and the request table at the same time (timing bug or something else).

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