Skip to content

Commit 706ff55

Browse files
reorder sections on primary key, lookups, and normalization
1 parent f3a589c commit 706ff55

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

book/30-schema-design/025-primary-key.md renamed to book/30-schema-design/020-primary-key.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,27 @@ The composite primary key `(state, district)` ensures that:
229229

230230
For tracking marathon champions, you need both the **year** and the **division** (men's or women's) to uniquely identify each champion.
231231

232+
(DataJoint)
233+
```python
234+
@schema
235+
class MarathonChampion(dj.Manual):
236+
definition = """
237+
year : int
238+
division : enum('men', 'women')
239+
---
240+
name : varchar(60)
241+
country : char(2)
242+
time_in_seconds : decimal(8,3)
243+
"""
244+
```
245+
(Equivalent SQL)
232246
```sql
233247
CREATE TABLE marathon_champions (
234248
year YEAR NOT NULL,
235249
division ENUM('men', 'women') NOT NULL,
236250
name VARCHAR(60) NOT NULL,
237251
country CHAR(2) NOT NULL,
238-
time TIME NOT NULL,
252+
time_in_seconds : decimal(8,3) NOT NULL,
239253
PRIMARY KEY (year, division)
240254
);
241255
```
File renamed without changes.

book/30-schema-design/045-normalization.ipynb

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,73 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Database normalization \n",
7+
"# Database Normalization \n",
8+
"\n",
9+
"# Normalization Principle\n",
10+
"\n",
11+
"A fundamental principle in database design is **normalization**—the practice of organizing data to minimize redundancy and dependency. One key aspect of normalization is that **each table should represent one distinct entity class**.\n",
12+
"\n",
13+
"## Why Separate Entity Types?\n",
14+
"\n",
15+
"Different entity types have different:\n",
16+
"- **Identification systems**: How they are uniquely identified\n",
17+
"- **Attributes**: What properties they have\n",
18+
"- **Relationships**: How they connect to other entities\n",
19+
"- **Business rules**: What constraints apply to them\n",
20+
"\n",
21+
"## Example: Pet Shop Database\n",
22+
"\n",
23+
"Consider designing a database for a pet shop. You might be tempted to put everything in one table:\n",
24+
"\n",
25+
"```sql\n",
26+
"-- BAD DESIGN: Mixing different entity types\n",
27+
"CREATE TABLE pet_shop_data (\n",
28+
" id INT PRIMARY KEY,\n",
29+
" name VARCHAR(50),\n",
30+
" type VARCHAR(20), -- 'cat', 'dog', 'employee', 'customer'\n",
31+
" breed VARCHAR(30),\n",
32+
" salary DECIMAL(10,2),\n",
33+
" phone VARCHAR(20),\n",
34+
" address TEXT\n",
35+
");\n",
36+
"```\n",
37+
"\n",
38+
"This design violates the normalization principle because it mixes:\n",
39+
"- **Pets** (cats, dogs) with attributes like breed\n",
40+
"- **Employees** with attributes like salary\n",
41+
"- **Customers** with attributes like phone and address\n",
42+
"\n",
43+
"## Better Design: Separate Tables\n",
44+
"\n",
45+
"```sql\n",
46+
"-- GOOD DESIGN: Separate entity types\n",
47+
"CREATE TABLE pet (\n",
48+
" pet_id INT PRIMARY KEY,\n",
49+
" name VARCHAR(50) NOT NULL,\n",
50+
" species ENUM('cat', 'dog', 'bird', 'fish') NOT NULL,\n",
51+
" breed VARCHAR(30),\n",
52+
" birth_date DATE,\n",
53+
" owner_id INT\n",
54+
");\n",
55+
"\n",
56+
"CREATE TABLE employee (\n",
57+
" employee_id INT PRIMARY KEY,\n",
58+
" name VARCHAR(50) NOT NULL,\n",
59+
" position VARCHAR(30) NOT NULL,\n",
60+
" salary DECIMAL(10,2) NOT NULL,\n",
61+
" hire_date DATE NOT NULL\n",
62+
");\n",
63+
"\n",
64+
"CREATE TABLE customer (\n",
65+
" customer_id INT PRIMARY KEY,\n",
66+
" name VARCHAR(50) NOT NULL,\n",
67+
" phone VARCHAR(20),\n",
68+
" email VARCHAR(100),\n",
69+
" address TEXT\n",
70+
");\n",
71+
"```\n",
72+
"\n",
73+
"Each table now represents a distinct entity class with appropriate attributes and identification systems.\n",
874
"\n",
975
"**Database normalization** is a set of principles for designing databases with clarity and logical rigor. \n",
1076
"Normalized designs communicate the mapping between real-world entities and their representations in database design. \n",

0 commit comments

Comments
 (0)