Skip to main content

Difference between Primary Key and Foreign Key

A “key” is one of the most basic and important concepts of the Database Management System (DBMS); the keys helps us work easily with the records and the tables in our database by providing us with the ease of accessing and retrieving the data from the table. A primary key is the first key to be understood as it uniquely identifies the particular record of any table. The primary key and foreign key however, must not be confused with each other as they have a completely different definition as well as usage. Here are the differences between the two.

Difference between Primary Key and Foreign Key

Image Source

Difference between Primary Key and Foreign Key

S.no. Primary key Foreign key
1 The primary key of a particular table is the attribute which uniquely identifies every record and does not contain any null value. The foreign key of a particular table is simply the primary key of some other table which is used as a reference key in the second table.
2 A primary key attribute in a table can never contain a null value. A foreign key attribute may have null values as well.
3 Not more than one primary key is permitted in a table. A table can have one or more than one foreign key for referential purposes.
4 Duplicity is strictly prohibited in the primary key; there cannot be any duplicate values. Duplicity is permitted in the foreign key attribute, hence duplicate values are permitted.
5 Example:

Consider the table student, which keeps record of students in a class:

Here, Id is the primary key which uniquely identifies each record in the table “student”

ID NAME Address
101 ABC x
102 DEF y
103 GHI z
104 JKL w
Now, let us consider the table teacher, which records the teachers in the school:

Here Tid or teacher id is the unique primary key, for each record in the table. This table uses the “ID” attribute which is the primary key of the “student” table as its reference key or foreign key to refer to the student the particular teacher teaches, :

TID NAME TAddress ID
1 Priya P 101
2 Riya R 102
3 Sam S 102
4 Tom T 103

Since one student is taught by more than one teachers, the ID attribute (which is the foreign key in this table), contains duplicate values.

The foreign key of one table although is derived from the primary key of the other table, holds completely different characteristics and these differences are crucial which should be taken care of to avoid any errors.

Comment below if you have queries related to primary key vs foreign key.

The post Difference between Primary Key and Foreign Key appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2019/04/difference-between-primary-key-and-foreign-key.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold ...

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and...