It is important to know the difference between primary keys and foreign keys when working with relational databases. The two are probably the most fundamental elements of any database; they not only help define the structure but ensure that data integrity is well maintained.
The primary key is a unique record in a table, and a foreign key links data between tables in a database. Both of these are important for data integrity and relational structure.
Let’s dive deeper into the differences between the primary key vs foreign key.
Let’s begin.
Also Read: 6 Types of Keys in DBMS
What is Primary Key and Foreign Key?
Before learning about the difference between primary key vs foreign key, let’s know first what they mean.
What is the Primary Key?
A primary key is a unique attribute or field inside a database table that uniquely defines each record or row contained within that table. Often, it is the defining name for every single piece of data stored within the table, meaning no two records will have an identical identifier. It makes it relatively easy to locate and access specific pieces of data for manipulation within the table.
A primary key is significant because it has several uses: maintaining data integrity and enforcing integrity constraints, record lookups can be done efficiently, establishing links between different tables within a database, and providing a reliable method of ensuring uniqueness for records. You will find just how important having a primary key in your database is in the business analytics course.
Examples of Primary key
Let’s understand the difference between primary key and foreign key with the help of examples:
Employee ID |
First Name |
Last Name |
Department |
001 |
John |
Smith |
Sales |
002 |
Emily |
Johnson |
HR |
003 |
Michael |
Williams |
Marketing |
004 |
Sarah |
Davis |
Operations |
In this example, the “Employee ID” is the primary key for the “Employees” table, ensuring each employee has a unique ID. This key uniquely identifies each record, allowing easy access to specific employee data.
Most primary keys in DBMS are numerical, although they can also be mostly text or a combination. Learn about the difference between primary key and foreign key.
What is Foreign Key?
A foreign key links one table to other tables by reference to another table’s primary key. This linking enables both tables to share and swap their data. The general purpose of foreign keys is to enforce referential integrity for consistency in the data of the various tables involved. This refers to ensuring that values in a foreign key column of one table also appear within a primary key column of another table.
Examples of Foreign key
Let’s discuss the difference between primary key and foreign key with examples:
Two tables, “Students” and “Courses,” show a foreign key relationship. The “Students” table holds student data, while the “Courses” table contains course information. We’ll connect these tables with foreign keys and discuss the difference between primary key and foreign key.
Table: Students
Student ID |
First Name |
Last Name |
Age |
Grade |
001 |
John |
Smith |
18 |
A |
002 |
Emily |
Johnson |
19 |
B |
003 |
Michael |
Williams |
20 |
A |
Table: Courses
Course ID |
Course Name |
Instructor |
101 |
Mathematics |
Prof. Anderson |
102 |
English |
Prof. Thompson |
103 |
Science |
Prof. Parker |
To establish a relationship between these tables, we can introduce a foreign key in the “Students” table that references the primary key of the “Courses” table. Let’s add a column called “Course ID” as a foreign key in the “Students” table.
Table: Students
Student ID |
First Name |
Last Name |
Age |
Grade |
Course ID |
001 |
John |
Smith |
18 |
A |
101 |
002 |
Emily |
Johnson |
19 |
B |
102 |
003 |
Michael |
Williams |
20 |
A |
103 |
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Major Difference between Primary Key and Foreign Key
Let’s deep dive and understand the major difference between Primary Key vs Foreign Key in detail:
Basis |
Primary Key |
Foreign Key |
Definition |
A primary key is a unique identifier for each record in a table. |
A foreign key establishes a relationship between tables by referencing the primary key of another table. |
Basic |
Ensures uniqueness and data integrity within a single table. |
Establishes relationships and maintains referential integrity between tables. |
NULL |
Does not allow NULL values in the primary key field. |
Allows NULL values in the foreign key field, indicating a missing or unspecified relationship. |
Count |
Only one primary key can exist per table. |
Multiple foreign keys can exist within a table, depending on the relationships with other tables. |
Duplication |
No duplicate primary key values are allowed within the table. |
It can contain duplicate foreign key values, reflecting multiple records associated with the same reference. |
Indexing |
Primary keys are automatically indexed to enhance data retrieval speed. |
Foreign keys can be indexed but are not automatically indexed. |
Deletion |
Deleting a record with a primary key constraint can affect referential integrity in related tables. |
Deleting a record with a foreign key constraint can be restricted or cascaded to maintain integrity. |
Insertion |
Each new record must have a unique primary key value assigned. |
The foreign key can reference an existing primary key value or be NULL if the relationship is optional. |
Temporary table |
Primary keys can be applied to temporary tables. |
Foreign keys can also be applied to temporary tables to establish relationships. |
Relationship |
Primary keys define the basis for establishing relationships with other tables. |
Foreign keys establish relationships and connect data between related tables. |
This table summarises the major differences between foreign key vs primary key. It highlights their definitions, basic functionalities, NULL values treatment, duplicate handling, indexing, impact on deletion and insertion operations, applicability to temporary tables, and their roles in establishing relationships between tables.
Here are some additional differences between the foreign key vs primary key, presented in bullet point form:
Primary Key
- A primary key is defined within a single table and uniquely identifies each record in that table.
- Its purpose is to ensure data integrity, enforce uniqueness, and provide a reliable means of identifying individual records.
- The primary key value cannot be null or duplicated within the table.
- Primary keys are often used to establish relationships with other tables through foreign keys.
Foreign Key
- A foreign key is a way to create connections between tables by using the primary key of one table as a reference. It’s a dependency or association between two tables, and it maintains referential integrity.
- Null values can be accepted by foreign keys, meaning that relationships can be optional or unspecified.
- They can be duplicated within a table, reflecting multiple records associated with the same reference.
Also Read: Constraints in SQL
Primary Key Vs. Foreign Key: Uses
Here is some difference between primary key and foreign key in terms of uses:
Primary Key Uses
- The primary key uniquely identifies every record in a table.
- It ensures the integrity of the data because uniqueness and non-null values are required, which is important in the application of data structures.
- Primary keys ensure that the integrity and reliability of data in a table are well maintained.
- They enable accurate data retrieval and manipulation within a single table.
- Primary keys are the foundation of creating relationships with other tables through foreign keys.
Foreign Key Uses
- Foreign keys relate tables.
- They enforce data integrity and accuracy.
- Foreign keys enforce referential integrity by matching values to valid primary keys in the referenced table.
The Bottom Line
This guide helps explain the differences and similarities between primary vs foreign keys. The primary and the foreign keys are significant concepts in the world of relational database management systems. They help in offering a key or unique identification number to every row in a table and help in building relationships between multiple tables. Any kind of database heavily depends on both of these kinds of keys that are required to store data properly structured and maintain their integrity.
Understanding the differences between foreign key vs primary key helps manage databases and keep data both accurate and secure.
FAQs
Primary keys identify records uniquely within a table. Foreign keys identify relationships between tables.
The choice of a primary or foreign key depends on the specific requirement and purpose of a database.
- Primary keys are used within a single table to identify records and ensure data integrity uniquely.
- Foreign keys establish relationships between tables, connect data, and maintain referential integrity.
Primary keys and foreign keys also share some similarities, like:
- Both primary and foreign keys are key factors in the processes used to establish relationships between tables in a relational database.
- They help to keep data integrity by ensuring that the data is always accurate and consistent in every related table.
There is difference between primary key and foreign key with examples:
Examples of primary keys:
- Student ID in a table of students
- Order Number in a table of orders
- Employee ID in a table of employees
Examples of foreign keys:
- Customer ID in a table of orders, referencing the primary key of the customer table
- Department ID in a table of employees, referencing the primary key of the department table
- Book ID in a table of sales, referencing the primary key of the book table
Updated on January 6, 2025