Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 1f22185

Browse files
committed
(GH-136) Complete 1NF section
1 parent d6601b3 commit 1f22185

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

src/rdbms/normalization.txt

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,18 @@ First normal form (1NF)
103103

104104
.. rubric:: Data example
105105

106-
.. todo: add data example (no NF)
106+
The table below represents players and their characters inventory, ordered by
107+
their characters' levels.
108+
109+
+------------+----------------------------------------------------------------+
110+
| username | inventory |
111+
+============+================================================================+
112+
| prombery87 | 2 amulets, 3 rings |
113+
+------------+----------------------------------------------------------------+
114+
| wheed1997 | 18 copper coins, 1 bow, 5 arrows |
115+
+------------+----------------------------------------------------------------+
116+
| acen1999 | 1 sword, 1 shield, 4 rings |
117+
+------------+----------------------------------------------------------------+
107118

108119
Requirements
109120
------------
@@ -117,8 +128,74 @@ Requirements
117128
Applying 1NF
118129
------------
119130

120-
.. todo: complete subsection
121-
.. todo: add data example in 1NF
131+
First of all, using the rows order to represent the information violates 1NF.
132+
This can be easily fixed by adding additional column *level* to store the info
133+
about the character's level. From this, the ordered sequences can be fetched
134+
by using the query:
135+
136+
.. code-block:: sql
137+
138+
SELECT username FROM player ORDER BY level;
139+
140+
The second rule is **do not** mix data types within the same column.
141+
As example, the characters' level might be described as follows:
142+
143+
+------------+----------------------------------+-----------------------------+
144+
| username | inventory | level |
145+
+============+==================================+=============================+
146+
| prombery87 | 2 amulets, 3 rings | 10 |
147+
+------------+----------------------------------+-----------------------------+
148+
| wheed1997 | 18 copper coins, 1 bow, 5 arrows | almost 8 |
149+
+------------+----------------------------------+-----------------------------+
150+
| acen1999 | 1 sword, 1 shield, 4 rings | 5.5 |
151+
+------------+----------------------------------+-----------------------------+
152+
153+
The data types are mixed in *level* column, which violates 1NF; and normally,
154+
RDBMS won't let you do this.
155+
156+
The tables without a **primary key** violates 1NF as well. *username* field
157+
may be used as **PK** for the players relation example. This would prevent
158+
the data duplication, like having both ``wheed1997`` players at level 7 and 8
159+
at the same time, which makes no sense.
160+
161+
+------------+----------------------------------+-----------------------------+
162+
| username | inventory | level |
163+
+============+==================================+=============================+
164+
| prombery87 | 2 amulets, 3 rings | 10 |
165+
+------------+----------------------------------+-----------------------------+
166+
| wheed1997 | 18 copper coins, 1 bow, 5 arrows | 8 |
167+
+------------+----------------------------------+-----------------------------+
168+
| wheed1997 | 18 copper coins, 1 bow, 5 arrows | 7 |
169+
+------------+----------------------------------+-----------------------------+
170+
| acen1999 | 1 sword, 1 shield, 4 rings | 5 |
171+
+------------+----------------------------------+-----------------------------+
172+
173+
At last, *inventory* column contains repeating groups of data (also called
174+
*non-atomic data*), which violates 1NF. To normalize the table, it should be
175+
altered in a way, to represent only one inventory item per row.
176+
177+
+------------+-------------+---------------+
178+
| player_id | item_type | item_quantity |
179+
+============+=============+===============+
180+
| prombery87 | amulet | 2 |
181+
+------------+-------------+---------------+
182+
| prombery87 | ring | 3 |
183+
+------------+-------------+---------------+
184+
| wheed1997 | copper coin | 18 |
185+
+------------+-------------+---------------+
186+
| wheed1997 | bow | 1 |
187+
+------------+-------------+---------------+
188+
| wheed1997 | arrow | 5 |
189+
+------------+-------------+---------------+
190+
| acen1999 | sword | 1 |
191+
+------------+-------------+---------------+
192+
| acen1999 | shield | 1 |
193+
+------------+-------------+---------------+
194+
| acen1999 | ring | 4 |
195+
+------------+-------------+---------------+
196+
197+
The primary key for this table is the combination of *player_id* and
198+
*item_type* -- a composite primary key.
122199

123200
Second normal form (2NF)
124201
========================

0 commit comments

Comments
 (0)