数据库派生值与外键相同?

时间:2012-12-19 04:29:03

标签: database foreign-keys derived

数据库派生值是否与外键相同?

基于connoly和begg书籍,第四版第352页派生的价值是 An attribute that represents a value that is derivable from the value of a related attribute or set of attributes, not necessarily in the same entity type.

如果我们有2个表,例如CUSTOMER和ORDER,

Customer
- Id_Cust
- Name
- Phone

ORDER
- Order_id<
- id_cust

我们可以说“ORDER.id_cust派生自Customer.id_cust”吗?

实际上,我对上述概念感到困惑。

1 个答案:

答案 0 :(得分:2)

不,外键和派生值是两回事。

product  quantity  price  subtotal
--
ACH123   5         $1.50  $7.50

在上面的简化表中,“小计”是派生值。它源于“数量”和“价格”。 (将它们相乘。)

数据库设计者通常不存储派生值而不使用CHECK()约束或触发器来确保派生值始终正确。在上表中,将数量更新为4将使派生值“小计”不正确。除非更新还具有“小计”的正确值,否则CHECK()约束可以防止更新为“数量”。当“数量”或“价格”发生变化时,触发器可以自动更新“小计”。

CHECK()约束通常是更好的选择。

相关问题