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

Commit 9a5f957

Browse files
committed
(GH-136) Complete 3NF section
1 parent e85da3a commit 9a5f957

File tree

1 file changed

+91
-16
lines changed

1 file changed

+91
-16
lines changed

src/rdbms/normalization.txt

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ As example, the characters' level might be described as follows:
143143
+------------+----------------------------------+-----------------------------+
144144
| username | inventory | level |
145145
+============+==================================+=============================+
146-
| prombery87 | 2 amulets, 3 rings | 10 |
146+
| prombery87 | 2 amulets, 3 rings | 9 |
147147
+------------+----------------------------------+-----------------------------+
148148
| wheed1997 | 18 copper coins, 1 bow, 5 arrows | almost 8 |
149149
+------------+----------------------------------+-----------------------------+
@@ -161,7 +161,7 @@ at the same time, which makes no sense.
161161
+------------+----------------------------------+-----------------------------+
162162
| username | inventory | level |
163163
+============+==================================+=============================+
164-
| prombery87 | 2 amulets, 3 rings | 10 |
164+
| prombery87 | 2 amulets, 3 rings | 9 |
165165
+------------+----------------------------------+-----------------------------+
166166
| wheed1997 | 18 copper coins, 1 bow, 5 arrows | 8 |
167167
+------------+----------------------------------+-----------------------------+
@@ -206,21 +206,21 @@ representing players and theirs inventory.
206206
+------------+-------------+---------------+--------------+
207207
| player_id | item_type | item_quantity | player_level |
208208
+============+=============+===============+==============+
209-
| prombery87 | amulet | 2 | 10 |
209+
| prombery87 | amulet | 2 | 9 |
210210
+------------+-------------+---------------+--------------+
211-
| prombery87 | ring | 3 | 10 |
211+
| prombery87 | ring | 3 | 9 |
212212
+------------+-------------+---------------+--------------+
213-
| wheed1997 | copper coin | 18 | 7 |
213+
| wheed1997 | copper coin | 18 | 6 |
214214
+------------+-------------+---------------+--------------+
215-
| wheed1997 | bow | 1 | 7 |
215+
| wheed1997 | bow | 1 | 6 |
216216
+------------+-------------+---------------+--------------+
217-
| wheed1997 | arrow | 5 | 7 |
217+
| wheed1997 | arrow | 5 | 6 |
218218
+------------+-------------+---------------+--------------+
219-
| acen1999 | sword | 1 | 5 |
219+
| acen1999 | sword | 1 | 3 |
220220
+------------+-------------+---------------+--------------+
221-
| acen1999 | shield | 1 | 5 |
221+
| acen1999 | shield | 1 | 3 |
222222
+------------+-------------+---------------+--------------+
223-
| acen1999 | ring | 4 | 5 |
223+
| acen1999 | ring | 4 | 3 |
224224
+------------+-------------+---------------+--------------+
225225

226226
The player's *level* should be duplicated for each row. This table design
@@ -263,11 +263,11 @@ So, it's the best solution to store player related data in a separate table.
263263
+------------+--------------+
264264
| player_id | player_level |
265265
+============+==============+
266-
| prombery87 | 10 |
266+
| prombery87 | 9 |
267267
+------------+--------------+
268-
| wheed1997 | 7 |
268+
| wheed1997 | 6 |
269269
+------------+--------------+
270-
| acen1999 | 5 |
270+
| acen1999 | 3 |
271271
+------------+--------------+
272272

273273
.. table:: player_inventory
@@ -295,7 +295,45 @@ So, it's the best solution to store player related data in a separate table.
295295
Third normal form (3NF)
296296
=======================
297297

298-
.. todo: definition, bcnf, example
298+
Let's assume, the player table is extended with the *player_rating* column,
299+
which describes the players' skills as "beginner", "intermediate", or
300+
"advanced". And let's assume, the skills are arranged in some like:
301+
302+
+--------------+-----------+--------------+-----------+
303+
| Skill Level | Beginner | Intermediate | Advanced |
304+
+--------------+---+---+---+----+----+----+---+---+---+
305+
| Player Level | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
306+
+--------------+---+---+---+----+----+----+---+---+---+
307+
308+
.. table:: player
309+
310+
+------------+--------------+---------------+
311+
| player_id | player_level | player_rating |
312+
+============+==============+===============+
313+
| prombery87 | 9 | advanced |
314+
+------------+--------------+---------------+
315+
| wheed1997 | 6 | intermediate |
316+
+------------+--------------+---------------+
317+
| acen1999 | 3 | beginner |
318+
+------------+--------------+---------------+
319+
320+
The *non-key attributes* dependencies can be described as follows:
321+
322+
::
323+
324+
{ player_id } -> { player_level }
325+
{ player_id } -> { player_level } -> { player_skill }
326+
327+
The *player_rating* does not depend on the primary key, but it depends on
328+
the *player_level* attribute, which is dependent on the primary key. These
329+
type of dependencies is called **transitive dependency**. Having these in
330+
a table opens a way for having data in an inconsistent form. E.g. let's assume
331+
player "acen1999" have increase their level to 4, but something went wrong,
332+
and *player_rating* has not been updated.
333+
334+
+------------+--------------+---------------+
335+
| acen1999 | 4 | beginner |
336+
+------------+--------------+---------------+
299337

300338
Requirements
301339
------------
@@ -316,8 +354,45 @@ Requirements
316354
Applying 3NF
317355
------------
318356

319-
.. todo: complete subsection
320-
.. todo: add data example in 3NF
357+
Introducing the "player_rating_levels" will fix the problem.
358+
359+
.. table:: player
360+
361+
+------------+--------------+
362+
| player_id | player_level |
363+
+============+==============+
364+
| prombery87 | 9 |
365+
+------------+--------------+
366+
| wheed1997 | 6 |
367+
+------------+--------------+
368+
| acen1999 | 3 |
369+
+------------+--------------+
370+
371+
.. table:: player_rating_levels
372+
373+
.. table:: player
374+
375+
+--------------+---------------+
376+
| player_level | player_rating |
377+
+==============+===============+
378+
| 9 | advanced |
379+
+--------------+---------------+
380+
| 8 | advanced |
381+
+--------------+---------------+
382+
| 7 | advanced |
383+
+--------------+---------------+
384+
| 6 | intermediate |
385+
+--------------+---------------+
386+
| 5 | intermediate |
387+
+--------------+---------------+
388+
| 4 | intermediate |
389+
+--------------+---------------+
390+
| 3 | beginner |
391+
+--------------+---------------+
392+
| 2 | beginner |
393+
+--------------+---------------+
394+
| 1 | beginner |
395+
+--------------+---------------+
321396

322397
Conclusion
323398
==========

0 commit comments

Comments
 (0)