了解外键约束的作用。我是否正确使用它们?

发布于 2025-01-03 16:15:32 字数 618 浏览 0 评论 0原文

我需要帮助来理解设置约束时外键的适用性。我明白设置外键的作用是为了防止孤立数据,但我发现了一种将外键放在子项中的愿望,这似乎打破了一种模式。不确定我这样做是否正确,如果我的限制正确的话,希望得到一些建议。

这是我的设计:

在此处输入图像描述

(1) 我希望我所有的“产品”都有一个类型单位与数量相关。单位如“每”、“英尺”、“加仑”等,因此在数量和单位之间,您会得到如下内容:

数量单位 5 加仑

我不想允许一堆疯狂的单位,所以我设置了这个限制。这几乎是书本上的内容。

(2)我还相信并非所有产品都会有“图像”,所以我将外键放在“ProductImage”表中,这样我就不会有带有空行的列的“产品”,因为我也在尝试使设计“标准化”。

“FeeTypes”也存在同样的问题,因为并非所有“产品”都会收取费用。

我对打破将外键约束放在孩子而不是父母身上的模式感到内疚。我只是无法理解“FeeType”是父母。这种逻辑上的冲突就是我的疑问所在。

从设计的角度来看,我的设计正确吗? 我仍然正确限制数据吗? 除了防止孤立数据之外,还有其他“作用”吗?

提前致谢。

I need help in understanding the applicability of foreign keys when setting up constraints. I understand that the role of setting up foreign keys is to prevent orphaned data, but I have found a desire to put the foreign key in the child, which seems to break a pattern. Not sure if I am doing this right, and would like some advice if I have my constraints correctly.

Here is the design I have:

enter image description here

(1) I want all my "product"s to have a type of unit associate with the quantity. Units being like "Each", "Foot", "Gallon", etc, so between the quantity and the unit, you would have something like:

Quantity Unit
5 Gallons

I do not want to allow a bunch of crazy units, so I set this constraint up. This is pretty much by the book.

(2) I also believe that not all products will have an "Image", so I put the foreign key in the "ProductImage" table so I would not have "Product"s with a column with an empty row because I am also trying to "Normalize" the design.

The same issue with "FeeTypes" because not all "Product"s will have fees.

I feel guilt about breaking the pattern of putting the foreign key constraint in the child and not the parent. I just cannot wrap my head around "FeeType" being a parent. This conflict in logic is where I have the question.

Is my design correct, from a design perspective?
Am I still constraining the data properly?
Is there another "role" besides preventing orphaned data?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

游魂 2025-01-10 16:15:32

这里存在三种情况(从 Product 表的角度来看):

  1. 多对一关系,例如许多产品具有相同的单位类型 - 每个产品一种单位类型.
    在这种情况下,外键必须位于引用主键 UnitType.UnitTypeIDProduct 表中。
  2. 一对多关系,例如,一个产品可以有多个图像 - 一个图像只能属于一个产品
    在这种情况下,外键必须位于 ProductImages 表,引用 Product.ProductID
  3. 多对多关系,例如任何产品都可以有许多类别 - 任何类别都可能描述许多产品
    在这种情况下,您将需要一个包含ProductID的连接表/CategoryID 对,列是分别引用 Product.ProductIDCategory.CategoryID 的外键。

因此,UnitType(案例 1)和 ProductImage(案例 2)表的设计没问题,但 FeeType 可能应该是案例 1 . 和 Category 应该是情况 3。

顺便说一句,在外键列中包含 NULL 是完全可以的;它不会违反正常化规则。因此,例如,如果某些产品没有关联费用,您可以在 Product.FeeTypeID 列中使用 NULL。但您需要在查询中使用外部联接,以确保不会从结果中排除任何免费产品。

There are three cases here (from the Product table's point of view):

  1. Many-to-one relationship, e.g. many products having the same unit type - one unit type per product.
    In this case the foreign key must be in the Product table referencing the primary key UnitType.UnitTypeID.
  2. One-to-many relationship, e.g. one product can have multiple images - one image can belong to only one product
    In this case the foreign key must be in the ProductImages table, referencing Product.ProductID.
  3. Many-to-many relationship, e.g. any product can have many categories - any category might describe many products
    In this case you will need a connection table that contain ProductID/CategoryID pairs, with columns being foreign keys referencing Product.ProductID and Category.CategoryID respectively.

So, the design of UnitType (case 1.) and ProductImage (case 2.) tables is OK, but FeeType should probably be case 1. and Category should be case 3.

BTW, it would be perfectly OK to have NULL in a foreign key column; it would not break the rules of normalization. So, for example, if some products do not have fees associated, you can have NULL in the Product.FeeTypeID column. But you will need to use an outer join in your queries to ensure that no products with no fees will not be excluded from the results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文