与复合键表的一对多关系

问题描述 投票:0回答:1

可能有更好的方法来做到这一点,但就是这样

我正在尝试记录运费

要素是

  • 运送区
  • 运输重量
  • 3 家承运商 UPS、USPS、FEDEX
  • 7 区域 2 至 8

表1 用户识别码 区域(PK) 重量(PK) *所以你不能创建重复的组合”

表2 用户识别码 UPS 价格 美国邮政价格 联邦快递价格

如何将 3 个条目添加到表 1 中的每个组合

希望我解释得很好

不知道如何添加与复合键表的一对多关系

或者是否有更好的方法来做到这一点

ms-access one-to-many composite-primary-key
1个回答
0
投票

看起来您仍在研究表标准化。在这种情况下,这里有一个例子:

-------------------------------------------
|     CarrierID      |    CarrierName     |
-------------------------------------------
|                  1 | UPS                |
-------------------------------------------
|                  2 | USPS               |
-------------------------------------------
|                  3 | FEDEX              |
-------------------------------------------

sql:

SELECT Carriers.CarrierName, Zones.ZoneName, ShippingCostFormulas.ShippingCostFormulaName, ShippingCosts.Weight, ShippingPrice([Weight],[ZoneID],[CarrierID]) AS ShippingPrice
FROM Carriers INNER JOIN (Zones INNER JOIN (ShippingCostFormulas INNER JOIN ShippingCosts ON ShippingCostFormulas.ShippingCostFormulaID = ShippingCosts.ShippingCostFormulaID) ON Zones.ZoneID = ShippingCosts.ZoneFK) ON Carriers.CarrierID = ShippingCosts.CarrierFK;

'results:
--------------------------------------------------------------------------------------------------------------------------------
|    CarrierName     |      ZoneName      |        ShippingCostFormulaName        |       Weight       |     ShippingPrice     |
--------------------------------------------------------------------------------------------------------------------------------
| UPS                | two                | FixedPrice                            |                0.2 |                 $0.10 |
--------------------------------------------------------------------------------------------------------------------------------
| USPS               | three              | FixedPrice                            |                0.3 |                 $0.17 |
--------------------------------------------------------------------------------------------------------------------------------
| FEDEX              | not not four       | FixedPrice                            |                0.3 |                 $0.19 |
--------------------------------------------------------------------------------------------------------------------------------
| UPS                | two                | ByWeightZoneCarrier                   |                0.2 |                 $0.10 |
--------------------------------------------------------------------------------------------------------------------------------
| USPS               | three              | ByWeightZoneCarrier                   |                0.3 |                 $0.17 |
--------------------------------------------------------------------------------------------------------------------------------
| FEDEX              | not not four       | ByWeightZoneCarrier                   |                0.3 |                 $0.19 |
--------------------------------------------------------------------------------------------------------------------------------

'formulas:
Public Function ShippingPrice(weight As Double, zone As Long, carrier As Long) As Currency

If carrier = 1 Then
ShippingPrice = weight * 0.5
End If
If carrier = 2 Then
ShippingPrice = weight * 0.4 + 0.05
End If
If carrier = 3 Then
Dim price As Currency
price = weight * 0.3 + 0.1
If zone = 1 Or zone = 5 Then
price = price + 0.2
End If
ShippingPrice = price
End If
End Function

表需要保存旧数据并能够处理新数据。 我们无法完全控制 Carrier、Zones 和 ShippingPriceFormulas,因此我们需要类型表来证明数据库在 ScroogeMcDuck Shipping 使用自己的区域并将运费加倍时的情况。不需要复合键。我将声明问题的区域权重部分过于模糊,并猜测您应该考虑数据输入表单和数据验证。使用像 ShippingPrice 这样的公共函数替换直接计算字段可以充分利用 VBA 的全部功能,并且只需稍微增加一点复杂性即可发挥更多功能。

示例表结构已经需要改进,因为重量应该在包裹表中,而 ShippingDate 很重要并且不是直接建模的。相反,ShippingDate 隐藏在随着时间的推移制定新的 ShippingPrice 规则的后面。

您似乎想比较托运人之间的费率。通常,您使用更强大的表单和报告来与数据交互,但查询足以作为示例:抓取并重新排列您需要显示或计算的数据,然后也显示计算结果,

*忽略fixedPriceFunction 我把它丢在了剪辑室的地板上,

© www.soinside.com 2019 - 2024. All rights reserved.