Skip to content
engineering workbench.by StudyPort Academy
Learning path
Foundations18 min

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

Primary Keys vs Foreign Keys
AttributePrimary KeyForeign Key
RoleUniquely identifies a single row in its own tableReferences a primary key in a parent table
UniquenessMust be strictly UNIQUE across all rowsCan contain duplicate values (one-to-many)
NullabilityNOT NULL is mandatoryCan be NULL if the relationship is optional
Integrity RuleEntity integrity: identity must never be ambiguousReferential 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.

    01What does referential integrity guarantee in a relational database?
    02What happens during an INSERT if a foreign key references a non-existent parent ID (with enforcement enabled)?

    Ready to call this one understood?

    Mark this lesson complete to earn 100 XP. You can always revisit it.

    Sign in to save progress
    Next lesson