A primary key is used to ensure data in the specific column is unique. It is 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: Refer the figure –
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).
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: Refer the figure –
STUD_NO in STUDENT_COURSE is a foreign key to STUD_NO in STUDENT relation.
Let’s see the difference between Primary Key and Foreign Key:
|S.NO.||PRIMARY KEY||FOREIGN KEY|
|1||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.|
|2||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.|
|3||Only one primary key is allowed in a table.||Whereas more than one foreign key are allowed in a table.|
|4||It is a combination of UNIQUE and Not Null constraints.||It can contain duplicate values and a table in a relational database.|
|5||It does not allow NULL values.||It can also contain NULL values.|
|6||Its value cannot be deleted from the parent table.||Its value can be deleted from the child table.|
|7||It constraint can be implicitly defined on the temporary tables.||It constraint cannot be defined on the local or global temporary tables.|
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Types of Keys in Relational Model (Candidate, Super, Primary, Alternate and Foreign)
- Difference between Primary key and Unique key
- Difference between Primary and Candidate Key
- Difference between Primary key and Super key
- Difference between Primary and Secondary Memory
- Primary key in MS SQL Server
- Primary Indexing in Databases
- Difference between RAM and ROM
- Difference between SQL and T-SQL
- Difference between DFD and ERD
- Difference between ADO and ADO.NET
- Difference Between SEO and SEM
- Difference between DDR 5 and DDR 3
- What is the difference between GUI and CUI?
- Difference between LAN and MAN
- Difference Between Blu-Ray and DVD
- Difference Between Gi-Fi and Li-Fi
- Difference between TCP and RTP
- Difference between JSP and ASP
- Difference between PNG and GIF
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.