1:1或1:0关系的规范化 [英] Normalization of an 1:1 or 1:0 relationship

查看:282
本文介绍了1:1或1:0关系的规范化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用关系数据库,你想要3NF(你叫它3NF在英语吗?),然后你拉1:1关系一起成一个表。但是如果关系是1:0/1(/意思或),会发生什么?

when using relation databases and you want 3NF (do you call it 3NF in english?), then you pull 1:1 relationsships together into one table. But what happens if the rationship is 1:0/1 (/ meaning or)?

然后你保持分开,以避免表中的空格?

Then you keep them separated to avoid blank spaces in tables? Kepping them apart is valid 3NF in ths case?

推荐答案

根据你的问题和随后的评论@ paxdialbo的答案,我的理解是你想要一个解决方案来存储可选属性,其中有很多,同时避免NULL。完成此操作的两种方式,第6次正常表单(6NF)或实体属性值(EAV)模型。

Based on your question and subsequent comments on @paxdialbo's answer my understanding is you want a solution for storing optional attributes, of which there are many, while avoiding NULLs. Two ways of accomplishing this, 6th Normal Form (6NF) or an Entity Attribute Value (EAV) model.

这涉及创建一个特定于该属性的表:

This involves creating a table specific to the attribute:

create table attributeName (
    id
    value
)

id 是外键, value 捕获该属性(例如社会保险号)。对于给定键缺少记录指示不存在。

Where id is a foreign key and value captures that attribute (e.g. social security number). Absence of a record for a given key indicates non-existence.

现在你可以想象,第6个正常表单可能导致表扩散。 EAV模型通过对多个属性使用类似的模型来解决:

Now as you can imagine, 6th Normal Form can lead to table proliferation. An EAV model solves by using similar model for multiple attributes as such:

create table integerAttribute (
    name
    id
    value
)

/ code>列标识属性(例如SocialSecurity),虽然更复杂的实现是替代名称列, / code>存储在单独的元数据表中并通过外键引用。无论如何,这种方法意味着具有用于不同数据类型的其他表(即 datetimeAttribute varcharAttribute 等等)。

真正的问题是要处理的可选属性。如果相对较少,最简单的解决方案是在主表上添加可选的NULLable列。 6NF和EAV增加了显着的复杂性和性能问题。通常,当使用这些方法中的一个时,将整个实体串行化到主表上的CLOB中以简化公共读取(即通过主键),以避免多个LEFT连接检索完全水化的实体。

The real question to ponder is how many optional attributes you're dealing with. If relatively few, the easiest solution is actually adding optional NULLable columns on the main table. 6NF and EAV add significant complexity and performance concerns. Often what's done when using one of these approaches is serializing the overall entity into a CLOB on the main table to simplify the common read (i.e. by primary key), to avoid multiple LEFT joins to retrieve a fully hydrated entity.

这篇关于1:1或1:0关系的规范化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆