Difference between Primary Key and Foreign Key

Difference between Primary Key and Foreign Key

A primary key is used to ensure data in the specific column is unique. It is a column that cannot have NULL values. It is either an existing table column or a column that is specifically generated by the SQL database according to a defined sequence.

Difference between primary key and foreign key
Difference between primary key and foreign key

What is Primary key:

Example: Refer to the image above:
STUD_NO, as well as STUD_PHONE both, are candidate keys for relation STUDENT but STUD_NO can be chosen as the primary key (only one out of many candidate keys).

What is a Foriegn key:

Example: Refer to the above image
STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation.

A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It is a column (or columns) that references a column (most often the primary key) of another table.

Let’s see the difference between Primary Key and Foreign Key:

S.NO.PRIMARY KEYFOREIGN KEY
1A primary key is used to ensure data in the specific column is unique.A foreign key is a column or group of columns in a
relational database table that provides a link
between data in two tables.
2It uniquely identifies a record in the relational database table.It refers to the field in a table which is the primary key
of another table.
3Only one primary key is allowed in a table.More than one foreign key is allowed in a table.
4It is a combination of UNIQUE and Not Null constraints.It can contain duplicate values and a table in a relational database.
5It does not allow NULL values.It can also contain NULL values.
6Its value cannot be DELETE FROM the parent table.Its value can be deleted from the child table.
7It constraint can be implicitly defined on the temporary tables.Its constraint cannot be defined on the local or global temporary tables.
Difference between Primary key and foreign key

You can read about how to install SQL developer from this link.

Also, you can read about this topic more from this link.

1 thought on “Difference between Primary Key and Foreign Key”

  1. Pingback: SQL Sample Database - DataWitzz

Leave a Comment