Normalization of an 1:1 or 1:0 relationship

后端 未结 3 1168
日久生厌
日久生厌 2021-01-06 18:10

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

3条回答
  •  梦毁少年i
    2021-01-06 18:26

    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.

    6th Normal Form

    This involves creating a table specific to the attribute:

    create table attributeName (
        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.

    Entity Attribute Value

    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
    )
    

    The name column identifies the attribute (e.g. 'SocialSecurity'), albeit a more sophisticated implementation is instead of a name column, name is stored in a separate meta data table and referenced via a foreign key. Regardless, this approach implies have other tables for different data types (i.e. datetimeAttribute, varcharAttribute, etc...).

    Conclusion

    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.

提交回复
热议问题