简体   繁体   English

第二范式的ER模式

[英]E-R Schema in second normal form

I have a doubt on a table in my ER schema and the second normal form. 我对我的ER模式和第二种正常形式的表有疑问。 The table has an attribute that depends on another one and I don't know if this causes problems with the normalization. 该表的属性取决于另一个属性,我不知道这是否会导致规范化问题。

The table structure is like this: 表结构如下:

| contract_id | start_date | end_date |

The primary key is *contract_id*. 主键是* contract_id *。 The problem is that the value of *end_date* is exactly 1 year after the *start_date*. 问题是* end_date *的值正好是* start_date *之后的1年。

Rows example: 行示例:

| 1 | 2013-01-01 | 2014-01-01 |
| 2 | 2012-02-03 | 2013-02-03 |

Is this table in 2NF? 该表是2NF版本吗?

I don't see why it shouldn't be 2NF. 我不明白为什么它不应该是2NF。 For 2NF to hold, there should not be a partial dependency on any column in a compound/composite primary key (which is not the case here). 为了保留2NF,在复合/复合主键中的任何列上都不应部分依赖(这里不是这种情况)。

Your question is more likely a concern for 3NF. 您的问题很可能是3NF的问题。

It is not covered by 2NF or 3NF but this model violates Domain/Key normal form. 它不受2NF或3NF的保护,但是此模型违反了Domain / Key正常形式。 Simply remove one of attributes if interval is always is a year or add a check constraint. 如果间隔始终为一年,则只需删除一个属性,或添加检查约束。
eg CHECK (end_date - start_date = 365), 365 is financial year, calendar year is not a constant. 例如CHECK(end_date-start_date = 365),365是会计年度,日历年不是常数。

A calculated column from other columns in the same row violates 3-NF . 同一行中其他列的计算列违反3-NF Some would tell you to not store the calculated column in your database to comply with 3-NF and that is indeed valid for the current model. 有人会告诉您不要将计算所得的列存储在数据库中以符合3-NF的要求,这对于当前模型确实有效。 However, you have to assess whether this is valid for your business or not. 但是,您必须评估这是否对您的业务有效。 Suppose in 3 months time, the business rules change and the end_date is no longer equal to the start_date+1 year, what will you do? 假设在3个月的时间内,业务规则发生了变化,并且end_date不再等于start_date + 1年,那么您会怎么做?

It is arguably not 2NF (presumably start_date is unique), but it is not 3NF, because you could use the expression 可以说它不是2NF(大概start_date是唯一的),但不是3NF,因为您可以使用表达式

adddate(start_date, interval 1 year)

Or better create a view with end_date being this expression. 或者最好用end_date作为此表达式创建一个视图。

You also must ask yourself about data integrity: what happens to your database if through same mistake your end date is not one year after the start date? 您还必须问自己有关数据完整性的问题:如果由于同样的错误,结束日期晚于开始日期,则数据库会发生什么变化? If the end date is calculated, such a situation is not possible - you get data integrity built in. 如果计算了结束日期,则不可能出现这种情况-内置了数据完整性。

If end date is a separate column, every insert and update of these columns should be checked for correctness, which is more work and potentially more bugs. 如果结束日期是一个单独的列,则应检查这些列的每次插入和更新的正确性,这会增加工作量并可能会导致更多错误。

Lastly, each row will be smaller if end date is not stored, which will give a small performance gain, although the gain may be insignificant. 最后,如果没有存储结束日期,则每一行都将较小,这将带来较小的性能提升,尽管该收益可能微不足道。

that doesn't violate 2NF but it does 3NF. 不会违反2NF,但会违反3NF。 you don't need to store a coputational field in the database. 您无需在数据库中存储带算字段。 that being said you don't have all the information you need to compute the value in the database either. 也就是说,您也不具备计算数据库中值所需的所有信息。 I would drop the end_date as stated above, but i would add a contract_term field. 我将如上所述删除end_date ,但会添加一个contract_term字段。 it would be easy to do now that they are all 1 year, or 12 months, or however you chose to compute it, as opposed to in the future when they may change. 现在,它们都是1年或12个月,或者不管您选择计算它,而不是在将来可能发生变化的情况下,现在都很容易做到。 then you will be able to compute the expiration date faithfully even if the business needs change, plus you are not storing a computed field. 那么即使业务需求发生变化,您也将能够如实地计算到期日期,而且您无需存储计算字段。 also, since contract_term describes the entity you are not violating 2NF or 3NF. 同样,由于contract_term描述的是您未违反2NF或3NF的实体。

If start_date->end_date and end_date->start_date then it follows that (start_date,end_date) is not a candidate key because that composite would not be a minimal superkey. 如果start_date-> end_date和end_date-> start_date,则(start_date,end_date)不是候选键,因为该组合不是最小的超键。 Therefore 2NF is satisfied because no proper subset of any candidate key is a determinant. 因此满足2NF,因为任何候选密钥的正确子集都不是决定因素。

3NF means that for every dependency you require to be satisfied either the determinant is a superkey or the dependent attributes are part of a candidate key. 3NF意味着对于您需要满足的每个依赖关系,行列式是超键,或者依赖属性是候选键的一部分。 The relevant question therefore is whether start_date and end_date are both candidate keys. 因此,相关的问题是start_date和end_date是否都是候选键。 If and only if they are then 3NF is satisfied. 当且仅当它们满足时,才满足3NF。 If start_date and end_date are nonprime (not candidate keys) then your table satisfies 2NF but not 3NF. 如果start_date和end_date是非质数(不是候选键),则表满足2NF,但不满足3NF。

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

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