What is a composite primary key and when is it used?

SQL
Medium
93.7K views

This question tests understanding of identifying records using multiple columns. It evaluates design decisions for relationships.

Why Interviewers Ask This

Candidates must demonstrate they can handle complex entity relationships. Interviewers look for the ability to identify scenarios where a single column cannot uniquely identify a row. This shows maturity in database schema design.

How to Answer This Question

Define a composite primary key as using two or more columns together. Explain that it is necessary when no single column provides uniqueness. Give an example like a junction table linking orders and products.

Key Points to Cover

  • Multiple columns for uniqueness
  • Used when single key fails
  • Common in many-to-many relationships
  • Ensures row distinctness

Sample Answer

A composite primary key uses two or more columns together to uniquely identify each row when one column alone isn't sufficient. For example, in a table linking students to courses, neither student ID nor course ID is unique alone, but the combination is. This ensures every enrollment record is distinct.

Common Mistakes to Avoid

  • Thinking it's just multiple foreign keys
  • Not providing a concrete example
  • Ignoring the impact on indexing

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 49 SQL questions