-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1464 lines (1435 loc) · 43.8 KB
/
script.js
File metadata and controls
1464 lines (1435 loc) · 43.8 KB
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
const escapeHtml = (value = "") =>
String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
const highlightTokens = {
open: "__HL_OPEN__",
close: "__HL_CLOSE__",
};
const applyHighlight = (value = "", className = "text-hl") => {
const withTokens = String(value)
.replaceAll("[[", highlightTokens.open)
.replaceAll("]]", highlightTokens.close);
const escaped = escapeHtml(withTokens);
return escaped
.replaceAll(highlightTokens.open, `<span class="${className}">`)
.replaceAll(highlightTokens.close, "</span>");
};
const syntaxHighlightSql = (htmlText) => {
const keywordList = [
"SELECT",
"FROM",
"WHERE",
"JOIN",
"LEFT",
"RIGHT",
"INNER",
"OUTER",
"ON",
"GROUP",
"BY",
"ORDER",
"INSERT",
"INTO",
"VALUES",
"UPDATE",
"SET",
"DELETE",
"CREATE",
"TABLE",
"PRIMARY",
"KEY",
"FOREIGN",
"REFERENCES",
"NOT",
"NULL",
"UNIQUE",
"DEFAULT",
"CASE",
"WHEN",
"THEN",
"ELSE",
"END",
"AS",
"AND",
"OR",
"DISTINCT",
"LIKE",
"IN",
"COUNT",
"SUM",
"AVG",
];
const typeList = [
"INT",
"INTEGER",
"VARCHAR",
"DECIMAL",
"DATE",
"TEXT",
"AUTO_INCREMENT",
];
const keywordRegex = new RegExp(`\\b(${keywordList.join("|")})\\b`, "gi");
const typeRegex = new RegExp(`\\b(${typeList.join("|")})\\b`, "gi");
const numberRegex = /\b\d+(?:\.\d+)?\b/g;
const stringRegex = /'[^']*'/g;
const commentRegex = /--.*$/gm;
const highlightSegment = (segment) => {
const tokens = [];
const protect = (regex, className) => {
segment = segment.replace(regex, (match) => {
const id = tokens.length;
tokens.push({ className, value: match });
return `__TOK_${id}__`;
});
};
protect(commentRegex, "token-comment");
protect(stringRegex, "token-string");
protect(numberRegex, "token-number");
segment = segment.replace(typeRegex, '<span class="token-type">$1</span>');
segment = segment.replace(
keywordRegex,
'<span class="token-keyword">$1</span>',
);
segment = segment.replace(/__TOK_(\d+)__/g, (_, id) => {
const token = tokens[Number(id)];
return `<span class="${token.className}">${token.value}</span>`;
});
return segment;
};
return htmlText
.split(/(<[^>]+>)/g)
.map((part) => (part.startsWith("<") ? part : highlightSegment(part)))
.join("");
};
const codeBlock = (title, lines) => {
const withComment = [`-- ${title}`, ...lines];
const raw = withComment.join("\n");
const withTokens = raw
.replaceAll("[[", highlightTokens.open)
.replaceAll("]]", highlightTokens.close);
const escaped = escapeHtml(withTokens)
.replaceAll(highlightTokens.open, '<span class="code-hl">')
.replaceAll(highlightTokens.close, "</span>");
const highlighted = syntaxHighlightSql(escaped);
return `
<div class="content-block">
<p class="mini-title">${escapeHtml(title)}</p>
<pre class="code-block"><code>${highlighted}</code></pre>
</div>
`;
};
const tableHtml = (headers, rows) => `
<div class="table-wrap">
<table>
<thead>
<tr>${headers.map((header) => `<th>${escapeHtml(header)}</th>`).join("")}</tr>
</thead>
<tbody>
${rows
.map(
(row) => `
<tr>${row.map((cell) => `<td>${applyHighlight(cell)}</td>`).join("")}</tr>
`,
)
.join("")}
</tbody>
</table>
</div>
`;
const imageCard = (src, title, caption) => `
<figure class="image-card">
<img src="${escapeHtml(src)}" alt="${escapeHtml(title)}" />
<figcaption>
<p class="image-title">${escapeHtml(title)}</p>
<p class="image-caption">${escapeHtml(caption)}</p>
</figcaption>
</figure>
`;
const bulletList = (items) => `
<ul class="bullet-list">
${items.map((item) => `<li>${item}</li>`).join("")}
</ul>
`;
const slides = [
{
section: "Intro",
content: `
<div class="slide__grid slide__grid--center text-center">
<div style="display: flex; flex-direction: column; align-items: center; margin-top: 4.5rem;">
<img src="UniversityLogo.png" alt="University Icon" style="width: 25rem; margin-bottom: 1.5rem; color: var(--muted);"></img>
<h1 style="font-size: 4.8rem; margin-top: 1rem;">Bookstore POS</h1>
<p class="lede" style="font-size: 2rem; color: var(--text); margin-bottom: 1.5rem;">
Normalization and operational SQL walkthrough
</p>
<div style="color: var(--muted);">
<i class="fa-solid fa-database" style="font-size: 3rem; margin: 0 1.2rem;"></i>
<i class="fa-solid fa-server" style="font-size: 3rem; margin: 0 1.2rem;"></i>
<i class="fa-solid fa-cash-register" style="font-size: 3rem; margin: 0 1.2rem;"></i>
</div>
</div>
</div>
`,
},
{
section: "Intro",
content: `
<div class="slide__grid slide__grid--center text-center">
<div style="margin-top: 4.5rem;">
<h1 style="font-size: 3.8rem;">Group Members</h1>
<ul style="list-style: none; padding: 0; font-size: 2rem; margin-top: 2.5rem; line-height: 2.4;">
<li><strong>M.Haseeb</strong> (24014119-204)</li>
<li><strong>Ali Husnain</strong> (24014119-100)</li>
<li><strong>Mussab</strong> (24014119-131)</li>
</ul>
<p class="lede" style="margin-top: 1.5rem;">Database Systems - 4th Semester</p>
</div>
</div>
`,
},
{
section: "Intro",
title: "Agenda and learning goals",
content: `
<p class="lede">We cover the problem, the normalization process, and the operational SQL used by a POS bookstore.</p>
${bulletList([
"Unnormalized schema and the anomalies it creates",
"Normalization path from 1NF to 3NF",
"Complete 10-table 3NF schema with keys",
"Operational queries: purchase, sales, inventory, and reporting",
"ER diagram relationships and integrity rules",
])}
`,
media: `
<div class="panel">
<p class="tag"><i class="fa-solid fa-layer-group"></i> Detailed technical walkthrough</p>
<p style="margin: 1rem 0 0;">50 slides, code-first, with actual query results.</p>
<div class="stat-grid" style="margin-top: 1.5rem;">
<div class="stat-card">
<h4>10 normalized tables</h4>
<p>3NF with PK/FK constraints</p>
</div>
<div class="stat-card">
<h4>2 unnormalized tables</h4>
<p>Redundancy and anomalies</p>
</div>
</div>
</div>
`,
},
{
section: "Intro",
title: "Bookstore POS scope",
content: `
<p class="lede">The system models inventory, purchases from suppliers, sales to customers, and analytics.</p>
${bulletList([
"Purchasing workflow: suppliers, purchase orders, and line items",
"Sales workflow: customers, sale orders, and line items",
"Inventory control: stock, pricing, and reorder indicators",
"Reporting layer: revenue, profit, and customer insights",
])}
`,
media: `
${imageCard(
"Result/SaleOrder.png",
"POS transaction snapshot",
"Sample sale order output used to validate the normalized schema.",
)}
`,
},
{
section: "Intro",
title: "Core entities and events",
content: `
<p class="lede">Each entity has a single responsibility so every fact lives in exactly one place.</p>
${bulletList([
"Publishers, Authors, and Books define the catalog",
"Customers and Suppliers represent external parties",
"Orders capture events; line items capture quantities and prices",
"Inventory status is derived from purchases and sales",
])}
`,
media: `
${tableHtml(
["Entity", "Purpose", "Key Example"],
[
["Books", "Catalog items with price and stock", "ISBN + title"],
["Purchase Orders", "Inbound inventory", "Supplier + date"],
["Sale Orders", "Customer purchases", "Customer + total"],
["Line Items", "Quantities per order", "Book + quantity"],
],
)}
`,
},
{
section: "Intro",
title: "End-to-end data flow",
content: `
<p class="lede">The same book moves through purchase, stock, and sales workflows.</p>
${bulletList([
"Suppliers deliver stock via Purchase_Orders",
"Stock increases through Purchase_Order_Items",
"Customers place Sale_Orders",
"Stock decreases through Sale_Order_Items",
"Reports aggregate data for management decisions",
])}
`,
media: `
${imageCard(
"Result/OrderPlaced.png",
"Order lifecycle evidence",
"Each query output validates the flow between catalog, orders, and inventory.",
)}
`,
},
{
section: "Unnormalized DB",
content: `
<div class="slide__grid slide__grid--center text-center un-title-slide">
<div style="margin-top: 8rem;">
<i class="fa-solid fa-triangle-exclamation" style="font-size: 6rem; color: #d00000; margin-bottom: 2rem;"></i>
<h1>Unnormalized Database</h1>
<p class="lede" style="font-size: 2rem;">Flat structure leading to redundancy, NULLs, and anomalies</p>
</div>
</div>
`,
},
{
section: "Unnormalized DB",
title: "Books table with repeated publisher + author",
content: `
<p class="lede">Publisher and author facts are stored inside every book row.</p>
${codeBlock("Books (Unnormalized)", [
"CREATE TABLE Books (",
" book_id INT,",
" [[book_title VARCHAR(200)]],",
" [[book_isbn VARCHAR(20)]],",
" book_price DECIMAL(10,2),",
" book_category VARCHAR(100),",
" stock_quantity INT,",
" [[publisher_name VARCHAR(150)]],",
" publisher_address VARCHAR(250),",
" publisher_phone VARCHAR(20),",
" publisher_email VARCHAR(100),",
" [[author_name VARCHAR(150)]],",
" author_email VARCHAR(100),",
" author_phone VARCHAR(20),",
" author_nationality VARCHAR(100)",
");",
])}
`,
media: `
${tableHtml(
["book_title", "publisher_name", "author_name"],
[
["Clean Code", "[[Pearson Education]]", "[[Robert C. Martin]]"],
[
"The Pragmatic Programmer",
"[[Pearson Education]]",
"[[David Thomas]]",
],
["Design Patterns", "[[Pearson Education]]", "[[Erich Gamma]]"],
],
)}
<p style="margin-top: 1.6rem; color: #b00020; font-weight: 700;">
<i class="fa-solid fa-circle-xmark"></i> Same publisher and author repeated in every row.
</p>
`,
},
{
section: "Unnormalized DB",
title: "Orders table mixing purchase + sales",
content: `
<p class="lede">Single table stores both purchase and sale orders, producing heavy NULL columns.</p>
${codeBlock("Orders (Unnormalized)", [
"CREATE TABLE Orders (",
" order_id INT,",
" order_date DATE,",
" order_type VARCHAR(10),",
" [[customer_name VARCHAR(150)]],",
" [[customer_phone VARCHAR(20)]],",
" [[supplier_name VARCHAR(150)]],",
" [[supplier_phone VARCHAR(20)]],",
" book_title VARCHAR(200),",
" quantity INT,",
" unit_price DECIMAL(10,2),",
" total_amount DECIMAL(10,2),",
" payment_status VARCHAR(20)",
");",
])}
`,
media: `
<div class="panel">
<h3>Why it fails</h3>
${bulletList([
"Every order row has unused columns",
"Customer data repeats on each sale",
"Supplier data repeats on each purchase",
"Difficult to enforce valid relationships",
])}
</div>
`,
},
{
section: "Unnormalized DB",
title: "Orders data with NULL columns",
content: `
<p class="lede">Purchase orders and sale orders occupy the same structure.</p>
${tableHtml(
[
"order_id",
"order_type",
"customer_name",
"supplier_name",
"book_title",
"total_amount",
],
[
[
"1001",
"PURCHASE",
"NULL",
"[[BookMart Distributors]]",
"Clean Code",
"8000.00",
],
["2001", "SALE", "[[Ahmed Khan]]", "NULL", "Clean Code", "1198.00"],
["2002", "SALE", "[[Sara Ali]]", "NULL", "Atomic Habits", "1350.00"],
],
)}
`,
media: `
<div class="panel">
<h3>Immediate impact</h3>
${bulletList([
"Harder to query by party type",
"Enforces no unique customer/supplier records",
"Mixed semantics hurt reporting accuracy",
])}
</div>
`,
},
{
section: "Unnormalized DB",
title: "Update anomaly",
content: `
<p class="lede">Changing a publisher name requires updating every row that stores it.</p>
${codeBlock("Update publisher name", [
"UPDATE Books",
"SET publisher_name = 'Pearson Education (UK)'",
"WHERE publisher_name = 'Pearson Education';",
"-- Miss one row and data becomes inconsistent",
])}
`,
media: `
<div class="panel">
<h3>Why it matters</h3>
${bulletList([
"Same fact stored in many rows",
"High risk of partial updates",
"Reports show conflicting publisher names",
])}
</div>
`,
},
{
section: "Unnormalized DB",
title: "Insert anomaly",
content: `
<p class="lede">You cannot add a new publisher without inventing a book row.</p>
${codeBlock("Insert publisher without a book?", [
"INSERT INTO Books (",
" publisher_name, publisher_address",
") VALUES ('New Publisher', 'City, Country');",
"-- Fails because book fields are required",
])}
`,
media: `
<div class="panel">
<h3>Symptoms</h3>
${bulletList([
"Missing publishers until they publish a book",
"Creates fake data to satisfy constraints",
"Catalog planning becomes inaccurate",
])}
</div>
`,
},
{
section: "Unnormalized DB",
title: "Delete anomaly",
content: `
<p class="lede">Deleting the last book of a publisher removes the publisher itself.</p>
${codeBlock("Delete last book", [
"DELETE FROM Books",
"WHERE book_title = 'Design Patterns';",
"-- Publisher data disappears if it existed only there",
])}
`,
media: `
<div class="panel">
<h3>Result</h3>
${bulletList([
"Publisher record lost with book deletion",
"No way to track historical relationships",
"Weak audit trail",
])}
</div>
`,
},
{
section: "Unnormalized DB",
title: "Redundancy and NULL summary",
content: `
<p class="lede">A few rows contain dozens of repeated attributes and empty columns.</p>
<div class="stat-grid">
<div class="stat-card">
<h4>Publisher fields</h4>
<p>Repeated in every book row</p>
</div>
<div class="stat-card">
<h4>Customer/Supplier</h4>
<p>NULL half the time</p>
</div>
<div class="stat-card">
<h4>Update cost</h4>
<p>Multiple rows per fact</p>
</div>
<div class="stat-card">
<h4>Query complexity</h4>
<p>Mixed semantics in one table</p>
</div>
</div>
`,
media: `
<div class="panel">
<h3>Normalization goals</h3>
${bulletList([
"Store each fact once",
"Separate entity types",
"Use foreign keys for relationships",
"Prevent NULL-heavy tables",
])}
</div>
`,
},
{
section: "Unnormalized DB",
title: "Unnormalized ER view",
content: `
<p class="lede">Only two massive tables represent the entire business.</p>
${bulletList([
"Books table merges book, author, and publisher",
"Orders table merges customers, suppliers, and line items",
"No explicit relationships or keys",
"High data duplication across rows",
])}
`,
media: `
${imageCard(
"ER-Diagram/Unormalized.svg",
"Flat schema diagram",
"Two tables attempt to represent many distinct entities.",
)}
`,
},
{
section: "Normalization",
title: "Normalization roadmap",
content: `
<p class="lede">We split the data in steps to reach 3NF while preserving meaning.</p>
${bulletList([
"1NF: Ensure atomic values and remove repeating groups",
"2NF: Separate partial dependencies in composite keys",
"3NF: Remove transitive dependencies between non-keys",
])}
`,
media: `
<div class="panel">
<h3>Outcome</h3>
${bulletList([
"10 focused tables",
"Clean PK/FK relationships",
"Flexible queries and reporting",
])}
</div>
`,
},
{
section: "Normalization",
title: "1NF: atomic fields",
content: `
<p class="lede">Every column stores a single, indivisible value.</p>
${codeBlock("Example cleanup", [
"-- Each column stores one value",
"book_title VARCHAR(200)",
"author_name VARCHAR(150)",
"publisher_name VARCHAR(150)",
"-- No multi-value lists in a single field",
])}
`,
media: `
<div class="panel">
<h3>Checklist</h3>
${bulletList([
"No comma-separated values",
"Consistent data types",
"Each row is unique",
])}
</div>
`,
},
{
section: "Normalization",
title: "2NF: split partial dependencies",
content: `
<p class="lede">Separate data that depends only on part of a key.</p>
${codeBlock("Split publisher out", [
"CREATE TABLE Publishers (",
" publisher_id INT PRIMARY KEY,",
" [[publisher_name VARCHAR(150)]],",
" country VARCHAR(100)",
");",
"-- Books now reference publisher_id",
])}
`,
media: `
<div class="panel">
<h3>Result</h3>
${bulletList([
"Publisher data stored once",
"Books table shrinks",
"Updates happen in one place",
])}
</div>
`,
},
{
section: "Normalization",
title: "3NF: remove transitive dependencies",
content: `
<p class="lede">Non-key fields should depend only on the primary key.</p>
${codeBlock("Split orders by type", [
"CREATE TABLE Sale_Orders (",
" so_id INT PRIMARY KEY,",
" customer_id INT,",
" total_amount DECIMAL(12,2)",
");",
"CREATE TABLE Purchase_Orders (",
" po_id INT PRIMARY KEY,",
" supplier_id INT,",
" total_amount DECIMAL(12,2)",
");",
])}
`,
media: `
<div class="panel">
<h3>Impact</h3>
${bulletList([
"No mixed customer/supplier columns",
"Clear domain for each order type",
"Consistent constraints",
])}
</div>
`,
},
{
section: "Normalized DB",
content: `
<div class="slide__grid slide__grid--center text-center norm-title-slide">
<div style="margin-top: 8rem;">
<i class="fa-solid fa-check-circle" style="font-size: 6rem; color: #008800; margin-bottom: 2rem;"></i>
<h1>Normalized Database</h1>
<p class="lede" style="font-size: 2rem;">Structured into 3NF eliminating anomalies</p>
</div>
</div>
`,
},
{
section: "Normalized DB",
title: "10-table schema overview",
content: `
<p class="lede">Each table has a single responsibility, linked by foreign keys.</p>
${tableHtml(
["Domain", "Tables"],
[
["Catalog", "Publishers, Authors, Books, Book_Authors"],
["Customers", "Customers"],
["Suppliers", "Suppliers"],
["Purchasing", "Purchase_Orders, Purchase_Order_Items"],
["Sales", "Sale_Orders, Sale_Order_Items"],
],
)}
`,
media: `
${imageCard(
"ER-Diagram/Normalized.svg",
"Normalized schema diagram",
"Ten tables connected with explicit PK/FK relationships.",
)}
`,
},
{
section: "Normalized DB",
title: "Publishers table",
content: `
<p class="lede">Dedicated table storing Publisher information exactly once.</p>
${codeBlock("Table format", [
"CREATE TABLE Publishers (",
" [[publisher_id INT PRIMARY KEY AUTO_INCREMENT]],",
" publisher_name VARCHAR(150) NOT NULL,",
" address VARCHAR(250),",
" phone VARCHAR(20),",
" email VARCHAR(100) UNIQUE,",
" country VARCHAR(100)",
");",
])}
`,
media: `
${tableHtml(
["publisher_id", "publisher_name", "country"],
[
["1", "Pearson Education", "UK"],
["2", "Penguin Random House", "USA"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Authors table",
content: `
<p class="lede">Authors live in their own table for a clean many-to-many link.</p>
${codeBlock("Table format", [
"CREATE TABLE Authors (",
" [[author_id INT PRIMARY KEY AUTO_INCREMENT]],",
" author_name VARCHAR(150) NOT NULL,",
" email VARCHAR(100) UNIQUE,",
" phone VARCHAR(20),",
" nationality VARCHAR(100)",
");",
])}
`,
media: `
${tableHtml(
["author_id", "author_name", "nationality"],
[
["1", "Robert C. Martin", "American"],
["4", "James Clear", "American"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Books table",
content: `
<p class="lede">Books reference Publishers and store pricing + stock.</p>
${codeBlock("Table format", [
"CREATE TABLE Books (",
" [[book_id INT PRIMARY KEY AUTO_INCREMENT]],",
" isbn VARCHAR(20) UNIQUE,",
" title VARCHAR(200) NOT NULL,",
" category VARCHAR(100),",
" price DECIMAL(10,2) NOT NULL,",
" stock_quantity INT NOT NULL DEFAULT 0,",
" [[publisher_id INT]],",
" FOREIGN KEY (publisher_id)",
" REFERENCES Publishers(publisher_id)",
");",
])}
`,
media: `
${tableHtml(
["book_id", "title", "publisher_id", "stock_quantity"],
[
["1", "Clean Code", "1", "50"],
["4", "Atomic Habits", "2", "80"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Book_Authors junction table",
content: `
<p class="lede">Many-to-many relationships are resolved with a junction table.</p>
${codeBlock("Table format", [
"CREATE TABLE Book_Authors (",
" [[book_id INT]],",
" [[author_id INT]],",
" PRIMARY KEY (book_id, author_id),",
" FOREIGN KEY (book_id) REFERENCES Books(book_id),",
" FOREIGN KEY (author_id) REFERENCES Authors(author_id)",
");",
])}
`,
media: `
${tableHtml(
["book_id", "author_id", "meaning"],
[
["1", "1", "Clean Code -> Robert C. Martin"],
["4", "4", "Atomic Habits -> James Clear"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Customers table",
content: `
<p class="lede">Customers are stored once and referenced by sale orders.</p>
${codeBlock("Table format", [
"CREATE TABLE Customers (",
" [[customer_id INT PRIMARY KEY AUTO_INCREMENT]],",
" full_name VARCHAR(150) NOT NULL,",
" phone VARCHAR(20),",
" email VARCHAR(100) UNIQUE,",
" address VARCHAR(250)",
");",
])}
`,
media: `
${tableHtml(
["customer_id", "full_name", "phone"],
[
["1", "Ahmed Khan", "+92-300-1111111"],
["2", "Sara Ali", "+92-321-2222222"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Suppliers table",
content: `
<p class="lede">Suppliers are separated for purchase workflows and analytics.</p>
${codeBlock("Table format", [
"CREATE TABLE Suppliers (",
" [[supplier_id INT PRIMARY KEY AUTO_INCREMENT]],",
" supplier_name VARCHAR(150) NOT NULL,",
" phone VARCHAR(20),",
" email VARCHAR(100) UNIQUE,",
" address VARCHAR(250),",
" city VARCHAR(100)",
");",
])}
`,
media: `
${tableHtml(
["supplier_id", "supplier_name", "city"],
[
["1", "BookMart Distributors", "Karachi"],
["3", "Global Books Supply", "Islamabad"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Purchase_Orders header",
content: `
<p class="lede">Header table captures supplier, date, and payment status.</p>
${codeBlock("Table format", [
"CREATE TABLE Purchase_Orders (",
" [[po_id INT PRIMARY KEY AUTO_INCREMENT]],",
" po_date DATE NOT NULL,",
" [[supplier_id INT NOT NULL]],",
" total_amount DECIMAL(12,2) NOT NULL,",
" payment_status VARCHAR(20) NOT NULL,",
" FOREIGN KEY (supplier_id) REFERENCES Suppliers(supplier_id)",
");",
])}
`,
media: `
${tableHtml(
["po_id", "po_date", "supplier_id", "payment_status"],
[
["1", "2024-01-05", "1", "PAID"],
["3", "2024-02-01", "1", "PENDING"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Purchase_Order_Items line items",
content: `
<p class="lede">Each line item links one book with quantity and price.</p>
${codeBlock("Table format", [
"CREATE TABLE Purchase_Order_Items (",
" [[po_item_id INT PRIMARY KEY AUTO_INCREMENT]],",
" [[po_id INT NOT NULL]],",
" [[book_id INT NOT NULL]],",
" quantity INT NOT NULL,",
" unit_price DECIMAL(10,2) NOT NULL,",
" FOREIGN KEY (po_id) REFERENCES Purchase_Orders(po_id),",
" FOREIGN KEY (book_id) REFERENCES Books(book_id)",
");",
])}
`,
media: `
${tableHtml(
["po_id", "book_id", "quantity", "unit_price"],
[
["1", "1", "20", "400.00"],
["2", "4", "30", "300.00"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Sale_Orders header",
content: `
<p class="lede">Customer orders are separated from purchase orders.</p>
${codeBlock("Table format", [
"CREATE TABLE Sale_Orders (",
" [[so_id INT PRIMARY KEY AUTO_INCREMENT]],",
" so_date DATE NOT NULL,",
" [[customer_id INT NOT NULL]],",
" total_amount DECIMAL(12,2) NOT NULL,",
" payment_status VARCHAR(20) NOT NULL,",
" FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)",
");",
])}
`,
media: `
${tableHtml(
["so_id", "so_date", "customer_id", "payment_status"],
[
["1", "2024-01-08", "1", "PAID"],
["4", "2024-02-05", "3", "PENDING"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Sale_Order_Items line items",
content: `
<p class="lede">Each sale line item links a book to a sale order.</p>
${codeBlock("Table format", [
"CREATE TABLE Sale_Order_Items (",
" [[so_item_id INT PRIMARY KEY AUTO_INCREMENT]],",
" [[so_id INT NOT NULL]],",
" [[book_id INT NOT NULL]],",
" quantity INT NOT NULL,",
" unit_price DECIMAL(10,2) NOT NULL,",
" FOREIGN KEY (so_id) REFERENCES Sale_Orders(so_id),",
" FOREIGN KEY (book_id) REFERENCES Books(book_id)",
");",
])}
`,
media: `
${tableHtml(
["so_id", "book_id", "quantity", "unit_price"],
[
["1", "1", "2", "599.00"],
["2", "4", "3", "450.00"],
],
)}
`,
},
{
section: "Normalized DB",
title: "Key constraints overview",
content: `
<p class="lede">Constraints enforce integrity between every table.</p>
${codeBlock("Example constraints", [
"Books.publisher_id -> Publishers.publisher_id",
"Book_Authors.book_id -> Books.book_id",
"Book_Authors.author_id -> Authors.author_id",
"Purchase_Orders.supplier_id -> Suppliers.supplier_id",
"Sale_Orders.customer_id -> Customers.customer_id",
])}
`,
media: `
<div class="panel">
<h3>Integrity rules</h3>
${bulletList([
"No orphaned line items",
"Unique emails for publishers/customers",
"Accurate joins for reporting",
"Enforced data consistency",
])}
</div>
`,
},
{
section: "ER Diagram",
title: "Normalized ER diagram",
content: `
<p class="lede">Relationships are explicit and readable with PK/FK links.</p>
${bulletList([
"Books link to Publishers via publisher_id",
"Book_Authors maps the many-to-many relationship",
"Orders link to Customers or Suppliers",
"Line items connect orders to books",
])}
`,
media: `
${imageCard(
"ER-Diagram/Normalized.svg",
"Entity relationships",
"Clear joins enable accurate analytics and consistent updates.",
)}
`,
},
{
section: "ER Diagram",
title: "Relationship rules",
content: `