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

1 Answers
Answered by suresh

Primary Key vs. Foreign Key in a Database

Primary Key vs. Foreign Key in a Database

In a database, a primary key and a foreign key are essential components that help establish relationships between tables. Here is the difference between the two:

Primary Key:

A primary key is a column or a set of columns that uniquely identifies each record in a table. It must have unique values and cannot contain null values. A primary key helps ensure data integrity and is often used as the basis for creating relationships with other tables.

Foreign Key:

A foreign key is a column or a set of columns that establishes a link between two tables. It typically refers to the primary key in another table, creating a relationship between the two. A foreign key helps maintain referential integrity and enforces constraints on the data, ensuring that related records are accurate and consistent.

In summary, while a primary key uniquely identifies records within a table, a foreign key establishes relationships between tables by linking to a primary key in another table.

Answer for Question: Can you explain the difference between a primary key and a foreign key in a database?