了解外键约束的作用。我是否正确使用它们?
我需要帮助来理解设置约束时外键的适用性。我明白设置外键的作用是为了防止孤立数据,但我发现了一种将外键放在子项中的愿望,这似乎打破了一种模式。不确定我这样做是否正确,如果我的限制正确的话,希望得到一些建议。
这是我的设计:
(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:
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里存在三种情况(从
Product
表的角度来看):在这种情况下,外键必须位于引用主键
UnitType.UnitTypeID
的Product
表中。在这种情况下,外键必须位于
ProductImages
表,引用Product.ProductID
。在这种情况下,您将需要一个包含
ProductID
的连接表/CategoryID
对,列是分别引用Product.ProductID
和Category.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):In this case the foreign key must be in the
Product
table referencing the primary keyUnitType.UnitTypeID
.In this case the foreign key must be in the
ProductImages
table, referencingProduct.ProductID
.In this case you will need a connection table that contain
ProductID
/CategoryID
pairs, with columns being foreign keys referencingProduct.ProductID
andCategory.CategoryID
respectively.So, the design of
UnitType
(case 1.) andProductImage
(case 2.) tables is OK, butFeeType
should probably be case 1. andCategory
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 haveNULL
in theProduct.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.