Skip to content

Commit 9952c81

Browse files
expand primary key and entity integrity
1 parent c99a8c7 commit 9952c81

File tree

4 files changed

+1801
-151
lines changed

4 files changed

+1801
-151
lines changed

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,72 @@ A reliable system for identifying objects in the real world must be established
2929

3030
This requires setting up a disciplined process outside the database.
3131

32+
## The Three Questions of Entity Integrity
33+
34+
When designing a database system, you must be able to answer three fundamental questions about entity integrity:
35+
36+
1. **How do I prevent duplicate records?** - Ensure that the same real-world entity cannot be represented by multiple database records.
37+
38+
2. **How do I prevent entities sharing the same record?** - Ensure that different real-world entities cannot be represented by the same database record.
39+
40+
3. **How do I match entities?** - When a real-world entity comes to you, how do you find its corresponding record in the database?
41+
42+
### Example: Laboratory Mice Database
43+
44+
Consider a neuroscience laboratory that needs to track mice used in experiments:
45+
46+
**Question 1: How do I prevent duplicate records?**
47+
- Each mouse gets a unique ear tag number when it arrives at the lab
48+
- The database enforces that no two mice can have the same ear tag number
49+
- Before inserting a new mouse record, the system checks if that ear tag already exists
50+
51+
**Question 2: How do I prevent entities sharing the same record?**
52+
- Each ear tag number can only be assigned to one mouse
53+
- If a mouse dies and the ear tag is reused, the old record must be properly archived or marked as inactive
54+
55+
**Question 3: How do I match entities?**
56+
- When a researcher brings a mouse to the lab, they can look up the mouse by its ear tag number
57+
- The database can quickly find the mouse's record using the ear tag as the primary key
58+
- All related experiment records can be linked to this mouse through the ear tag
59+
60+
## Entity Integrity in Practice
61+
62+
The three questions of entity integrity must be answered not just in theory, but in practice through your database design and business processes.
63+
64+
### Example: University Student Database
65+
66+
**Question 1: How do I prevent duplicate records?**
67+
- Each student gets a unique student ID number when they enroll
68+
- The database enforces that no two students can have the same student ID
69+
- Before inserting a new student record, the system checks if that student ID already exists
70+
71+
**Question 2: How do I prevent entities sharing the same record?**
72+
- Each student ID can only be assigned to one person
73+
- If a student graduates and the ID is reused years later, the old record must be properly archived
74+
75+
**Question 3: How do I match entities?**
76+
- When a student comes to the registrar's office, they can look up their record by student ID
77+
- The database can quickly find the student's record using the student ID as the primary key
78+
- All related records (grades, courses, payments) can be linked to this student through the student ID
79+
80+
### Example: Laboratory Animal Database
81+
82+
**Question 1: How do I prevent duplicate records?**
83+
- Each animal gets a unique ear tag or microchip ID when it arrives at the lab
84+
- The database enforces that no two animals can have the same ID
85+
- Before inserting a new animal record, the system checks if that ID already exists
86+
87+
**Question 2: How do I prevent entities sharing the same record?**
88+
- Each ID can only be assigned to one animal
89+
- If an animal dies and the ID is reused, the old record must be properly archived or marked as inactive
90+
91+
**Question 3: How do I match entities?**
92+
- When a researcher brings an animal to the lab, they can look up the animal by its ID
93+
- The database can quickly find the animal's record using the ID as the primary key
94+
- All related experiment records can be linked to this animal through the ID
95+
96+
If you can answer these three questions clearly for your domain, then you have designed for entity integrity.
97+
3298
For example, establishing the Social Security system in the United States required a reliable identification of workers by all employers to report their income across their entire careers.
3399
For this purpose, in 1936, the Federal Government established a new process to ensure that each US worker would be assigned a unique number, the Social Security Number (SSN).
34100
The SSN would be assigned at birth or at entering the country for employment and no person would be allowed to have two such numbers.

0 commit comments

Comments
 (0)