Which normal form does this table violate?

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

Consider this table:

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


        
6条回答
  •  萌比男神i
    2021-01-06 06:52

    Well,

    The point is that, as long as all hobby1, hobby2 and hobby3 values are not null, AND names are unique, this table could be considered more or less as abbiding by 1NF rules (see here for example ...)

    But does everybody has 3 hobbies? Of course not! Do not forget that databases are basically supposed to hold data as a representation of reality! So, away of all theories, one cannot say that everybody has 3 hobbies, except if ... our table is done to hold data related to people that have three hobbies without any preference between them!

    This said, and supposing that we are in the general case, the correct model could be

    +------------+-------+
    | id_person  |name   |
    +------------+-------+  
    

    for the persons (do not forget to unique key. I do not think 'name' is a good one

    +------------+-------+
    | id_hobby   |name   |
    +------------+-------+ 
    

    for the hobbies. id_hobby key is theoretically not mandatory, as hobby name can be the key ...

    +------------+-----------+
    | id_person  |id_hobby   |
    +------------+-----------+  
    

    for the link between persons and hobbies, as the physical representation of the many-to-many link that exists between persons and their hobbies.

    My proposal is basic, and satisfies theory. It can be improved in many ways ...

提交回复
热议问题