Back to community
Computer ScienceClass 12 (Maharashtra HSC) Resolved

What is the difference between a primary key and a foreign key in DBMS?

In our database chapter both keys are about identifying records and I keep swapping their definitions in tests. Can someone explain with a simple example?

Asked by Rohan Verma 50 45d ago
Need a Computer Science tutor? Browse verified Computer Science tutors near you.

1 answer

Accepted answer
6

A primary key uniquely identifies each row within its own table, and it cannot be null or duplicated. For example, in a Students table, RollNo can be the primary key because every student has a unique roll number. A foreign key is a column in one table that refers to the primary key of another table, creating a link between them. For example, a Marks table might have a RollNo column that is a foreign key pointing to RollNo in the Students table, so each mark record is tied to a real student. The foreign key enforces referential integrity, meaning you cannot enter a RollNo in Marks that does not exist in Students. In short, a primary key identifies records within a table, while a foreign key connects two tables together.

M
Meera Joshi
335 pts· 45d ago

Sign in as a tutor to answer this doubt.