Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Difference between Primary Key and Foreign Key
Next article icon

Difference between Primary Key and Foreign Key

Last Updated : 27 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Keys are one of the most important elements in a relational database to maintain the relationship between the tables and it also helps in uniquely identifying the data from a table. The primary key is a key that helps uniquely identify the tuple of the database. In contrast, the Foreign Key is a key used to determine the relationship between the tables through the primary key of one table that is the primary key of one table acts as a foreign key to another table. Now, let's discuss both of them in some detail.

What is Primary Key?

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

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

Table STUDENT:

STUD_NO STUD_NAME STUD_PHONE STUD_STATE STUD_COUNT STUD_AGE
1 RAM 9865278251 Haryana India 20
2 RAM 9655470231 Punjab India 19
3 SUJIT 7514290359 Rajasthan India 18
4 SURESH 8564103258 Punjab India 21

Table STUDENT_COURSE:

STUD_NO COURSE_NO COURSE_NAME
1 C1 DBMS
2 C2 Computer Networks
1 C2 Computer Networks

What is Foreign Key?

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.

Example: STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation.

Difference Between Primary Key and Foreign Key

PRIMARY KEY FOREIGN KEY
A 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.
It 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.
Only one primary key is allowed in a table. Whereas more than one foreign key is allowed in a table.
It is a combination of UNIQUE and Not Null constraints. It can contain duplicate values and a table in a relational database.
It does not allow NULL values . It can also contain NULL values.
Its value cannot be deleted from the parent table. Its value can be deleted from the child table.
It constraint can be implicitly defined on the temporary tables. It constraint cannot be defined on the local or global temporary tables.

Conclusion

In this article, we have mentioned the primary key and foreign key, and the differences between them. Both the keys, whether the primary key or the foreign key, play an important role in the Database management system. Primary Key contains unique values, whereas Foreign Key contains values taking reference from Primary Keys. The main characteristic property of the Primary key is that it can't be repeated, it is unique. There are some differences between their functions, Primary Keys determine a row in the table and Foreign Key determines the relation between tables.


Next Article
Difference between Primary Key and Foreign Key

S

SHUBHAMSINGH10
Improve
Article Tags :
  • DBMS
  • Difference Between
  • GATE CS

Similar Reads

    Difference between Primary key and Super key
    Key concepts that play a significant role in DBMS are the Primary Key and the Super Key. While both are fundamental to the structure and functionality of a database, they serve distinct purposes and have different characteristics. A Primary Key is a specific, minimal set of attributes that uniquely
    4 min read
    Difference between Primary key and Unique key
    Keys play a crucial role in relational databases by ensuring data integrity and efficient access to records. Keys help to identify individual rows in a table prevent from data being duplicated and also enable reliable relationships between tables. There are several types of Keys - Primary Key, Candi
    5 min read
    Difference between Primary and Candidate Key
    In relational database management systems(RDBMS) both the Primary Key and Candidate Key are the essential components and are used to uniquely identify records (tuples) within a table. They both are fundamental concepts used to ensure data integrity and prevent duplication of data. These(Primary key
    6 min read
    Difference between Primary and Secondary Memory
    Primary memory is used to store actively used data and is very fast but loses information when power is lost. Secondary memory provides long-term storage for files and programs, retaining data safely but is slower than primary memory. In this article, we are going to discuss the difference between p
    5 min read
    Difference between Super Key and Candidate Key
    Tables in databases need ways to identify each record uniquely. Super keys and candidate keys help with this. They're special columns or groups of columns that can tell records apart. Both super keys and candidate keys can have empty spaces (null values). These keys also help connect different table
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences