简体   繁体   English

MySql“多对一”表设计

[英]MySql “Many to One” table design

I am new to DB design and I am having some trouble finding info on how to define a "Many to One" relationship. 我是DB设计的新手,我在查找如何定义“多对一”关系方面遇到了一些麻烦。 I can find all sorts of info on "One to Many" and "Many to Many" but nothing on "Many to One". 我可以找到关于“一对多”和“多对多”的各种信息,但没有关于“多对一”的信息。 My hangup is how to store the data. 我的挂断是如何存储数据。 What I have is one table called "Categories" then I have another table called "Inventory", each "Inventory" item can belong to multiple "Categories". 我有一个名为“类别”的表,然后我有另一个名为“库存”的表,每个“库存”项目可以属于多个“类别”。

How do I store multiple "Categories" in a single "Inventory" row? 如何在单个“库存”行中存储多个“类别”? Should I have a intermediate table that stores the "Categories" ID with the corresponding "Inventory" ID? 我是否应该有一个中间表来存储具有相应“库存”ID的“类别”ID? Or would adding something like a JSON string that has the "Categories" ID's in the "Inventory" row be the right way to do this? 或者添加像“库存”行中具有“类别”ID的JSON字符串这样的东西是正确的方法吗? Or is there a way to store an Array of "Categories" ID's in the "Inventory" row? 或者有没有办法在“库存”行中存储“类别”ID数组?

Thanks allot for the help! 谢谢你的帮助!

the correct term of Many to One is One to Many . Many to One的正确用语是One to Many simply create a table like this, 只需创建一个这样的表,

CREATE TABLE Categories
(
    CategoryID INT Primary KEY,
    CategoryName
);

CREATE TABLE InventoryList
(
    ProductID INT Primary KEY,
    CategoryID INT,
    ProductName VARCHAR(50),
    CONSTRAINT tb_fk FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
);

"Many to one" is just "One to many" looked at from the other end. “多对一”只是“一对多”从另一端看。

Just add a column inventory_id to the Categories table. 只需将一列inventory_id添加到Categories表中即可。

If not only each "Inventory" item can belong to multiple "Categories" but also each "Categories" item can belong to multiple "Inventory" items, than you have a many-to-many relation for which you need a intermediate table with category_id and inventory_id . 如果不仅每个“库存”项目都属于多个“类别”,而且每个“类别”项目都可以属于多个“库存”项目,那么您需要具有category_id的中间表的多对多关系和inventory_id

First I would suggest that you download mysql workbench - it gives you a nice visual db design mode so you can see how things hang together (creates foreign key relationships etc etc for you). 首先我建议您下载mysql工作台 - 它为您提供了一个很好的可视化数据库设计模式,这样您就可以看到事物如何挂在一起(为您创建外键关系等)。

In this instance this is actually a many-to-many relationship. 在这种情况下,这实际上是一个多对多的关系。 as such you will need a category table, an inventory table and a category_has_inventory table (or inventory_has_category depending on semantics) where you store the id of the inventory and category in each tuple - workbench even creates this table for you when using the many-to-many relationship tool. 因此,您将需要一个类别表,一个库存表和一个category_has_inventory表(或者inventory_has_category,具体取决于语义),您可以在每个元组中存储库存和类别的ID - 工作台甚至在使用many-to时为您创建此表 - 很多关系工具。

Pop back on here if you need further help. 如果您需要进一步的帮助,请回到这里。

IF a category can only contain one inventory item then you could create a one-to many relationship by adding inventory_id to the category table but that sounds wrong to me. 如果一个类别只能包含一个库存项目,那么您可以通过将inventory_id添加到类别表来创建一对多关系,但这对我来说听起来不对。

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

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