Skip to content

Commit f9fc4ed

Browse files
test tab-sets
1 parent ef582c9 commit f9fc4ed

File tree

1 file changed

+21
-69
lines changed

1 file changed

+21
-69
lines changed

book/30-schema-design/020-primary-key.md

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -79,72 +79,6 @@ In addition to the primary key, a table may have **secondary unique indexes** to
7979

8080
For example, in a `Person` table, unique indexes might be enforced on attributes like **email addresses**, **usernames**, **driver’s licenses**, **cellphone numbers**, or **Social Security numbers**. These indexes ensure that no duplicate entries exist for attributes that are intended to be unique, further supporting entity integrity.
8181

82-
# Normalization Principle
83-
84-
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**.
85-
86-
## Why Separate Entity Types?
87-
88-
Different entity types have different:
89-
- **Identification systems**: How they are uniquely identified
90-
- **Attributes**: What properties they have
91-
- **Relationships**: How they connect to other entities
92-
- **Business rules**: What constraints apply to them
93-
94-
## Example: Pet Shop Database
95-
96-
Consider designing a database for a pet shop. You might be tempted to put everything in one table:
97-
98-
```sql
99-
-- BAD DESIGN: Mixing different entity types
100-
CREATE TABLE pet_shop_data (
101-
id INT PRIMARY KEY,
102-
name VARCHAR(50),
103-
type VARCHAR(20), -- 'cat', 'dog', 'employee', 'customer'
104-
breed VARCHAR(30),
105-
salary DECIMAL(10,2),
106-
phone VARCHAR(20),
107-
address TEXT
108-
);
109-
```
110-
111-
This design violates the normalization principle because it mixes:
112-
- **Pets** (cats, dogs) with attributes like breed
113-
- **Employees** with attributes like salary
114-
- **Customers** with attributes like phone and address
115-
116-
## Better Design: Separate Tables
117-
118-
```sql
119-
-- GOOD DESIGN: Separate entity types
120-
CREATE TABLE pet (
121-
pet_id INT PRIMARY KEY,
122-
name VARCHAR(50) NOT NULL,
123-
species ENUM('cat', 'dog', 'bird', 'fish') NOT NULL,
124-
breed VARCHAR(30),
125-
birth_date DATE,
126-
owner_id INT
127-
);
128-
129-
CREATE TABLE employee (
130-
employee_id INT PRIMARY KEY,
131-
name VARCHAR(50) NOT NULL,
132-
position VARCHAR(30) NOT NULL,
133-
salary DECIMAL(10,2) NOT NULL,
134-
hire_date DATE NOT NULL
135-
);
136-
137-
CREATE TABLE customer (
138-
customer_id INT PRIMARY KEY,
139-
name VARCHAR(50) NOT NULL,
140-
phone VARCHAR(20),
141-
email VARCHAR(100),
142-
address TEXT
143-
);
144-
```
145-
146-
Each table now represents a distinct entity class with appropriate attributes and identification systems.
147-
14882
## Looser Entity Integrity
14983

15084
Depending on the business needs, **entity integrity** requirements can vary. Some businesses may allow multiple digital identities per person, while others may tolerate multiple people sharing a single digital identity. Other businesses may require a strict one-to-one match between real-world entities and their digital representations.
@@ -208,6 +142,21 @@ Sometimes, a single column cannot uniquely identify a record. In these cases, we
208142

209143
Consider tracking U.S. representatives. A single district number (like "District 1") is not sufficient because there are multiple District 1s across different states. To uniquely identify a representative, you need both the **state** and the **district number**.
210144

145+
(DataJoint)
146+
```python
147+
@schema
148+
class USRepresentative(dj.Manual):
149+
definition = """
150+
state : char(2)
151+
district : tinyint unsigned
152+
---
153+
name : varchar(60)
154+
party : char(1)
155+
phone : varchar(20)
156+
office_room : varchar(20)
157+
"""
158+
```
159+
(Equivalent SQL)
211160
```sql
212161
CREATE TABLE us_representative (
213162
state CHAR(2) NOT NULL,
@@ -229,7 +178,8 @@ The composite primary key `(state, district)` ensures that:
229178

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

232-
(DataJoint)
181+
::::{tab-set}
182+
::: {tab-item} DataJoint
233183
```python
234184
@schema
235185
class MarathonChampion(dj.Manual):
@@ -242,7 +192,8 @@ class MarathonChampion(dj.Manual):
242192
time_in_seconds : decimal(8,3)
243193
"""
244194
```
245-
(Equivalent SQL)
195+
:::
196+
::: {tab-item} SQL
246197
```sql
247198
CREATE TABLE marathon_champions (
248199
year YEAR NOT NULL,
@@ -253,7 +204,8 @@ CREATE TABLE marathon_champions (
253204
PRIMARY KEY (year, division)
254205
);
255206
```
256-
207+
:::
208+
::::
257209
This design ensures that each year has exactly one men's champion and one women's champion, preventing duplicate entries for the same year-division combination.
258210

259211
## When to Use Composite Primary Keys

0 commit comments

Comments
 (0)