You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/30-schema-design/020-primary-key.md
+21-69Lines changed: 21 additions & 69 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -79,72 +79,6 @@ In addition to the primary key, a table may have **secondary unique indexes** to
79
79
80
80
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.
81
81
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
-
CREATETABLEpet_shop_data (
101
-
id INTPRIMARY 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
-
CREATETABLEpet (
121
-
pet_id INTPRIMARY 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
-
CREATETABLEemployee (
130
-
employee_id INTPRIMARY KEY,
131
-
name VARCHAR(50) NOT NULL,
132
-
position VARCHAR(30) NOT NULL,
133
-
salary DECIMAL(10,2) NOT NULL,
134
-
hire_date DATENOT NULL
135
-
);
136
-
137
-
CREATETABLEcustomer (
138
-
customer_id INTPRIMARY 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
-
148
82
## Looser Entity Integrity
149
83
150
84
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
208
142
209
143
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**.
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.
0 commit comments