aboutsummaryrefslogtreecommitdiff
path: root/08-relational-database-design/relational-database-design.ipynb
blob: d14df51f5dc42e748aa3cd3e9b6985a4e4bc3480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa6bdd5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import agh_db_lectures"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "893f4758",
   "metadata": {},
   "source": [
    "# Relational Database Design"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7034051",
   "metadata": {},
   "source": [
    "## Key Types"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78001f45",
   "metadata": {},
   "outputs": [],
   "source": [
    "agh_db_lectures.nw_diagram.download_open()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b84d4552",
   "metadata": {},
   "source": [
    "- superkey\n",
    "- candidate key\n",
    "- primary key\n",
    "- foreign key"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba402167",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "**Superkey** is a set of attributes whose values uniquely identify each tuple in a relation.  The `supplier_id` attributes itself forms a superkey of the `suppliers` relation.  But every superset of `{supplier_id}` (e.g., `{supplier_id, city}`) is also a superkey.\n",
    "\n",
    "For now, we can use our knowledge of the world and the business to tell whether something is a superkey.\n",
    "\n",
    "**Candidate key** is a minimal superkey, e.g., one that cannot have any attribute removed or it will no longer be a superkey.\n",
    "\n",
    "**Primary key** is one of the candidate keys selected by DB schema designer to use for identifying tuples in a relation.\n",
    "\n",
    "**Foreign key** is a set of attributes in a relation that are used to reference the primary key of another relation.  The foreign key attributes must be of the same types as the target primary key attributes.\n",
    "\n",
    "For each tuple in a relation with a foreign key there has to be a tuple in the referened relation that has the same values of primary key attributes (unless foreign key contains `NULL`s).\n",
    "\n",
    "Look for sample keys of all kinds in the Northwind db schema."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4234ac0",
   "metadata": {},
   "source": [
    "## Entity Relationship Model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e668e60",
   "metadata": {},
   "source": [
    "With examples featuring a model of a (git) source code hosting platform, like [Codeberg](https://codeberg.org/) or GitLab."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9aa04046",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "So far we've been working with databases using moderately low-level abstractions.  Tables and rows are sometimes called a logical model of the database.  They are specific to the relational data model and somewhat specific to the DBMS in use (for example, a Postgres DB schema might use the unsigned type `SERIAL` for a table column, whereas other DMBS might lack support for such data type).\n",
    "\n",
    "When designing a database schema, we might start with a conceptual model, like the **entity-relationship model**, instead.  Such a model can make it easier to comprehend a large design and it also might (or might not, depending who you work with) represent business data relationships in a way closer to how people on nontechnical positions think of them.\n",
    "\n",
    "There are various diagram notations for the ER model.  The notation of Peter Chen is the most popular and will be (more or less loosely) followed."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bca2777c",
   "metadata": {},
   "source": [
    "An **entity set**.\n",
    "\n",
    "![](er-model-entity-set-example.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e40c088c",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "We shall define **entity** as an element of the world that we want to model.  A person, product, article, purchase, etc. can be an entity.  We represent an **entity set** (\"type\" in some literature) with a rectangle.\n",
    "\n",
    "An entity can have **attributes**.  E.g., an `ACCOUNT` entity (being, e.g., a git forge account) can have an `EMAIL` attribute associated with it.  Attributes of an entity set (the \"attribute types\" in some more formal literature) are represented with ellipses.\n",
    "\n",
    "An attribute (type) can have a domain — the set of allowed values of that attribute.  For example, a set of valid email addresses.  A special value `null` can also belong to some attribute's domain.  Even though domains are often mentioned when discussing the ER model, they are not represented on the diagrams.\n",
    "\n",
    "A subset of entity set attributes should be selected that uniquely identifies each entity.  It is called the primary key.  Attibutes that are part of the primary key (key attributes) are represented with underlined attribute name.\n",
    "\n",
    "The ER model permits modeling attributes that are **derived** from other attributes.  For example, the age of an account can be computed from the registration date.  It is a derived attribute.  We represent it with an ellipse with a dashed outline.\n",
    "\n",
    "The ER model also permits modeling attributes that can have multiple values.  For example, a git forge account can have multiple SSH keys configured in order to authenticate from multiple devices.  The number of SSH keys would vary between the `ACCOUNT` entities.  We can represent SSH keys as a **multivalued** attribute, with double outline on the diagram.\n",
    "\n",
    "An attribute can also be **composite**, meaning it is further broken into parts.  E.g., the `LOGIN_DATA` of an `ACCOUNT` entity can be a composite attribute consisting of `PASSWORD_HASH` and `SALT`.  We could also make the parts themselves composite.  Perhaps it would make sense to represent `SALT` as consiting of algorithms type, iterations count and random data?  Nonetheless, an authentication API used by an application is likely to accept the entire `LOGIN_DATA` of an `ACCOUNT` as a single string-valued argument.\n",
    "\n",
    "Entities can form relationships.  For example, en `ACCOUNT` entity could be in a membership relationship with a `GROUP` entity.  Consider groups like [guix](https://codeberg.org/guix) on Codeberg.  Since we work with the notion of entity sets and multiple entities can be in multiplem membership relationships, we shall talk about **relationship sets**.  A relationship set is going to be represented with a diamond."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e5b5957",
   "metadata": {},
   "source": [
    "A **many-to-many** mapping.\n",
    "\n",
    "![](many-to-many.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36cf5a59",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A single `ACCOUNT` entity can be in a membership relationship with zero, one, or multiple `GROUP` entities.  Also, multiple `ACCOUNT` entities can be in a relationship with a single `GROUP` entity.  There can be many entities on each side of the relationship set, so we say it has a **many-to-many** cardinality.  There can also exist relationship sets with **one-to-one**, **one-to-many**, and **many-to-one** cardinalities.  The **cardinality** can be represented on the diagram with annotations like `1`, or `0..n`.  Some other common notations are just `n` or an asterisk (`*`) for the \"many\" side of a relationship set.\n",
    "\n",
    "If we wanted each group to have a single owner (aka admin), the relationship set of group ownership could be modeled with many-to-one mapping cardinality.  If, for some reason, we were to require that each user owns at most one group, that could modeled with a one-to-one mapping."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91d3ba17",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Entity set participates **totally** in a relationship set if every entity **has to** be in some relationship from the set.  Otherwise, it is participates **partially**.\n",
    "\n",
    "Notice that groups on a git forge could own code repositories (Codeberg works like this).  We can include an entity set `REPOSITORY` in our model.  Note that even though it has an attribute `REPO_NAME`, it is insufficient to uniquely identify all repositories on the forge.  Multiple groups can have repositories with the same name (like there are multiple repositories named \"guix\" on Codeberg).\n",
    "\n",
    "If an entity set does not have enough attributes to form a key, we say that it is **weak**.  A weak entity set has to participate in a relationship set with another entity set (or multiple other entity sets…) that is not weak (i.e., is **strong**).  We use the notions of an **identifying relationship**, and an **identifying entity set**.  In the weak entity set we designate a subset of attributes called the **discriminator**.  Together with the primary key of the identifying entity set, the discriminator forms the primary key of the weak entity set.  The weak entity and its identifying relationship set are going to be represented with double outlines on the diagrams.  A weak entity could of course additionally participate in other relationship sets that are not used to identify it (and are thus **not** represented with a double outline).\n",
    "\n",
    "An entity can be in a relationship with another entity of the same type.  As in case of the `FOLLOWS` relationship set (which is a **recursive** relationship set).  To distinguish between the left-side and right-side participation, we can **name the roles** of entities on each side (e.g., `follower` and `followed`).\n",
    "\n",
    "Finally, a relationship set can have attributes of its own.  We can add a `MEMBERSHIP_DATE` attribute to the relationship set is we want to record when someone has joined given group."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16ba4d5e",
   "metadata": {},
   "source": [
    "![](er-model-example.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d854025",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A ternary (or higher degree) relationship can occur between 3 (or more) entities.  Here we used it to record who added each account to group.  It is a minor detail that 2 sides of this ternary relationship set happen to invole the same entity set — an n-ary relationship set can just as well involve distinct entity sets.\n",
    "\n",
    "It is impossible to model it correctly by just binary relationship sets between the entity sets that we already have.  However, it would be possible to model it correctly with a weak entity set identified jointly by three other entity sets."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0090871b",
   "metadata": {},
   "source": [
    "## Enhanced Entity Relationship Model"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b5e50a0",
   "metadata": {},
   "source": [
    "EER model builds upon Peter Chen's ER model and adds specialization/generalization (described here) as well as aggregation (not covered).\n",
    "\n",
    "![](eer-model-example.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5b98e37",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A typical git forge would allow both groups **and** accounts to own repositories.  As such, the `ACCOUNT` and `GROUP` entity types could be thought of as **specializations** of another entity type that we could call `NAMESPACE_HOLDER` (since we typically refer to repositories as `https://some-forge.org/namespace_name/repo_name`).\n",
    "\n",
    "We could also say that our `NAMESPACE_HOLDER` entity type is a **generalization** of the other two types.  Whether we talk about specialization or generalization depends on whether we think top-down or bottom-up.\n",
    "\n",
    "Specialization is similar to inheritance in object-oriented programming languages.  A entity type that is a specialization of another type **inherits** its attributes, possibly also including some additional attributes of its own.\n",
    "\n",
    "Specialization can be represented with line going from an entity set to a circle and other lines going from the circle to the specializations.  The lines that go to specializations are crossed with arcs, as shown in the diagram.  Multi-level specialization/generalization hierarchies can be used.\n",
    "\n",
    "A specialization is **disjoint** if each entity is a member of at most one specialized type.  It is **overlapping** if an entity can be a member of multiple specialized types.  We denote it with letter \"d\" or \"o\" inside the circle.\n",
    "\n",
    "A specialization is **total** if each higher-level entity has to be a member of some specialized type.  It is **partial** if we allow higher-level entities not to be members of any specialized type.  We denote it with letter \"t\" or \"p\" along the line going from higher-level entity set to the circle.\n",
    "\n",
    "Aggregation and **categorization** (aka multiple inheritance) are also used in EER model but are not going to be covered here."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "273226ba",
   "metadata": {},
   "source": [
    "## Notes on (E)ER modeling"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f17dfe29",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Theodore, are there any reasons not to represent the `SSH_KEY` as another entity set rather than a multivalued attribute?\n",
    "\n",
    "There isn't always a rule whether to use a new entity type or a multivalued attribute (or a ternary relationship set).  \"Good taste\" is needed.\n",
    "\n",
    "There can be multiple valid choices for a primary key.  Also, one might choose to introduce an additional number (identifier) attribute to some entity set and use it in the primary key.\n",
    "\n",
    "**Do not** include primary key attributes of some entity as attributes of some other entity in order to make a \"foreign key\".  There are no foreign keys in the (E)ER model, use relationship sets instead :)\n",
    "\n",
    "A workflow can be as follows.\n",
    "\n",
    "- entity sets identification\n",
    "- relationship sets identification\n",
    "- deciding on cardinality of relationship sets' mappings\n",
    "- identification of entity set attributes\n",
    "- primary key selection\n",
    "- identification of weak entity sets and their identifying relationship sets and identifying entity sets\n",
    "- identification of weak entity sets' discriminators\n",
    "- abstractions\n",
    "  - specialization/generalization,\n",
    "  - aggregation, and\n",
    "  - categorization\n",
    "- determining whether the abstractions are\n",
    "  - total/partial, and\n",
    "  - disjoin/overlapping"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa25821b",
   "metadata": {},
   "source": [
    "## Mapping the Conceptual (E)ER Model to Relational Model\n",
    "\n",
    "We shall give some examples but omit formal algorithm descriptions."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6e246ff",
   "metadata": {},
   "source": [
    "#### Entity Sets and Attributes\n",
    "\n",
    "![](er-model-entity-w-attributes.svg)\n",
    "\n",
    "-----------------------------\n",
    "\n",
    "![](er-model-entity-w-attributes-mapped.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8170f6e1",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Entity set gets mapped to a relation. Ordinary attributes become relation attributes.\n",
    "\n",
    "Multivalued attributes get their own relations.\n",
    "\n",
    "Derived attributes are absent in relation schema.\n",
    "\n",
    "Note how we mark primary keys and foreign key constraints in schema diagram.  Be aware that different notations are in use.  See the WP database schema for another common way to mark cardinalities."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cfb437c",
   "metadata": {},
   "source": [
    "#### Entity Relationships and Weak Entity Types\n",
    "\n",
    "![](er-model-example.svg)\n",
    "\n",
    "-----------------------------\n",
    "\n",
    "![](er-model-mapped.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfc17db9",
   "metadata": {},
   "source": [
    "#### Generalization / Specialization\n",
    "\n",
    "![](eer-model-example.svg)\n",
    "\n",
    "-----------------------------\n",
    "\n",
    "![](eer-model-mapped.svg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3cf4dec",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Alternative approach is to have just one relation for all specialized types with the attributes of all specializations present there and nullable."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aba0baab",
   "metadata": {},
   "source": [
    "## Schema Normalization"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ddb4d0e",
   "metadata": {},
   "source": [
    "Let's formalize:\n",
    "\n",
    "#### Relation Schema\n",
    "\n",
    "$some\\_relation({attribute_1, attribute_2, attribute_3})$\n",
    "\n",
    "#### Relation Instance\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">attribute_1</th>\n",
    "      <th style=\"text-align: center\">attribute_2</th>\n",
    "      <th style=\"text-align: center\">attribute_3</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1112</td>\n",
    "      <td style=\"text-align: center\">4</td>\n",
    "      <td style=\"text-align: center\">12.43</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1345</td>\n",
    "      <td style=\"text-align: center\">4</td>\n",
    "      <td style=\"text-align: center\">3.19</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1564</td>\n",
    "      <td style=\"text-align: center\">8</td>\n",
    "      <td style=\"text-align: center\">17.03</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01deea02",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Schema is a design of relation (or entire database) structure, including integrity constraints.\n",
    "\n",
    "Instance is a data snapshot of a relation at given point in time."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c00c5277",
   "metadata": {},
   "source": [
    "### The Use-Case of Normalization"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "276f2d2a",
   "metadata": {},
   "source": [
    "Assume another relation schema was designed to store information about issues and comments under them.\n",
    "\n",
    "$R = \\{issue\\_number, repo\\_name, owner\\_name, issue\\_text, comment\\_id, comment\\_text\\}$\n",
    "\n",
    "$issue\\_comment(R)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "365e5a93",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Such relation schema could be, for example, the result of ad-hoc design.  We deliberately make the example small by, e.g., omitting authorship information."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b232eb0c",
   "metadata": {},
   "source": [
    "Consider an instance of this relation with some data.\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">issue_number</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "      <th style=\"text-align: center\">issue_text</th>\n",
    "      <th style=\"text-align: center\">comment_id</th>\n",
    "      <th style=\"text-align: center\">comment_text</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1112</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">I get `bash: git: command not found` when trying to download this :(</td>\n",
    "      <td style=\"text-align: center\">9814</td>\n",
    "      <td style=\"text-align: center\">You need to install the git command.  E.g., with `apt install git`.</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1112</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">I get `bash: git: command not found` when trying to download this :(</td>\n",
    "      <td style=\"text-align: center\">9819</td>\n",
    "      <td style=\"text-align: center\">Works like a charm. Thanks!</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1345</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">`librecad` package lacks icons and .desktop files from the upstream project at https://github.com/LibreCAD/LibreCAD.</td>\n",
    "      <td style=\"text-align: center\">10239</td>\n",
    "      <td style=\"text-align: center\">Fixed in 58b522a.</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15d3af8d",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "What are the problem with this relation schema?\n",
    "\n",
    "- duplicate storage of `issue_text`\n",
    "- need to be careful when updating it\n",
    "- need to be careful when deleting an issue\n",
    "- challenge of recording an issue that has no comments yet (`NULL` in `comment_*` columns?)\n",
    "\n",
    "It is often said that a design suffers from **anomalies** of data modification.  Data redundancy is bad, mkay?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0701ae4f",
   "metadata": {},
   "source": [
    "#### Decomposed Relation\n",
    "\n",
    "$issue(issue\\_number, repo\\_name, owner\\_name, issue\\_text)$\n",
    "\n",
    "$comment(comment\\_id, issue\\_number, comment\\_text)$\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">issue_number</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "      <th style=\"text-align: center\">issue_text</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1112</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">I get `bash: git: command not found` when trying to download this :(</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1345</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">`librecad` package lacks icons and .desktop files from the upstream project at https://github.com/LibreCAD/LibreCAD.</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">comment_id</th>\n",
    "      <th style=\"text-align: center\">issue_number</th>\n",
    "      <th style=\"text-align: center\">comment_text</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">9814</td>\n",
    "      <td style=\"text-align: center\">1112</td>\n",
    "      <td style=\"text-align: center\">You need to install the git command.  E.g., with `apt install git`.</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">9819</td>\n",
    "      <td style=\"text-align: center\">1112</td>\n",
    "      <td style=\"text-align: center\">Works like a charm. Thanks!</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">10239</td>\n",
    "      <td style=\"text-align: center\">1345</td>\n",
    "      <td style=\"text-align: center\">Fixed in 58b522a.</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25075bfa",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "We can **decompose** relation schemas with data redundancy into smaller relation schemas that do not suffer from this problem.\n",
    "\n",
    "The resulting tables here would be joinable on the `issue_number` attribute.  No information would be lost."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c69b175",
   "metadata": {},
   "source": [
    "#### Lossy Decomposition\n",
    "\n",
    "$issue(issue\\_number, repo\\_name, owner\\_name, issue\\_text)$\n",
    "\n",
    "$comment(comment\\_id, repo\\_name, owner\\_name, comment\\_text)$\n",
    "\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">issue_number</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "      <th style=\"text-align: center\">issue_text</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1112</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">I get `bash: git: command not found` when trying to download this :(</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1345</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">`librecad` package lacks icons and .desktop files from the upstream project at https://github.com/LibreCAD/LibreCAD.</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">comment_id</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "      <th style=\"text-align: center\">comment_text</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">9814</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">You need to install the git command.  E.g., with `apt install git`.</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">9819</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">Works like a charm. Thanks!</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">10239</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">Fixed in 58b522a.</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a17fd70",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Such decomposed schema is broken — it doesn't keep the information about the precise issue that a comment has been made under.  There could possibly be multiple issues opened for the same repo."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df8d739e",
   "metadata": {},
   "source": [
    "Decomposition is **lossy** if it loses information."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9bf86f1",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Decomposition is **lossless** if and only if a natural join of decomposed smaller relations gives back the original relation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e56af3b",
   "metadata": {},
   "source": [
    "## 1NF\n",
    "\n",
    "First normal form."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abe553fe",
   "metadata": {},
   "source": [
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">user_name</th>\n",
    "      <th colspan=\"2\" style=\"text-align: center\">privileges</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">repo_owner_name</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\"></th>\n",
    "      <th style=\"text-align: center\">branch_name</th>\n",
    "      <th style=\"text-align: center\">access_type</th>\n",
    "      <th style=\"text-align: center\"></th>\n",
    "      <th style=\"text-align: center\"></th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td rowspan=\"3\" style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td rowspan=\"3\" style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td rowspan=\"3\" style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">join-diagram-wip</td>\n",
    "      <td style=\"text-align: center\">push</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore-experiments</td>\n",
    "      <td style=\"text-align: center\">forcepush</td>\n",
    "    </tr>\n",
    "    <tr style=\"border-top: 1px solid black\">\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr style=\"border-top: 1px solid black\">\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">trunk</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "    </tr>\n",
    "    <tr style=\"border-top: 1px solid black\">\n",
    "      <td style=\"text-align: center\">eugenia</td>\n",
    "      <td style=\"text-align: center\">trunk</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b0ab2ed",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A relation is in 1NF if it has no multivalued nor composite attributes.  Note that many RDBMS engines do not even have dedicated support for such attributes.  Others do.  For example, Postgres features an array type, and SQL standard since 2003 specifies an XML type.\n",
    "\n",
    "Depending on the semantic of data, a single string-valued attribute can be effetively composite (or multivalued).  One example is the `user_pass` attribute in WP database that holds both the password hash and the salt in a single string.\n",
    "\n",
    "The above is not in 1NF."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31aad002",
   "metadata": {},
   "source": [
    "### Decomposition to 1NF"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7c7aa74",
   "metadata": {},
   "source": [
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">user_name</th>\n",
    "      <th style=\"text-align: center\">branch_name</th>\n",
    "      <th style=\"text-align: center\">access_type</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">repo_owner_name</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">join-diagram-wip</td>\n",
    "      <td style=\"text-align: center\">push</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">theodore-experiments</td>\n",
    "      <td style=\"text-align: center\">forcepush</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">trunk</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">eugenia</td>\n",
    "      <td style=\"text-align: center\">trunk</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f1f6b08",
   "metadata": {},
   "source": [
    "## Functional Dependencies"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba007b76",
   "metadata": {},
   "source": [
    "$a, b, c, d, e$ — relation attributes\n",
    "\n",
    "$\\{a, b\\} \\rightarrow \\{c, d\\}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c560c52e",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Functional dependencies between relation attributes can be used to approach the issue of normalization in a formal way.\n",
    "\n",
    "We will use arrow notation.\n",
    "\n",
    "Functional dependency means that values of LHS attributes uniquely determine the values of RHS attributes."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a61548c",
   "metadata": {},
   "source": [
    "Assume the following relation.\n",
    "\n",
    "$r(a, b, c, d, e)$\n",
    "\n",
    "$t_1, t_2$ — tuples of a relation instance\n",
    "\n",
    "The functional dependency $\\{a, b\\} \\rightarrow \\{c, d\\}$ means that:\n",
    "\n",
    "$(t_1[a], t_1[b]) = (t_2[a], t_2[b]) \\implies (t_1[c], t_1[d]) = (t_2[c], t_2[d])$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d721d7eb",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "I.e., if we know the values of LHS attributes, we could tell what the RHS attribute values are.\n",
    "\n",
    "We can say that a relation instance **satisfies** a functional dependency.  If all legal instances of a relation do so, then the functional dependency **holds** on schema."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49ce1901",
   "metadata": {},
   "source": [
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">a</th>\n",
    "      <th style=\"text-align: center\">b</th>\n",
    "      <th style=\"text-align: center\">c</th>\n",
    "      <th style=\"text-align: center\">d</th>\n",
    "      <th style=\"text-align: center\">e</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1</td>\n",
    "      <td style=\"text-align: center\">1</td>\n",
    "      <td style=\"text-align: center\">13</td>\n",
    "      <td style=\"text-align: center\">5.67</td>\n",
    "      <td style=\"text-align: center\">24</td>\n",
    "      </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">1</td>\n",
    "      <td style=\"text-align: center\">1</td>\n",
    "      <td style=\"text-align: center\">13</td>\n",
    "      <td style=\"text-align: center\">5.67</td>\n",
    "      <td style=\"text-align: center\">24</td>\n",
    "      </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">2</td>\n",
    "      <td style=\"text-align: center\">1</td>\n",
    "      <td style=\"text-align: center\">43</td>\n",
    "      <td style=\"text-align: center\">0.43</td>\n",
    "      <td style=\"text-align: center\">11</td>\n",
    "      </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">2</td>\n",
    "      <td style=\"text-align: center\">2</td>\n",
    "      <td style=\"text-align: center\">54</td>\n",
    "      <td style=\"text-align: center\">15.53</td>\n",
    "      <td style=\"text-align: center\">0</td>\n",
    "      </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">2</td>\n",
    "      <td style=\"text-align: center\">2</td>\n",
    "      <td style=\"text-align: center\">54</td>\n",
    "      <td style=\"text-align: center\">15.53</td>\n",
    "      <td style=\"text-align: center\">9</td>\n",
    "      </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d066613",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Relation instance presented above satisfies the functional dependency we are considering.\n",
    "\n",
    "Change the value of `c` in the last row to 77.  The new instance does not satisfy the functional dependency."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9f7fd06",
   "metadata": {},
   "source": [
    "#### Examples From the Git Forge Schema\n",
    "\n",
    "$name \\rightarrow email$\n",
    "\n",
    "$\\{added, group\\_name\\} \\rightarrow adder$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c70462ea",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "LHS and RHS of a functional dependency are **sets** of attributes but we can abuse the notation and drop the braces when we have just one attribute."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57392202",
   "metadata": {},
   "source": [
    "#### Trivial Functional Dependencies\n",
    "\n",
    "$\\{added, group\\_name\\} \\rightarrow group\\_name$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd4417ef",
   "metadata": {},
   "source": [
    "#### Armstrong's Axioms"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c3da0ab",
   "metadata": {},
   "source": [
    "$\\alpha, \\beta, \\gamma$ — attribute sets\n",
    "\n",
    "$\\alpha\\gamma$ — simplified notation for $\\alpha{\\cup}\\gamma$\n",
    "\n",
    "The axioms are:\n",
    "\n",
    "$\\beta{\\subseteq}\\alpha \\implies \\alpha \\rightarrow \\beta$\n",
    "\n",
    "$\\alpha \\rightarrow \\beta \\implies \\alpha\\gamma \\rightarrow \\beta\\gamma$\n",
    "\n",
    "$\\alpha \\rightarrow \\beta \\land \\beta \\rightarrow \\gamma \\implies \\alpha \\rightarrow \\gamma$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9fc19a9",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Functional dependencies can logically imply other functional dependencies.  A set $F^+$ of all functional dependencies implied by set $F$ is called **closure**.  Armstrong's axioms allow finding the closure of every set of functional dependencies.\n",
    "\n",
    "Other tautologies can be proved from these axioms."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17435f19",
   "metadata": {},
   "source": [
    "#### Implied Functional Dependencies Example\n",
    "\n",
    "We have the following functional dependencies.\n",
    "\n",
    "$name \\rightarrow email$\n",
    "\n",
    "$email \\rightarrow \\{name, registration\\_date, email, password\\_hash, salt\\}$\n",
    "\n",
    "We can then prove the following as well.\n",
    "\n",
    "$\\{password\\_hash, salt\\} \\rightarrow \\{password\\_hash, salt\\}$\n",
    "\n",
    "$name \\rightarrow \\{registration\\_date, email\\}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e010e5f8",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "$\\{password\\_hash, salt\\} \\rightarrow \\{password\\_hash, salt\\}$ is a trivial one."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c4e78ab",
   "metadata": {},
   "source": [
    "## BCNF\n",
    "\n",
    "Boyce-Codd Normal Form (also called 3.5 Normal Form)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56a05fdc",
   "metadata": {},
   "source": [
    "Let\n",
    "\n",
    "$R$ — set of relation attributes\n",
    "\n",
    "$F$ — set of functional dependencies with attributes in $R$\n",
    "\n",
    "$F^+$ — closure of $F$\n",
    "\n",
    "A relation schema is in BCNF if and only if it is in 1NF, and\n",
    "\n",
    "$\\forall (\\alpha \\rightarrow \\beta){\\in}F^+: \\beta{\\subseteq}\\alpha \\lor \\alpha\\ is\\ a\\ superkey$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7875974e",
   "metadata": {},
   "source": [
    "### BCNF Example"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "403002aa",
   "metadata": {},
   "source": [
    "Consider again the following attributes and relation schema.\n",
    "\n",
    "$R = \\{issue\\_number, repo\\_name, owner\\_name, issue\\_text, comment\\_id, comment\\_text\\}$\n",
    "\n",
    "$issues\\_comments(R)$\n",
    "\n",
    "We have these (and more) functional dependencies.\n",
    "\n",
    "$comment\\_id \\rightarrow comment\\_text$\n",
    "\n",
    "$comment\\_id \\rightarrow issue\\_number$\n",
    "\n",
    "$issue\\_number \\rightarrow \\{repo\\_name, owner\\_name, issue\\_text\\}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b15e1c7c",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "We won't be writing down all the other functional dependencies that result from these.  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d42cef9",
   "metadata": {},
   "source": [
    "The first two functional dependencies are OK wrt the BCNF requirements."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ddbb3d60",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "`comment_id` is the primary key (and **the only** candidate key)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62600027",
   "metadata": {},
   "source": [
    "The last functional dependency\n",
    "\n",
    "- is not trivial, and\n",
    "- has LHS that is not a superkey.\n",
    "\n",
    "Therefore, the dependency $issue\\_number \\rightarrow \\{repo\\_name, owner\\_name, issue\\_text\\}$ violates BCNF."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc9e10cb",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "`issue_number` does not uniquely identify each tuple in the relation."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42293b57",
   "metadata": {},
   "source": [
    "### Decomposition to BCNF Schemas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3a6e1c4",
   "metadata": {},
   "source": [
    "If we have a nontrivial functional dependency $\\alpha \\rightarrow \\beta$ that violates BCNF, then we can decompose the relation schema into two separate schemas:\n",
    "\n",
    "$\\alpha{\\cup}\\beta$\n",
    "\n",
    "$R{-}(\\beta{-}\\alpha)$\n",
    "\n",
    "In our case:\n",
    "\n",
    "$R = \\{issue\\_number, repo\\_name, owner\\_name, issue\\_text, comment\\_id, comment\\_text\\}$\n",
    "\n",
    "$\\alpha = issue\\_number$\n",
    "\n",
    "$\\beta = \\{repo\\_name, owner\\_name, issue\\_text\\}$\n",
    "\n",
    "We get the following decomposed relation schemas.\n",
    "\n",
    "$issue(\\alpha{\\cup}\\beta) = issue(issue\\_number, repo\\_name, owner\\_name, issue\\_text)$\n",
    "\n",
    "$comment(R{-}(\\beta{-}\\alpha)) = comment(comment\\_id, issue\\_number, comment\\_text)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e7203ae",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Here this would give us the same decomposition we used earlier.\n",
    "\n",
    "Note that merely checking if a database schema is in BCNF is an NP-complete problem.  We don't discuss the particular algorithms as part of this course."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8127d26",
   "metadata": {},
   "source": [
    "### Dependency Preservation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf4d2695",
   "metadata": {},
   "source": [
    "Consider this schema, dependencies, and sample instance.\n",
    "\n",
    "$repo\\_role\\_user(repo\\_name, owner\\_name, role\\_id, user)$\n",
    "\n",
    "$\\{repo\\_name, owner\\_name, user\\} \\rightarrow role\\_id$\n",
    "\n",
    "$role\\_id \\rightarrow \\{repo\\_name, owner\\_name\\}$\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "      <th style=\"text-align: center\">role_id</th>\n",
    "      <th style=\"text-align: center\">user</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">377</td>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "      <td style=\"text-align: center\">eugenia</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "baefb785",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Assume a scenario where each repository can have multiple roles defined and users can have at most one role for each repository.  Role is repo-specific and defines privileges."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea61a132",
   "metadata": {},
   "source": [
    "We have the following candidate keys.\n",
    "\n",
    "$k_1 = \\{repo\\_name, owner\\_name, user\\}$\n",
    "\n",
    "$k_2 = \\{role\\_id, user\\}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "452a3c2e",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Functional dependency $\\{repo, user\\} \\rightarrow role\\_id$ has a candidate key on its LHS.  It does not violate BCNF.\n",
    "\n",
    "Functional dependency $role\\_id \\rightarrow \\{repo\\_name, owner\\_name\\}$ is not trivial and its LHS is not a superkey.  It does violate the BCNF."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "836735af",
   "metadata": {},
   "source": [
    "We decompose according to our rule specified earlier.\n",
    "\n",
    "$R = \\{repo\\_name, owner\\_name, role\\_id, user\\}$\n",
    "\n",
    "$\\alpha = role\\_id$\n",
    "\n",
    "$\\beta = \\{repo\\_name, owner\\_name\\}$\n",
    "\n",
    "The following are new relation schemas and their sample instances.\n",
    "\n",
    "$role\\_repo(\\alpha{\\cup}\\beta) = role\\_repo(role\\_id, repo\\_name, owner\\_name)$\n",
    "\n",
    "$user\\_role(R{-}(\\beta{-}\\alpha)) = user\\_role(user, role\\_id)$\n",
    "\n",
    "<div style=\"display: flex\">\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">role_id</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">377</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">user</th>\n",
    "      <th style=\"text-align: center\">role_id</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">377</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">eugenia</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "</div>\n",
    "\n",
    "Recall our functional dependency $\\{repo\\_name, owner\\_name, user\\} \\rightarrow role\\_id$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98f0debb",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "This dependency involves attributes that no longer appear together in any single relation schema.\n",
    "\n",
    "We could no longer tell whether this dependency is satisfied by looking at a single relation instance.\n",
    "\n",
    "We say this decomposition **is not dependency preserving**.  This is an undesirable situation.\n",
    "\n",
    "Not all relation schemas have a dependency preserving decomposition to a set of BCNF shemas."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "337dfbfc",
   "metadata": {},
   "source": [
    "## 3NF\n",
    "\n",
    "Third Normal Form."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15e40158",
   "metadata": {},
   "source": [
    "Recall that a relation schema is in BCNF if and only if it is in 1NF, and\n",
    "\n",
    "$\\forall (\\alpha \\rightarrow \\beta){\\in}F^+: \\beta{\\subseteq}\\alpha \\lor \\alpha\\ is\\ a\\ superkey$\n",
    "\n",
    "Let's relax the condition relating to dependencies.  Replace it with the following.\n",
    "\n",
    "$\\forall (\\alpha \\rightarrow \\beta){\\in}F^+: \\beta{\\subseteq}\\alpha \\lor \\alpha\\ is\\ a\\ superkey \\lor \\forall A{\\in}(\\beta-\\alpha) \\exists k{\\in}K: A{\\in}k\\$\n",
    "\n",
    "where\n",
    "\n",
    "$K$ — set of relation's all candidate keys"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0852599",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "This is one of the possible, equivalent definitions of 3NF (not the canonical one).\n",
    "\n",
    "3NF permits nonkey attributes on the LHS of a nontrivial functional dependency provided that the relevant attributes in the RHS belong to some candidate keys.  These attribute are not required to belong to the same candidate key, though."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "036a3ccf",
   "metadata": {},
   "source": [
    "### 3NF in the User Roles Example"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69bad4c2",
   "metadata": {},
   "source": [
    "Recall our earlier schema, dependencies and candidate keys.\n",
    "\n",
    "$repo\\_role\\_user(repo\\_name, owner\\_name, role\\_id, user)$\n",
    "\n",
    "$\\{repo\\_name, owner\\_name, user\\} \\rightarrow role\\_id$\n",
    "\n",
    "$role\\_id \\rightarrow \\{repo\\_name, owner\\_name\\}$\n",
    "\n",
    "$k_1 = \\{repo\\_name, owner\\_name, user\\}$\n",
    "\n",
    "$k_2 = \\{role\\_id, user\\}$\n",
    "\n",
    "We see that $repo\\_name{\\in}k_1$ and $owner\\_name{\\in}k_1$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c842f46",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "The second functional dependency violates BCNF on $user\\_repo\\_role$ and leads to a decomposition that is not dependency preserving.\n",
    "\n",
    "If we instead strive for 3NF, we shall see that this dependency does not violate it.  Dependency's RHS attributes all belong to a candidate key.  3NF does not require further decomposition of this schema.\n",
    "\n",
    "3NF is weaker than BCNF and allows some degree of redundancy while preserving functional dependencies."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6e2c0ba",
   "metadata": {},
   "source": [
    "### Multivalued dependencies"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2461d28c",
   "metadata": {},
   "source": [
    "Assume users can watch (aka subscribe to) others' repositories.  Consider this relation schema and sample instance.\n",
    "\n",
    "$account(name, email, ssh\\_key, watched\\_repo\\_name, watched\\_repo\\_owner)$\n",
    "\n",
    "$name \\rightarrow email$\n",
    "\n",
    "$email \\rightarrow name$\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">name</th>\n",
    "      <th style=\"text-align: center\">email</th>\n",
    "      <th style=\"text-align: center\">watched_repo_name</th>\n",
    "      <th style=\"text-align: center\">watched_repo_owner</th>\n",
    "      <th style=\"text-align: center\">ssh_key</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">wkosior@agh.edu.pl</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…dyItC</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">wkosior@agh.edu.pl</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…ujdSO</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wk-bot</td>\n",
    "      <td style=\"text-align: center\">wk-bot@gnu.org</td>\n",
    "      <td style=\"text-align: center\">apache-configs</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…ujdSO</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">543210@student.agh.edu.pl</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…T7EjR</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">543210@student.agh.edu.pl</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">ssh-rsa <span style=\"white-space: pre\">AAAAB3…PucE=</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">543210@student.agh.edu.pl</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…T7EjR</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">543210@student.agh.edu.pl</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "      <td style=\"text-align: center\">ssh-rsa <span style=\"white-space: pre\">AAAAB3…PucE=</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">500000@student.agh.edu.pl</td>\n",
    "      <td style=\"text-align: center\">apache-configs</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">ssh-dss <span style=\"white-space: pre\">AAAAB3…0yxM=</span></td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37db72bb",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "A user can have multiple SSH keys and can be watching multiple repositories.  It's obviously wrong to store all these in a single relation, but let's hold on to this example just for a moment.\n",
    "\n",
    "Please do not mind the omission of login data in the example and truncated display of SSH keys — this is to avoid making the table even wider.  Please also treat $ssh\\_key$ as if it were an atomic attribute, even though we see a way to decompose it.\n",
    "\n",
    "There's clearly no dependency between the SSH keys and watched repositories.  For each user we shall therefore repeat user's every SSH key with user's every watched repository, in separate rows / tuples.  As when computing a cartesian product.\n",
    "\n",
    "Temporarily remove the 2 middle rows of Theodore.  Note that now the relation instance indicates some connection between Theodore's particular key and watched repository.  This is not what we want, so we need to get back to the cartesian-product-like instance.\n",
    "\n",
    "Note that there are no nontrivial functional dependencies involving $ssh\\_key$, $watched\\_repo\\_name$, and $watched\\_repo\\_owner$.\n",
    "\n",
    "The relation schema, although bad, is in BCNF.  We have not yet learned any formal rule we could use to decompose it."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9495194a",
   "metadata": {},
   "source": [
    "We have the following **multivalued dependencies**.\n",
    "\n",
    "$name \\twoheadrightarrow ssh\\_key$\n",
    "\n",
    "$name \\twoheadrightarrow \\{watched\\_repo\\_name, watched\\_repo\\_owner\\}$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb5b6793",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "The first dependency means that the value of $name$ is always associated with the same set of values of $ssh\\_key$, regardless of the values of other attribtues.\n",
    "\n",
    "The second dependency means that the value of $name$ is always associated with the same set of value tuples of $\\{watched\\_repo\\_name, watched\\_repo\\_owner\\}$, regardless of the values of other attributes.\n",
    "\n",
    "Our instance satisfies both dependencies.  If we remove the middle 2 of Theodore's rows, it shall satisfy neither.\n",
    "\n",
    "The mathematical notation for multivalued dependency constraint can be found in literature."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "137a3d4f",
   "metadata": {},
   "source": [
    "More could be derived, for example the following.\n",
    "\n",
    "$email \\twoheadrightarrow \\{watched\\_repo\\_name, watched\\_repo\\_owner\\}$\n",
    "\n",
    "$\\{ssh\\_key, watched\\_repo\\_name\\} \\twoheadrightarrow ssh\\_key$ — a trivial one"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "035dbf6a",
   "metadata": {},
   "source": [
    "### 4NF\n",
    "\n",
    "Fourth Normal Form."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88a61c3d",
   "metadata": {},
   "source": [
    "Let\n",
    "\n",
    "$R$ — set of relation attributes\n",
    "\n",
    "$M^+$ — set of all functional **and** multivalued dependencies with attributes in $R$\n",
    "\n",
    "A relation schema is in 4NF if and only if it is in BCNF, and\n",
    "\n",
    "$\\forall (\\alpha \\twoheadrightarrow \\beta){\\in}M^+: \\beta{\\subseteq}\\alpha \\lor \\alpha{\\cup}\\beta = R \\lor \\alpha\\ is\\ a\\ superkey$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ea649af",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Note that every superkey of our sample relation schema must contain all three: $ssh\\_key$, $watched\\_repo\\_name$, and $watched\\_repo\\_owner$.  Sample candidate key is $\\{name, ssh\\_key, watched\\_repo\\_name, watched\\_repo\\_owner\\}$.\n",
    "\n",
    "The dependency $name \\twoheadrightarrow ssh\\_key$ is not trivial and its LHS is not a superkey.\n",
    "\n",
    "Similarly, the dependency $name \\twoheadrightarrow \\{watched\\_repo\\_name, watched\\_repo\\_owner\\}$ is not trivial and its LHS is not a superkey.\n",
    "\n",
    "The relation schema is in BCNF but it is not in 4NF."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55ee9e3f",
   "metadata": {},
   "source": [
    "### Decomposition to 4NF Schemas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0769a7de",
   "metadata": {},
   "source": [
    "If we have a nontrivial multivalued dependency $\\alpha \\twoheadrightarrow \\beta$ that violates 4NF, then we can decompose the relation schema into two separate schemas:\n",
    "\n",
    "$\\alpha{\\cup}\\beta$\n",
    "\n",
    "$R{-}(\\beta{-}\\alpha)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "268228f4",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "This is analogous to our rule of decomposition to BCNF schemas."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64a283c9",
   "metadata": {},
   "source": [
    "In our case:\n",
    "\n",
    "$R = \\{name, email, ssh\\_key, watched\\_repo\\_name, watched\\_repo\\_owner\\}$\n",
    "\n",
    "$\\alpha = name$\n",
    "\n",
    "$\\beta = ssh\\_key$\n",
    "\n",
    "We get the following decomposed relation schemas.\n",
    "\n",
    "$account\\_ssh\\_key(\\alpha{\\cup}\\beta) = account\\_ssh\\_key(name, ssh\\_key)$\n",
    "\n",
    "$account'(R{-}(\\beta{-}\\alpha)) = account'(name, email, watched\\_repo\\_name, watched\\_repo\\_owner)$"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f13ae7bb",
   "metadata": {},
   "source": [
    "The $account'$ schema is still not in 4NF, so we can decompose it furter and get the following 4NF schemas and sample relation instances.\n",
    "\n",
    "$account\\_ssh\\_key(name, ssh\\_key)$\n",
    "\n",
    "$account\\_watch(name, watched\\_repo\\_name, watched\\_repo\\_owner)$\n",
    "\n",
    "$account''(name, email)$\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">name</th>\n",
    "      <th style=\"text-align: center\">email</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">wkosior@agh.edu.pl</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wk-bot</td>\n",
    "      <td style=\"text-align: center\">wk-bot@gnu.org</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">543210@student.agh.edu.pl</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">500000@student.agh.edu.pl</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "\n",
    "<div style=\"display: flex\">\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">name</th>\n",
    "      <th style=\"text-align: center\">ssh_key</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…dyItC</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…ujdSO</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wk-bot</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…ujdSO</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">ssh-ed25519 <span style=\"white-space: pre\">AAAAC3…T7EjR</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">ssh-rsa <span style=\"white-space: pre\">AAAAB3…PucE=</span></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">ssh-dss <span style=\"white-space: pre\">AAAAB3…0yxM=</span></td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">name</th>\n",
    "      <th style=\"text-align: center\">watched_repo_name</th>\n",
    "      <th style=\"text-align: center\">watched_repo_owner</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">wk-bot</td>\n",
    "      <td style=\"text-align: center\">apache-configs</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "        <td style=\"text-align: center\">tim448352</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">apache-configs</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "986cca34",
   "metadata": {},
   "source": [
    "### Another 4NF Example"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "270ecc35",
   "metadata": {},
   "source": [
    "Consider this schema, dependencies, and sample instance.\n",
    "\n",
    "$repo\\_role\\_user(repo\\_name, owner\\_name, role\\_id, branch, privilege, user)$\n",
    "\n",
    "$\\{repo\\_name, owner\\_name, user\\} \\rightarrow role\\_id$\n",
    "\n",
    "$role\\_id \\rightarrow \\{repo\\_name, owner\\_name\\}$\n",
    "\n",
    "$role\\_id \\twoheadrightarrow \\{branch, privilege\\}$\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "      <th style=\"text-align: center\">role_id</th>\n",
    "      <th style=\"text-align: center\">branch</th>\n",
    "      <th style=\"text-align: center\">privilege</th>\n",
    "      <th style=\"text-align: center\">user</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">join-diagram-wip</td>\n",
    "      <td style=\"text-align: center\">push</td>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">theodore-experiments</td>\n",
    "      <td style=\"text-align: center\">forcepush</td>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "      <td style=\"text-align: center\">377</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "      <td style=\"text-align: center\">trunk</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "      <td style=\"text-align: center\">trunk</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "      <td style=\"text-align: center\">eugenia</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75f2cc0a",
   "metadata": {},
   "source": [
    "We first decompose to the following BCNF schemas.\n",
    "\n",
    "$user\\_role(user, role\\_id, branch, privilege)$\n",
    "\n",
    "$role\\_repo(role\\_id, repo\\_name, owner\\_name)$\n",
    "\n",
    "Then we further decompose to 4NF schemas.\n",
    "\n",
    "$role\\_repo(role\\_id, repo\\_name, owner\\_name)$\n",
    "\n",
    "$role(role\\_id, branch, privilege)$\n",
    "\n",
    "$user\\_role'(user, role\\_id)$\n",
    "\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">role_id</th>\n",
    "      <th style=\"text-align: center\">branch</th>\n",
    "      <th style=\"text-align: center\">privilege</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">join-diagram-wip</td>\n",
    "      <td style=\"text-align: center\">push</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">theodore-experiments</td>\n",
    "      <td style=\"text-align: center\">forcepush</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">377</td>\n",
    "      <td style=\"text-align: center\">magister</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "      <td style=\"text-align: center\">trunk</td>\n",
    "      <td style=\"text-align: center\">pull</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "\n",
    "<div style=\"display: flex\">\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">role_id</th>\n",
    "      <th style=\"text-align: center\">repo_name</th>\n",
    "      <th style=\"text-align: center\">owner_name</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">377</td>\n",
    "      <td style=\"text-align: center\">db-lecture-materials</td>\n",
    "      <td style=\"text-align: center\">wkosior</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "      <td style=\"text-align: center\">guix</td>\n",
    "      <td style=\"text-align: center\">tim448352</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "<table>\n",
    "  <thead>\n",
    "    <tr>\n",
    "      <th style=\"text-align: center\">user</th>\n",
    "      <th style=\"text-align: center\">role_id</th>\n",
    "    </tr>\n",
    "  </thead>\n",
    "  <tbody>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">theodore</td>\n",
    "      <td style=\"text-align: center\">342</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">377</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">pancratius</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "      <td style=\"text-align: center\">eugenia</td>\n",
    "      <td style=\"text-align: center\">392</td>\n",
    "    </tr>\n",
    "  </tbody>\n",
    "</table>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32bf37df",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "These are relation instances that correspond to the initial instance."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd282530",
   "metadata": {},
   "source": [
    "### Notes on Database Design"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "104e6a5a",
   "metadata": {},
   "source": [
    "_notes_\n",
    "\n",
    "Other normal forms exist that are not covered here.\n",
    "\n",
    "One can design a database by\n",
    "\n",
    "- listing all needed relation attributes and relevant dependencies and normalizing the design starting with a single big relation,\n",
    "- creating an (E)ER model and mapping it to relational model, or\n",
    "- designing the desired relations ad-hoc and (possibly) later testing if they are in 3NF / 4NF.\n",
    "\n",
    "We have to choose between avoiding data redundancy (BCNF, 4NF) and ensuring dependency preservation (3NF).\n",
    "\n",
    "There is no simple, straightforward way to enforce functional dependencies with SQL constraints.  BCNF (4NF) is therefore often preferred to 3NF.\n",
    "\n",
    "We are likely to obtain a 4NF database schema if we come from an (E)ER model.\n",
    "\n",
    "Although redundancy is often undesirable, some forms of redundancy can be consciously included in the design for practical reasons.  The same is the case with multivalued and composite attributes."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "/gnu/store/q35bxk1i5blx0rcgxphhrq9xqmgha9bz-python-3.11.11/bin/python3",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}