多对多关系还是非多对多关系?
我在尝试定义 3 个表之间的关系类型时遇到了一些麻烦。 它们是产品、供应商,第三个是数量,其中我有产品和供应商的组合以及存储该组合的产品数量的字段。
我可以从一个或多个供应商那里获得一种产品,这就是 QUANTITY 表存在的原因。例如:我可以从供应商 X 和供应商 Y 购买相同的笔记本 A。
表 QUANTITY 具有字段 prod_id、supp_id 和 qty。产品和供应商都有一个 ID 字段。我不明白,所以我向比我更聪明的人寻求一点帮助,也就是说你们所有人。 :D
非常感谢。
[编辑]
非常抱歉,我应该从一开始就说得更清楚。这是关于建模的。 :D
I'm in a bit of trouble trying to define the type of a relationship between 3 tables.
They are PRODUCTS, SUPPLIERS, and the third, QUANTITY, where I have a combination of product and supplier and a field storing the quantity of products of that combination.
I can have one product from one or more suppliers, that's why the table QUANTITY exists. Eg.: I can buy the same notebook A from the supplier X and supplier Y.
The table QUANTITY have the fields prod_id, supp_id and qty. Both PRODUCTS and SUPPLIERS have an ID field. I can't figure it out, so I'm asking for a little help from smarter guys than me, meaning you all. :D
Thanks a lot.
[EDIT]
I'm very sorry, I should've been more clear from the start. This is about the modelling. :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然您询问有关人际关系的问题,这里有一些可能会有所帮助的信息。多对多表存在于设计(概念)级别,但通过构建两个一对多关系在数据库中构建。
尽管您不必这样做,但您可能应该声明Quantity.supp_id是来自供应商的外键,建立从供应商到数量的一对多关系;并声明 products.prod_id 是产品的外键,建立产品和数量的一对多关系。这样做可能有助于数据库优化查询。它还使您能够设置级联删除,以便从供应商表中删除供应商也会自动删除数量表中该供应商的所有记录(如果您希望发生这种情况)。
Since you ask about relationships, here's a little more information which may help. A many-to-many table exists at the level of design (concept), but is built in the db by building two one-to-many relationships.
Although you don't have to, you probably should declare that quantity.supp_id is a foreign key from suppliers, establishing a one-to-many relationship from suppliers to quantity; and declare that products.prod_id is a foreign key from products, establishing a one-to-many relationship from products to quantity. Doing so may help the db to optimize queries. It will also enable you to set up cascading deletes so that deleting a supplier from the suppliers table will automatically also delete all of that supplier's records in the quantity table (if you want that to happen).
您帖子中描述的情况似乎可以回答您提出的任何问题。也就是说,您在问题中陈述了问题的正确解决方案。
表 PRODUCTS 将具有字段 id(我更喜欢将其称为 prop_id,但这只是我的说法)。供应商将有 id(或者,在我的世界中,supp_id)。表 QUANTITY 将包含(prop_id、supp_id 和数量)。
理想情况下,产品中的 prop_id 将作为主键或唯一索引进行保护。与 SUPPLIERS 中的supp_id 相同。在 QUANTITY 中,您将在 prop_id 和supp_id 的组合上创建主键或唯一索引(因为每个组合只应出现一次,即使单独考虑的每个列可能有重复项)。
最后,我通常更喜欢在每个表上都有一个单列整数主键,所以(如果是我,其他人不同意)我会创建(prop_id,supp_id)唯一索引并在QUANTITY中创建一个新的主键列quantity_id桌子。
The situation described in your posting seems to answer whatever question you are asking. That is, you state a correct solution to your problem in the question.
Table PRODUCTS will have field id (I prefer to call it prop_id, but that's just me). SUPPLIERS will have id (or, in my world, supp_id). Table QUANTITY will have (prop_id, supp_id, and quantity).
Ideally, prop_id in PRODUCTS will be protected as primary key or a unique index. The same with supp_id in SUPPLIERS. In QUANTITY, you will create a primary key or unique index on the combination of prop_id and supp_id (since each combination should occur only once, even though each column considered alone might have duplicates).
Finally, I generally prefer to have a single-column integer primary key on every table, so (if it were me, others disagree) I would make (prop_id, supp_id) a unique index and create a new primary key column quantity_id in the QUANTITY table.
您似乎对具有附加属性的实体之间的关系感到困惑。
我同意这是相当不常见的,但它没有什么无效的,而且你似乎正在正确地建模它。
You appear to be puzzled by a relationship between entities having additional attributes.
I agree that it is fairly uncommon, but there's nothing invalid about it and you appear to be modeling it correctly.