简体   繁体   中英

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

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.
  • The relationship between LIST and ITEM is many-to-many, which is modeled by a junction (aka. link) table in between them: LIST_ITEM.

I have used identifying relationship between USER and LIST in the diagram above, producing more "natural" keys in the "downstream" tables which:

  • Reducing the need for JOINs (you already know which USER a given LIST_ITEM belongs to, without the need to JOIN with LIST).
  • But makes downstream keys "fatter".

The design using non-identifying relationship between USER and LIST (producing "slimmer" keys) would look like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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