Feed Genie Support Product Page
What is the difference between a primary key and a foreign key in a database?
Asked 02 Jul 2025 11:36:38
1
has this question
02 Jul 2025 11:36:38 Roy Mcmillan posted:
A primary key and a foreign key are both important concepts in relational databases, but they serve different purposes:Primary Key:
A primary key uniquely identifies each record (row) in a table.
It must be unique and cannot be NULL.
Each table can have only one primary key, which may consist of one or more columns.
Example:
In a Students table, student_id can be the primary key because it uniquely identifies each student.
Foreign Key:
A foreign key is a field (or collection of fields) in one table that refers to the primary key in another table.
It is used to create a relationship between two tables.
A foreign key can have duplicate values and can be NULL, depending on the design.
Example:
In a Courses table, if we have a column student_id that links to the Students table’s primary key, then student_id is a foreign key in the Courses table.