简体   繁体   English

可以在逻辑 ER 图中表示多对一关系吗?

[英]Can a many to one relationship be represented in a logical ER diagram?

I have a particular problem from my assignment which goes like this:我的作业有一个特殊问题,如下所示:

"Each product making up a set is supplied by a single supplier and is given a unique ID,. Products are always sold as part of a set, never on their own." “组成一套的每件产品都由一个供应商提供,并被赋予一个唯一的 ID。产品总是作为一套的一部分出售,而不是单独出售。”

So based on this is assumed Many Products creates One Package(aka set), but i don't know if i'm right, if so how can I visually show a Many to One relationship as an ER diagram.因此,基于此假设许多产品创建一个包(又名集),但我不知道我是否正确,如果是的话,我怎样才能将多对一关系直观地显示为 ER 图。

I have constructed my own Conceptual and Logical ER diagram, I just need to know if i'm right or wrong so that i can continue with the rest.我已经构建了自己的概念和逻辑 ER 图,我只需要知道我是对还是错,以便我可以继续使用 rest。

在此处输入图像描述

According to your description your schema will have one to many relation ie your single package comprises many products.根据您的描述,您的架构将具有一对多的关系,即您的单个 package 包含许多产品。

You can also find out your ERD diagram您还可以找到您的 ERD 图在此处输入图像描述

Here's a breakdown of the assignment and what I get from it:这是任务的细分以及我从中得到的:

Each product making up a set is supplied by a single supplier and is given a unique ID,.组成一套的每个产品都由一个供应商提供,并被赋予一个唯一的 ID。 Products are always sold as part of a set, never on their own.产品总是作为套装的一部分出售,而不是单独出售。

From this I get that we have these entities:从这里我知道我们有这些实体:

  1. Product产品
  2. Supplier供应商
  3. Package (Set) Package (套)

You should know that each Entity needs its own primary key.您应该知道每个实体都需要自己的主键。 Pros will either call this id, or product_id.专业人士将调用此 id 或 product_id。 There are ORM's that tend to work best out of the box, if you name the pk for each table 'id', especially when it is a simple sequence number.如果您将每个表的 pk 命名为“id”,那么有些 ORM 往往开箱即用,尤其是当它是一个简单的序列号时。

It's also better not to do what you are doing with attribute names.最好不要对属性名称做你正在做的事情。 In sql people stick with either all uppercase or all lowercase naming rather than camelcase.在 sql 中,人们坚持使用全部大写或全部小写命名而不是驼峰式命名。 Also I'd suggest that you don't name the price attribute pPrice just because it's in the Package table.另外我建议您不要仅仅因为它在 Package 表中而命名价格属性 pPrice。 Just name it price, because it can be referred to as Package.price if you need to tell it apart from some other table that also contains a price column.只需将其命名为价格,因为如果您需要将其与其他包含价格列的表区分开来,则可以将其称为 Package.price。

The important thing to understand is that the relationship between Package and Product is Many to Many重要的是要了解 Package 和 Product 之间的关系是多对多

  1. One Product can be part of Many Packages.一个产品可以是许多包的一部分。
  2. One Package can contain Many Products一个 Package 可以包含许多产品

In order to create entities for a Many to Many relationship, you need a table that sits between the 2 tables and will have foreign keys to both tables in it.为了为多对多关系创建实体,您需要一个位于两个表之间的表,并且其中的两个表都有外键。 Typically people will pick whatever they consider the dominant side -- I would probably use Package, and name the table "PackageProduct" to reinforce the idea that this table lets me package products together and sell or distribute them.通常,人们会选择他们认为占主导地位的任何一方——我可能会使用 Package,并将表命名为“PackageProduct”,以强化这样的想法,即该表让我可以将 package 产品一起销售或分销。

PackageProduct
--------------
id (pk)
package_id (foreign key to Package table)
product_id (foreign key to Product table)

You also need a supplier table, but you were informed that the relationship between Package and supplier is that a Package can have one and only one Supplier.您还需要一个供应商表,但您被告知 Package 和供应商之间的关系是 Package 可以有一个且只有一个供应商。

This is code for: create a one to many relationship between Supplier and Package.这是用于:在供应商和 Package 之间创建一对多关系的代码。 In doing this, Package will have a foreign key in it that stores the Supplier.id (or supplier_id)在这样做时,Package 将有一个外键存储供应商.id(或供应商_id)

So to conclude you should have these entities (tables):所以总而言之,你应该有这些实体(表):

  1. Package Package
  2. Product产品
  3. Supplier供应商
  4. PackageProduct包装产品

ERD ERD

Here's an ERD rendered with "Relational" format which I find a bit more descriptive, as the many sides of the connections use the crowsfoot, so it's very obvious.这是一个以“关系”格式呈现的 ERD,我发现它更具描述性,因为连接的许多方面都使用 crowsfoot,所以它非常明显。

ERD

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

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