CMD Guide
HomeDatabasesRelational Model

Keys in Relational Databases

One idea, layered: every key is a superkey with a constraint added

"Key" is overloaded, but the terms nest cleanly. Build them up:

Superkey contains candidate keys contains the chosen primary plus alternate keys, with a worked attribute-closure example
Superkey contains candidate keys contains the chosen primary plus alternate keys, with a worked attribute-closure example

Finding candidate keys mechanically — attribute closure

You don't eyeball candidate keys; you derive them from the functional dependencies. The tool is the closure X⁺: the set of all attributes determined by X. If X⁺ = every attribute, X is a superkey; if no proper subset of X is also a superkey, X is a candidate key.

Take R(StudentID, Email, CourseID, Grade) with FDs: StudentID→Email, Email→StudentID (email is unique per student), {StudentID, CourseID}→Grade.

{StudentID, CourseID}⁺
   start: {StudentID, CourseID}
   + StudentID→Email           → add Email
   + {StudentID,CourseID}→Grade → add Grade
   = {StudentID, Email, CourseID, Grade}  = ALL attributes  → superkey
   drop StudentID or CourseID → closure no longer covers all  → MINIMAL
   → {StudentID, CourseID} is a CANDIDATE KEY

Because Email→StudentID too, {Email, CourseID} has the same closure → it's a second candidate key. But {StudentID, Email, CourseID} is only a superkey: Email is redundant (StudentID already implies it), so it isn't minimal and isn't a candidate key. Pick one candidate as primary (say {StudentID, CourseID}); the other is an alternate key you still declare UNIQUE.

Why it matters beyond vocabulary

Takeaways


Deepened for this guide (the prior version only listed definitions). Closure algorithm per Silberschatz, "Database System Concepts". See also: Functional Dependency & Closure, Normalization, Indexing & Storage.

🤖 Don't fully get this? Learn it with Claude

Stuck on Keys in Relational Databases? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Keys in Relational Databases** (Databases) and want to truly understand it. Explain Keys in Relational Databases from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Keys in Relational Databases** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Keys in Relational Databases** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Keys in Relational Databases** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes