Knowledge Guide
HomeDatabasesSQL Fundamentals

Keys in SQL

SQL Keys

In MySQL, keys are fundamental for maintaining data integrity and establishing relationships between tables. SQL keys play vital roles, contributing to an optimized database structure and enhanced performance. Let's explore these keys in detail.

Candidate Key

A candidate key in a relational database is a set of one or more columns that can uniquely identify a record in a table. It could potentially be used as a primary key.

There can be more than one candidate key but only one primary key.

Image
Image

In the employee table, the columns id and email can uniquely identify a record.

Primary Key

The primary key is the key that uniquely identifies each record in a table. It is a unique key. There may be multiple candidate keys in a relationship, from which one primary key can be selected.

It has no duplicate values, and it cannot be null.

Image
Image

Foreign Key

A foreign key is a key that establishes a link between data in two tables. The foreign key in one table refers to the primary key in another table, creating a relationship between the two tables.

Image
Image

Composite Key

A composite key is a key that consists of two or more columns in a database table. Unlike a single-column key (such as a primary key or a unique key), a composite key uses a combination of columns to identify a record in the table uniquely.

Image
Image

In the above table, (email, phone) is a composite key.

Unique Key

A unique key is a set of one or more columns that uniquely identifies each record in a table. It is similar to the primary key but can accept a null value.

More than one unique key can exist in a table, unlike the primary key, which can exist only once.

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

Stuck on Keys in SQL? 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 SQL** (Databases) and want to truly understand it. Explain Keys in SQL 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 SQL** 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 SQL** 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 SQL** 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