官术网_书友最值得收藏!

Three steps of normalization

Normalization helps you efficiently store data. Once your models are fully normalized, they will not have redundant data, and each model should contain data that is only logically related to it.

To give a quick example, if we were to normalize the post table so that we can unambiguously refer to the superhero who posted that message, then we need to isolate the user details in a separate table. Django already creates the user table by default. So, you only need to refer to the ID of the user who posted the message in the first column, as shown in the following table:

Now, it is not only clear that there were three messages posted by the same user (with an arbitrary user ID), but we can also find that user's correct name by looking up the user table.

Generally, you will design your models to be in their fully normalized form and then selectively denormalize them for performance reasons (see the next section on Performance to know why). In databases, normal forms are a set of guidelines that can be applied to a table to ensure that it is normalized. Commonly found normal forms are first, second, and third normal forms, although they could go up to the fifth normal form.

In the next example, we will normalize a table and create the corresponding Django models. Imagine a spreadsheet called Sightings that lists the first time someone spots a superhero using a power or superhuman ability. Each entry mentions the known origins, superpowers, and location of the first sighting, including latitude and longitude:

The preceding geographic data has been extracted from http://www.golombek.com/locations.html.

主站蜘蛛池模板: 合水县| 平度市| 南召县| 平远县| 尼玛县| 宁晋县| 沧州市| 宁化县| 塔城市| 台安县| 托克托县| 绵竹市| 射洪县| 屯昌县| 屯门区| 凯里市| 宝丰县| 浮梁县| 鄂托克前旗| 湖州市| 平遥县| 岗巴县| 沅江市| 通化市| 昆明市| 金乡县| 通海县| 探索| 兴文县| 来凤县| 任丘市| 随州市| 富蕴县| 巴林左旗| 闽清县| 通城县| 长沙市| 中江县| 赫章县| 宁蒗| 巴林左旗|