简体   繁体   English

数据库设计中是否存在数量不定的列表,每个列表中都有不定数量的项目?

[英]Database design for unknown number of lists with a variable amount of items on each of them?

I'd like to design something that would allow users to put lists together of, let's say, grocery items. 我想设计一些东西,使用户可以将杂货类物品的清单放在一起。 If each user can have multiple lists (ie not a set amount) and those lists all have a variable number of items on them (again, not set) while some items appear on several different lists , how do I create a database without being horribly redundant? 如果每个用户可以有多个列表(即不是固定数量),并且这些列表上都有可变数量的项目(再次未设置),而某些项目出现在几个不同的列表中 ,那么我如何创建数据库而又不至于感到恐惧多余的?

I'm completely new to this kind of problem, not having put together any complex database before, and have no idea where to start. 对于这种问题,我是全新的,以前没有组装过任何复杂的数据库,也不知道从哪里开始。 This is what I came up with as an example, but I doubt this is the right way of doing things: 这是我想出的一个例子,但是我怀疑这是正确的处理方式:

在此处输入图片说明

Any help or ideas would be much appreciated! 任何帮助或想法将不胜感激!

You can create join table LIST_LINK_ITEM between three tables 您可以在三个表之间创建join table LIST_LINK_ITEM

Her primary key is : composition of three primary keys 她的主键是:三个主键的组成

Looks like you need something similar to this: 看起来您需要类似以下内容:

在此处输入图片说明

A list is private to user, but an item can be shared among multiple lists: 列表是用户专用的,但一个项目可以在多个列表中共享:

  • The relationship between USER and LIST is one-to-many which is modeled through a simple foreign key. USER和LIST之间的关系是一对多的,它是通过一个简单的外键建模的。
  • The relationship between LIST and ITEM is many-to-many, which is modeled by a junction (aka. link) table in between them: LIST_ITEM. LIST与ITEM之间的关系是多对多的,这是通过LIST_ITEM之间的联结表(又称为链接)来建模的。

I have used identifying relationship between USER and LIST in the diagram above, producing more "natural" keys in the "downstream" tables which: 我在上图中使用了USER和LIST之间的标识关系,在“下游”表中产生了更多“自然”键,这些键:

  • Reducing the need for JOINs (you already know which USER a given LIST_ITEM belongs to, without the need to JOIN with LIST). 减少对JOIN的需要(您已经知道给定LIST_ITEM属于哪个用户,而无需与LIST进行JOIN)。
  • But makes downstream keys "fatter". 但是会使下游键“更麻烦”。

The design using non-identifying relationship between USER and LIST (producing "slimmer" keys) would look like this: 使用USER和LIST之间的非标识关系(产生“ slimmer”键)的设计如下所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM