简体   繁体   English

将一个外键连接到多个表(主键)

[英]Connecting one foreign key to multiple tables (primary keys)

I am developing an application for making quotations.我正在开发一个用于报价的应用程序。 First you make cost break down (or calculation) and upon that result you add item to quotation.首先,您进行成本分解(或计算),然后根据该结果将项目添加到报价中。 The problem is that i have many product, so each category of a product will have its own cost break down form with different parameters to be filled in. If I will have only one table for cost breakdown, then it will be huge (a lot of fields in table).问题是我有很多产品,所以每个产品类别都有自己的成本分解表,需要填写不同的参数。如果我只有一张成本分解表,那么它会很大(很多表中的字段)。 I have a feeling that this is not the right approach.我有一种感觉,这不是正确的方法。 So I came up with diagram below:所以我想出了下图:

我的图

Is this solution even possible, or I must have "N" (if I have N-tables) different FK for each cost break down table?这个解决方案甚至可能吗,或者我必须为每个成本分解表有“N”个(如果我有N个表)不同的FK? Do you have any better solutions?你有更好的解决方案吗?

I have another question if my linking table "Quotation_QtnDetail" is necessary?如果我的链接表“Quotation_QtnDetail”是必要的,我还有一个问题?

It would be possible to store a reference to a particular value in one of these tables by having a CalculationType column indicating which table the record is in, along with a generic reference ID column (containing the ID of the relevant record).通过具有指示记录在哪个表中的CalculationType列以及通用引用 ID 列(包含相关记录的 ID),可以在这些表之一中存储对特定值的引用。 For example, if you were storing a CalcId of 123 and a CalculationType of 2, this would point to the record with ID 123 in the Calc2 table.例如,如果您存储的CalcId为 123, CalculationType为 2,这将指向Calc2表中 ID 为 123 的记录。

The downside to doing this is you're going to lose the ability to validate your data using FK constraints, and it will also make joins to your calculation tables a bit more complicated.这样做的缺点是您将失去使用 FK 约束验证数据的能力,并且还会使与计算表的连接变得更加复杂。

Regarding the Quotation_QtnDetail table, unless a QtnDetail record could ever be linked to multiple Quotation records, there is no need for this extra linking table.关于Quotation_QtnDetail表,除非一个QtnDetail记录可以链接到多个Quotation记录,否则不需要这个额外的链接表。 Instead, just link it directly by adding a QtnId column to the QtnDetail table.相反,只需通过将QtnId列添加到QtnDetail表来直接链接它。 Similarly, you may also be able to remove the Calc_QtnItm table if an item is only ever linked to a single calculation record.同样,如果某个项目只链接到单个计算记录,您也可以删除Calc_QtnItm表。

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

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