SQL Tutorial 3 min read

Keys in SQL – Primary Key, Foreign Key, Unique Key & Composite Key Explained (With Examples & Practice)

In my early days of SQL interview prep, I used to mix up Primary Key, Foreign Key, and Unique Key all the time. Interviewers love asking about keys because they test your understanding of data integrity, relationships, and database design — core concepts for any role involving databases.

Today, we're clearing all the confusion. I'll explain the four main types of keys in SQL:

  • Primary Key
  • Foreign Key
  • Unique Key
  • Composite Key

We'll use simple examples from our familiar e-commerce database (users, products, orders) that you can practice on live at sqlpractice.in.

Let's get started!

Why Do We Need Keys?

Keys are special constraints that help maintain:

  • Data Integrity (no duplicates, valid relationships)
  • Uniqueness (each row identifiable)
  • Relationships between tables
  • Performance (indexes created automatically)

Without proper keys, your database becomes messy, slow, and error-prone.

1. Primary Key (PK)

Definition: A column (or set of columns) that uniquely identifies every row in a table.

Rules:

  • Must be UNIQUE
  • Cannot be NULL
  • Only one per table

Most common choice: Auto-incrementing ID (SERIAL or BIGSERIAL)

Alternative: Natural key (e.g., email if guaranteed unique)

Why use surrogate key (auto-increment ID)?

  • Stable (doesn't change)
  • Fast for joins
  • Easier when data changes (e.g., user changes email)

Practice Question 1 (Easy) Identify which column should be the Primary Key in a products table. Practice here →

2. Foreign Key (FK)

Definition: A column that creates a relationship with another table's Primary Key.

Purpose: Enforces referential integrity — ensures valid relationships.

Common options:

  • ON DELETE CASCADE: Delete orders if user is deleted
  • ON DELETE SET NULL: Set user_id to NULL if user deleted
  • ON DELETE RESTRICT: Prevent deletion if orders exist (safest)

Practice Question 2 (Medium) What happens if you try to insert an order with non-existent user_id? Practice here →

3. Unique Key

Definition: Ensures all values in a column are unique, but allows NULL (unlike Primary Key).

Use cases:

  • Email, phone number, username (should be unique but might be optional)
  • Business rules (e.g., only one active coupon per user)

Key Difference from Primary Key:

  • Primary Key: Cannot be NULL + only one per table
  • Unique Key: Can be NULL + multiple per table

Practice Question 3 (Easy) Add a Unique constraint to prevent duplicate product names. Practice here →

4. Composite Key (Compound Key)

Definition: A Primary Key made of multiple columns together.

When to use:

  • No single column is naturally unique
  • Common in junction tables (Many-to-Many)

This ensures:

  • Same product can't be added twice to same order
  • Combination is unique

Practice Question 4 (Medium) Design a table for student-course enrollment using Composite Key. Practice here →

Common Interview Questions on Keys

  1. "Difference between Primary Key and Unique Key?"
  2. "Can a table have multiple Primary Keys?"
  3. "What is a Composite Key? Give example."
  4. "What happens without Foreign Keys?"
  5. "Should email be Primary Key or Unique Key?"

Practice Question 5–8 (Interview Level) Mix of all key types with real scenarios. Full keys practice pack →

Final Thoughts

Understanding keys transformed my SQL interviews. I went from fearing multi-table questions to confidently designing schemas.

Remember:

  • Use Primary Key for unique identification (prefer surrogate ID)
  • Use Foreign Key for relationships
  • Use Unique Key for business uniqueness
  • Use Composite Key when natural combination is unique

Frequently Asked Questions

Is SQL Practice completely free to use?

Yes — SQL Practice is 100% free with no sign-up required. Our free online SQL compiler lets you write, run, and test SQL queries instantly in your browser. No installation needed.

What SQL dialect does the online compiler support?

Our online SQL compiler runs SQLite for most practice sets, supporting SELECT, JOINs, GROUP BY, subqueries, CTEs, and window functions. The Hospital schema also supports live PostgreSQL query execution.

Are the SQL questions suitable for TCS, Infosys and Deloitte interviews?

Yes — our SQL interview questions 2026 are curated from real hiring rounds at TCS, Infosys, Deloitte, and more. Questions range from Easy (basic SELECT) to Hard (window functions, CTEs, correlated subqueries).

Can I practice SQL on different databases and schemas?

Yes! The SQL Playground lets you switch between Hospital, E-Commerce, Employees, and more schemas to practice SQL queries online against real-world data models.

How does the auto-grader check my SQL query?

The built-in SQL auto-grader runs your query and compares the output against the expected result set. You instantly see a correct or incorrect verdict — plus the reference solution to learn the correct SQL syntax.