LeetCampus
Interview Question

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

December 5, 2025
0 views
Difficulty: Medium
Popularity: Common
Share on

Question Explanation

This question is commonly asked to assess a candidate's understanding of fundamental database concepts. 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. Misunderstandings often arise around their definitions and purposes. 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. Real-world applications include tracking relationships between different data entities, like customers and orders, which is crucial for effective database management. Best practices include ensuring primary keys are unique and immutable, while foreign keys should maintain referential integrity to ensure data consistency across tables.

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.

Keywords

primary keyforeign keydatabase designdata integrityrelational database

Ready to practice more questions?

Explore our collection of technical interview questions from top companies.

View All Questions