Skip to content

Commit d2c8565

Browse files
streamlined UUID and primary key sections
1 parent 400ca32 commit d2c8565

File tree

4 files changed

+272
-550
lines changed

4 files changed

+272
-550
lines changed

book/30-database-design/012-integrity.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,19 @@ Completeness prevents missing values that could invalidate analyses:
8484
---
8585

8686
## 3. Entity Integrity
87-
**Each entity has a unique, reliable identifier.**
87+
**Each real-world entity corresponds to exactly one database record, and each database record corresponds to exactly one real-world entity.**
8888

89-
Entity integrity ensures one-to-one mapping between database records and real-world entities:
90-
- **Primary keys** uniquely identify each row
91-
- **Uniqueness constraints** prevent duplicates
92-
- **Identification strategies** (auto-increment, UUIDs, natural keys)
89+
Entity integrity ensures a **one-to-one correspondence** between real-world entities and their digital representations in the database. This is not simply about having unique identifiers—it's about establishing a reliable, bidirectional mapping where:
90+
- Each real-world entity must be represented by exactly one unique record in the database
91+
- Each database record must correspond to a single, distinct real-world entity
92+
- **Primary keys** uniquely identify each row and enable this correspondence
93+
- **Uniqueness constraints** prevent duplicate records for the same entity
94+
- **Identification strategies** (auto-increment, UUIDs, natural keys) support reliable entity matching
9395

94-
**Example:** Each mouse has exactly one unique ID.
96+
**Example:** Each mouse in the lab has exactly one unique ID, and that ID refers to exactly one mouse—never two different mice sharing the same ID, and never one mouse having multiple IDs.
9597

9698
**Covered in:**
97-
- [Primary Keys](025-primary-key.md)Identification strategies
99+
- [Primary Keys](020-primary-key.md)Entity integrity and the 1:1 correspondence guarantee (elaborated in detail)
98100
- [UUID](030-uuid.ipynb) — Universally unique identifiers
99101

100102
---
File renamed without changes.

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

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ Surrogate keys are especially useful for entities that exist only in digital for
294294

295295
## Universally Unique Identifiers (UUIDs)
296296

297-
**UUIDs** (Universally Unique Identifiers) are 128-bit identifiers that are designed to be globally unique across time and space. They are standardized by RFC 4122 and provide a reliable way to generate surrogate keys without coordination between different systems.
297+
**UUIDs** (Universally Unique Identifiers) are 128-bit identifiers that are designed to be globally unique across time and space. They are standardized by [RFC 9562](https://www.rfc-editor.org/rfc/rfc9562.html) (which obsoletes RFC 4122) and provide a reliable way to generate surrogate keys without coordination between different systems.
298298

299299
### UUID Format
300300

@@ -387,6 +387,10 @@ print(uuid3_value) # Different from UUID5 but also deterministic
387387

388388
### Practical UUID Examples
389389

390+
For detailed examples of using UUIDs in DataJoint tables, including table definitions, insertion code, and working with UUIDs in foreign key relationships, see [UUIDs in DataJoint](025-uuid.ipynb).
391+
392+
Here are some conceptual examples showing UUIDs as primary keys:
393+
390394
#### Example 1: Social Media Posts
391395
(DataJoint)
392396
```python
@@ -497,32 +501,7 @@ Different databases handle UUIDs differently:
497501
- **SQLite**: TEXT or BLOB
498502
- **SQL Server**: UNIQUEIDENTIFIER type
499503

500-
(DataJoint)
501-
```python
502-
@schema
503-
class User(dj.Manual):
504-
definition = """
505-
user_id : uuid
506-
---
507-
username : varchar(50)
508-
"""
509-
```
510-
511-
(Equivalent SQL)
512-
```sql
513-
-- MySQL
514-
CREATE TABLE user (
515-
user_id BINARY(16),
516-
username VARCHAR(50) NOT NULL,
517-
PRIMARY KEY (user_id)
518-
);
519-
520-
-- PostgreSQL
521-
CREATE TABLE user (
522-
user_id UUID NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),
523-
username VARCHAR(50) NOT NULL
524-
);
525-
```
504+
In DataJoint, UUIDs are automatically stored as `BINARY(16)` in MySQL for efficient storage. See [UUIDs in DataJoint](025-uuid.ipynb) for practical implementation examples.
526505

527506

528507
# Practical Examples of Ensuring Entity Integrity

0 commit comments

Comments
 (0)