The Relational Model: Tables, Keys, and Foreign Keys
Understand the idea
How primary and foreign keys stop your database from filling with orphan records.
The idea
The organizes data into clean, structured tables made of rows and columns. Instead of repeating information in messy text files, relational databases use clear relationships between tables.
A is a unique identifier (such as an ID number) that belongs to exactly one row in a table. A is a column in another table that points back to that primary key. This setup creates : the database engine refuses to save any row that points to a non-existent parent, preventing orphan records from ever entering your database.
Relational databases also protect relationships when data changes. For example, if a user account is deleted, a cascade rule can automatically clean up that user's old order records, or block the deletion until the orders are handled. Because the database engine enforces this rule, your data stays clean no matter which application or script touches it.
What changes
| Attribute | Primary Key | Foreign Key |
|---|---|---|
| Role | Uniquely identifies a single row in its own table | References a primary key in a parent table |
| Uniqueness | Must be strictly UNIQUE across all rows | Can contain duplicate values (one-to-many) |
| Nullability | NOT NULL is mandatory | Can be NULL if the relationship is optional |
| Integrity Rule | Entity integrity: identity must never be ambiguous | Referential integrity: referenced parent must exist |
The command
Enforcing foreign key constraints
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Run the example to see SQLite enforce foreign key referential integrity, creating an order tied to Alice and reading the joined customer profile.
Guided challenge
Enforce referential check
Update the function so it validates that the order's userId exists in validUserIds, rejecting orphan records.
Enforce referential check
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
true false
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.