

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Primary key and Foreign key in DBMS
In this post, we will understand the difference between Primary key and Foreign key in DBMS
Primary key
It is used to ensure that the data in the specific column is unique.
It helps uniquely identify a record in a relational database.
One primary key only is allowed in a table.
It is a combination of the ‘UNIQUE’ and ‘Not Null’ constraints.
This means it can’t be a NULL value.
Its value can’t be deleted from parent table.
The constraint can be implicitly defined for the temporary tables.
Example- In SQL:
CREATE TABLE table_name ( col_name int NOT NULL PRIMARY KEY, …. );
Foreign Key
It is a column or a group of columns in a relational database table.
It gives a link between the data in both the tables.
It is the field in a table which is analogous to primary key of other table.
More than one foreign key is allowed in a table.
It can contain duplicate values in a relational database.
It can contain NULL values.
Its value can be deleted from the child table.
The constraint can’t be defined on local or global temporary tables.
Example- in SQL:
CREATE TABLE table_name ( col_name int NOT NULL PRIMARY KEY, col_name int FOREIGN KEY REFERENCES Persons(col_name) );
- Related Questions & Answers
- Difference between Primary key and Foreign key in Database
- Difference between Primary key and Foreign key in SQL Database
- Difference between Primary Key and Candidate key
- Difference between Primary Key and Unique key
- Difference Between Primary and Candidate Key
- How to use Primary Key Constraints and Foreign Key Constraints to enforce database integrity in Oracle?
- Difference between Super Key and Candidate key
- Difference between Private Key and Public Key
- Primary key Vs Unique key
- What is the difference between MySQL PRIMARY KEY and UNIQUE constraint?
- Foreign Key in RDBMS
- How to refer primary key as Foreign to various table in MySQL?
- Explain the concept of primary key with an example (DBMS)?
- Remove Primary Key in MySQL?
- Reset Primary Key in MySQL