|
| 1 | +******************************************************************************* |
| 2 | + Database Normalization |
| 3 | +******************************************************************************* |
| 4 | + |
| 5 | +Normalization is a process of organizing data in a database so that it is free |
| 6 | +from redundancy and dependency. It helps to eliminate data inconsistencies and |
| 7 | +anomalies, thereby improving data integrity. Normalization is a set of rules or |
| 8 | +guidelines to design a database schema in such a way that it avoids data |
| 9 | +duplication, data redundancy, and data inconsistency. |
| 10 | + |
| 11 | +There are several normal forms, each building on the previous one, which guide |
| 12 | +us in the normalization process. The most commonly used normal forms are: |
| 13 | + |
| 14 | +- **First Normal Form (1NF)**: |
| 15 | + Each column should hold atomic (indivisible) values. |
| 16 | + There should be no repeating groups or arrays of data in a table. |
| 17 | +- **Second Normal Form (2NF)**: |
| 18 | + Each non-key column should be functionally dependent on the entire primary |
| 19 | + key. In other words, every column in a table should be related to |
| 20 | + the primary key and not dependent on any other non-key columns. |
| 21 | +- **Third Normal Form (3NF)**: |
| 22 | + All non-key columns should be dependent only on the primary key and not on |
| 23 | + any other non-key columns. This eliminates transitive dependencies. |
| 24 | + |
| 25 | +Normalization reduces data redundancy and dependency, making the database more |
| 26 | +efficient, flexible, and scalable. It also helps in maintaining data |
| 27 | +consistency and accuracy, and ensures that updates and deletions are properly |
| 28 | +handled. |
| 29 | + |
| 30 | +First normal form (1NF) |
| 31 | +======================= |
| 32 | + |
| 33 | +It's easier to understand normalization via examples. |
| 34 | +Let's start with some dummy orders data. |
| 35 | + |
| 36 | +.. csv-table:: Initial data |
| 37 | + :header-rows: 1 |
| 38 | + |
| 39 | + order,customer,product,quantity,price,total |
| 40 | + 1,"John Doe","(Apple, Orange)","(2, 3)","(100, 50)","(200, 150)" |
| 41 | + 2,"Jane Smith","Banana",4,25,100 |
| 42 | + |
| 43 | +This table violates 1NF, since its columns contain multiple values. To bring |
| 44 | +this to 1NF all data within the table are to be atomic. |
| 45 | + |
| 46 | +.. csv-table:: 1NF applied |
| 47 | + :header-rows: 1 |
| 48 | + |
| 49 | + order,customer,product,quantity,price,total |
| 50 | + 1,"John Doe","Apple",2,100 ,200 |
| 51 | + 2,"John Doe","Orange",3,50 ,150 |
| 52 | + 3,"Jane Smith","Banana",4,25,100 |
| 53 | + |
| 54 | +From now each peace of data is represented as atomic value. |
| 55 | + |
| 56 | +Second normal form (2NF) |
| 57 | +======================== |
| 58 | + |
| 59 | +The table above is in 1NF, but ``item price`` is dependent on the *item* entity |
| 60 | +itself and have low cohesion to *orders*. To bring it into 2NF a dedicated |
| 61 | +table to store information about items themselves is to be created. After that |
| 62 | +``order`` and ``product`` table are connected (related) to each other via a |
| 63 | +foreign key. |
| 64 | + |
| 65 | +.. csv-table:: ``order`` table |
| 66 | + :header-rows: 1 |
| 67 | + |
| 68 | + id, customer, product_id, quantity |
| 69 | + 1, "John Doe", 1, 2 |
| 70 | + 2, "John Doe", 2, 3 |
| 71 | + 3, "Jane Smith", 3, 4 |
| 72 | + |
| 73 | +.. csv-table:: ``product`` table |
| 74 | + :header-rows: 1 |
| 75 | + |
| 76 | + id, product, price |
| 77 | + 1, "Apple", 100 |
| 78 | + 2, "Orange", 50 |
| 79 | + 3, "Banana", 25 |
| 80 | + |
| 81 | +.. mermaid:: |
| 82 | + :align: center |
| 83 | + |
| 84 | + erDiagram |
| 85 | + ORDER ||--|{ PRODUCT : contains |
| 86 | + ORDER { |
| 87 | + int id pk |
| 88 | + string customer |
| 89 | + int product_id fk |
| 90 | + int quantity |
| 91 | + } |
| 92 | + PRODUCT { |
| 93 | + int id pk |
| 94 | + string name |
| 95 | + int price |
| 96 | + } |
| 97 | + |
| 98 | +.. note:: ``total`` field is also removed, since now it can be calculated |
| 99 | + as product of ``product.price`` and ``order.quantity``. |
| 100 | + |
| 101 | +Third normal form (3NF) |
| 102 | +======================= |
| 103 | + |
| 104 | +At last it's time to remove a transitive functional dependency. Customer's name |
| 105 | +is not actually dependent on a product, but on a person, who places an order. |
| 106 | + |
| 107 | +.. csv-table:: ``order`` table |
| 108 | + :header-rows: 1 |
| 109 | + |
| 110 | + id, customer_id, product_id, quantity |
| 111 | + 1, 1, 1, 2 |
| 112 | + 2, 1, 2, 3 |
| 113 | + 3, 2, 3, 4 |
| 114 | + |
| 115 | +.. csv-table:: ``customer`` table |
| 116 | + :header-rows: 1 |
| 117 | + |
| 118 | + id, customer |
| 119 | + 1, "John Doe" |
| 120 | + 3, "Jane Smith" |
| 121 | + |
| 122 | +.. mermaid:: |
| 123 | + :align: center |
| 124 | + |
| 125 | + erDiagram |
| 126 | + CUSTOMER }o--|| ORDER : places |
| 127 | + ORDER ||--|{ PRODUCT : contains |
| 128 | + ORDER { |
| 129 | + int id pk |
| 130 | + int customer_id fk |
| 131 | + int product_id fk |
| 132 | + int quantity |
| 133 | + } |
| 134 | + PRODUCT { |
| 135 | + int id pk |
| 136 | + string name |
| 137 | + int price |
| 138 | + } |
| 139 | + CUSTOMER { |
| 140 | + int id pk |
| 141 | + string name |
| 142 | + } |
0 commit comments