LeetCampus
Interview Question

Can you explain the difference between a primary key and a foreign key in database design?

Short Answer

A primary key uniquely identifies each record in a table, ensuring no two rows share the same key value. A foreign key is a field in one table that refers to the primary key in another table, establishing a link between them. This distinction is crucial for maintaining data integrity and defining relationships within a database.

What a Strong Answer Covers

  • Primary key uniquely identifies records.
  • Foreign key links to a primary key.
  • Primary keys ensure data uniqueness.
  • Foreign keys establish table relationships.
  • Both maintain data integrity.

Sample Answers

Example 1: College Project - Database Design for a Library System

In my final year project, I worked on a library management system where we designed a database with a 'Books' table and a 'Borrowers' table. The primary key for the 'Books' table was the 'BookID', which uniquely identified each book in the library. This ensured that no two books could have the same ID, providing accurate tracking of inventory. The 'Borrowers' table had a foreign key 'BookID' that referenced the 'Books' table, linking each borrower's transaction to the specific book they borrowed. This design helped us manage the relationship between books and borrowers effectively.

Example 2: Volunteer Experience - Organizing Events

During my time volunteering for a community center, I was involved in managing events. We created a simple database to track participants and events. The 'Participants' table had a primary key called 'ParticipantID', which uniquely identified each volunteer. For our 'Events' table, we included a foreign key 'ParticipantID' to show which participants were involved in which events. This structure allowed us to create reports that showed how many volunteers participated in each event and helped us understand engagement levels better.

Example 3: First Job Experience - Data Entry Role

In my first job as a data entry assistant, I worked with a customer database where the 'CustomerID' served as the primary key. Each customer had a unique identifier, ensuring accurate data entry and retrieval. We also had an 'Orders' table where the 'CustomerID' was used as a foreign key. This relationship allowed us to track all orders placed by each customer seamlessly. By understanding these keys, I was able to help maintain data integrity and support better customer service.

Why Interviewers Ask This Question

** Interviewers often look for clarity in explaining the roles of primary and foreign keys, as they are essential in establishing relationships and maintaining data integrity within a database. * A primary key uniquely identifies each record in a table, ensuring that no two rows have the same key value. In contrast, a foreign key is a field (or collection of fields) in one table, that refers to the primary key in another table, creating a link between the two.

** Best practices include ensuring primary keys are unique and immutable, while foreign keys should maintain referential integrity to ensure data consistency across tables.

Keywords

primary keyforeign keydatabase designdata integrityrelational database
December 5, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions