简体   繁体   English

规范化表6

[英]Normalizing a Table 6

I'm putting together a database that I need to normalize and I've run into an issue that I don't really know how to handle. 我正在组建一个我需要规范化的数据库,我遇到了一个我真不知道如何处理的问题。

I've put together a simplified example of my problem to illustrate it: 我已经把我的问题的简化示例放在一起来说明它:

Item ID___Mass___Procurement__Currency__________Amount
0__________2kg___inherited____null________________null
1_________13kg___bought_______US dollars_________47.20  
2__________5kg___bought_______British Pounds______3.10
3_________11kg___inherited____null________________null   
4__________9kg___bought_______US dollars__________1.32

(My apologies for the awkward table; new users aren't allowed to paste images) (我为尴尬的表道歉;新用户不允许粘贴图片)

In the table above I have a property (Amount) which is functionally dependent on the Item ID (I think), but which does not exist for every Item ID (since inherited items have no monetary cost). 在上面的表格中,我有一个属性(Amount),它在功能上依赖于Item ID(我认为),但是对于每个Item ID都不存在(因为继承的项目没有货币成本)。 I'm relatively new to databases, but I can't find a similar issue to this addressed in any beginner tutorials or literature. 我对数据库比较陌生,但在任何初学者教程或文献中都找不到类似的问题。 Any help would be appreciated. 任何帮助,将不胜感激。

I would just create two new tables ItemProcurement and Currencies . 我只想创建两个新表ItemProcurementCurrencies

If I'm not wrong, as per the data presented, the amount is part of the procurement of the item itself (when the item has not been inherited), for that reason I would group the Amount and CurrencyID fields in the new entity ItemProcurement . 如果我没有错,根据提供的数据,金额是项目本身采购的一部分(当项目未被继承时),因此我会将AmountCurrencyID字段分组到新实体ItemProcurement

As you can see, an inherited item wouldn't have an entry in the ItemProcurement table. 如您所见,继承的项目在ItemProcurement表中没有条目。

Concerning the main Item table, if you expect just two different values for the kind of procurement, then I would use a char(1) column (varying from B => bougth, I => inherited). 关于主Item表,如果您期望采购类型只有两个不同的值,那么我将使用char(1)列(从B => bougth, I =>继承)。

I would looks like this: 我看起来像这样:

在此输入图像描述

The data would then look like this: 然后数据看起来像这样:

TABLE Items
+-------+-------+--------------------+
|    ID |  Mass |  ProcurementMethod |
|-------+-------+--------------------+
|     0 |     2 |                  I | 
+-------+-------+--------------------+
|     1 |    13 |                  B | 
+-------+-------+--------------------+
|     2 |     5 |                  B | 
+-------+-------+--------------------+

TABLE ItemProcurement
+--------+-------------+------------+
| ItemID |  CurrencyID |     Amount |
|--------+-------------+------------+
|     1  |         840 |      47.20 | 
+--------+-------------+------------+
|     2  |         826 |       3.10 | 
+--------+-------------+------------+

TABLE Currencies
+------------+---------+-----------------+
| CurrencyID | ISOCode |     Description |
|------------+---------+-----------------+
|        840 |     USD |      US dollars | 
+------------+---------+-----------------+
|        826 |     GBP |  British Pounds | 
+------------+---------+-----------------+

Here is my off-the-cuff suggestion: 这是我的袖手旁观的建议:

UPDATE: Mass would be a Float/Decimal/Double depending upon your Db, Cost would be whatever the optimal type is for handling money (in SQL Server 2008, it is "Money" but these things vary). 更新:质量将是一个Float / Decimal / Double,具体取决于您的Db,成本将是处理资金的最佳类型(在SQL Server 2008中,它是“Money”但这些事情各不相同)。

ANOTHER UPDATE: The cost of an inherited item should be zero, not null (and in fact, there sometime IS an indirect cost, in the form of taxes, but I digress . . .). 另一个更新:继承项目的成本应为零,而不是空(事实上,有时候是间接成本,以税收的形式,但我离题......)。 Therefore, your Item Table should require a value for cost, even if that cost is zero. 因此,即使成本为零,您的项目表也应该需要成本值。 It should not be null. 它不应该为空。

Let me know if you have questions . 如果您有疑问,请告诉我。 . .

在此输入图像描述

Not only Amount , everything is dependent on ItemID , as this seems to be a candidate key. 不仅Amount ,一切都依赖于ItemID ,因为这似乎是一个候选键。

The dependence you have is that Currency and Amount are NULL (I guess this means Unknown/Invalid) when the Procurement is 'inherited' (or 0 cost as pointed by @XIVsolutions and as you mention "inherited items have no monetary cost" ) 您的依赖是CurrencyAmountNULL (我猜这意味着未知/无效),当Procurement'inherited' (或@XIVsolutions指出的0成本,并且当您提到“继承的项目没有货币成本”时

In other words, iems are divided into two types (of procurements) and items of one of the two types do not have all attributes. 换句话说,iems被分为两种类型(采购),而两种类型之一的项目没有所有属性。

This can be solved with a supertype/subtype split. 这可以通过超类型/子类型拆分来解决。 You have a supertype table ( Item ) and two subtype tables ( ItemBought and ItemInherited ), where each one of them has a 1::0..1 relationship with the supertype table. 你有一个超类型表( Item )和两个子类型表( ItemBoughtItemInherited ),其中每个表与超类型表都有1::0..1关系。 The attributes common to all items will be in the supertype table and every other attribute in the respecting subtype table: 所有项目共有的属性将在超类型表中,并且在尊重子类型表中的每个其他属性:

Item
----------------------------
ItemID    Mass   Procurement
0          2kg   inherited
1         13kg   bought
2          5kg   bought
3         11kg   inherited 
4          9kg   bought

ItemBought
---------------------------------
ItemID    Currency         Amount
1         US dollars       47.20  
2         British Pounds    3.10 
4         US dollars        1.32

ItemInherited
-------------
ItemID
0       
3    

If there is no attribute that only inherited items have, you even skip the ItemInherited table altogether. 如果没有只有继承项的属性,您甚至可以完全跳过ItemInherited表。

For other questions relating to this pattern, look up the tag: Class-Table-Inheritance. 有关此模式的其他问题,请查找标记:Class-Table-Inheritance。 While you're at it, look up Shared-Primary-Key as well. 在您使用它的同时,也可以查找Shared-Primary-Key。 For a more concpetual treatment, google on "ER Specialization". 要获得更加孜孜不倦的待遇,请关注“ER专业化”。

Why do you need to normalise it? 为什么需要将其标准化?

I can see some data integrity challenges, but no obvious structural problems. 我可以看到一些数据完整性挑战,但没有明显的结构问题。

The implicit dependency between "procurement" and the presence or not of the value/currency is tricky, but has nothing to do with the keys and so is not a big deal, practically. “采购”与价值/货币的存在与否之间的隐含依赖性是棘手的,但与密钥无关,因此实际上并不是什么大不了的事。

If we are to be purists (eg this is for homework purposes), then we are dealing with two types of item, inherited items and bought items. 如果我们要成为纯粹主义者(例如,这是为了家庭作业),那么我们正在处理两种类型的项目,继承项目和购买项目。 Since they are not the same type of thing, they should be modelled as two separate entities ie InheritedItem and BoughtItem, with only the columns they need. 由于它们不是同一类型的东西,它们应该被建模为两个独立的实体,即InheritedItem和BoughtItem,只有它们需要的列。

In order to get a combined view of all items (eg to get a total weight), you would use a view, or a UNION sql query. 为了获得所有项目的组合视图(例如,获得总权重),您将使用视图或UNION SQL查询。

If we are looking to object model in the database, then we can factor out the common supertype (Item), and model the subtypes (InheritedItem, BoughtItem) with foreign-keys to the supertype table (ypercube explanation below is very good), but this is very complicated and less future-proof than only modelling the subtypes. 如果我们在数据库中寻找对象模型,那么我们可以分解出常见的超类型(Item),并使用外键对子类型(InheritedItem,BoughtItem)建模超类型表(下面的ypercube说明非常好),但是与仅模拟子类型相比,这是非常复杂且不太具有前瞻性的。

This last point is the subject of much argument, but practically, in my experience, modelling concrete supertypes in the database leads to more pain later than leaving them abstract. 最后一点是很多争论的主题,但实际上,根据我的经验,在数据库中建模具体的超类型会导致更多的痛苦,而不是留下抽象。 Okay, that's probably waaay beyond what you wanted :). 好的,这可能超出你想要的范围:)。

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

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