Explain the difference between a primary key and a foreign key in a database.

1 Answers
Answered by suresh

Understanding the Difference between Primary Key and Foreign Key in a Database

When it comes to database management, it is crucial to distinguish between a primary key and a foreign key. These two concepts play significant roles in ensuring the integrity and relationships within a database.

Primary Key:

A primary key is a unique identifier for each record in a table. It uniquely identifies each row and ensures data integrity by preventing duplicate or null values. A primary key is used to enforce entity integrity within a table.

Foreign Key:

A foreign key is a field in a table that refers to the primary key in another table. It establishes a relationship between two tables by referencing the primary key of another table. Foreign keys help maintain referential integrity by ensuring that data in the referencing table corresponds to data in the referenced table.

Overall, the key difference between a primary key and a foreign key lies in their roles within a database structure. While a primary key uniquely identifies each record within a table, a foreign key establishes relationships between tables by referencing the primary key of another table.

Answer for Question: Explain the difference between a primary key and a foreign key in a database.