Which normal form does this table violate?

前端 未结 6 753
北荒
北荒 2021-01-06 06:13

Consider this table:

   +-------+-------+-------+-------+  
   | name  |hobby1 |hobby2 |hobby3 |  
   +-------+-------+-------+-------+   
   | kris  | ball           


        
6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-06 06:58

    This clearly "looks" like a design error.

    It's not not a design error when this data is simply stored and retrieved. You need only 3 of the hobbies and you don't intend to use this data in any other way than retrieve.

    Let's consider this relationship:

    • Hobby1 is the main hobby at some point in a person's life (before 18 years of age for example)
    • Hobby2 is the hobby at another point (19-30)
    • Hobby3 is her hobby at a another one.

    Then this table seems definitely well designed and while the 1NF convention is respected the naming arguably "sucks".

    In the case of an indiscriminate storage of hobbies this is clearly wrong in most if not all cases I can think of right now. Your table has duplicate rows which goes against the 1NF principles.

    Let's not consider the reduced efficiency of SQL requests to access data from this table when you need to sort the results for paging or any other practical reason.

    Let's take into consideration the effort required to work with your data when your database will be used by another developer or team:

    • The data here is "scattered". You have to look in multiple columns to aggregate related data.
    • You are limited to only 3 of the hobbies.
    • You can't use simple rules to establish unicity (same hobby only once per user).

    You basically create frustration, anger and hatred and the Force is disturbed.

提交回复
热议问题