Hello readers, in this article we will discuss foreign keys in DBMS. Basically, there are two types of keys in DBMS i.e. primary key, and foreign key. We won’t discuss the primary key but discuss the foreign key.

What is Foreign Key in DBMS (Easy Explanation) - ComputeNepal

What is a foreign key?

Talking about the definition of the foreign key, a foreign key is an attribute or set of attributes that refer to the primary key of the same table or different table. i.e. whenever we need to access data from a foreign key it will always refer to the primary key. And the data can be in the same table or a different table. One of the major points in the foreign key is that it is the key that maintains referential integrity. We aren’t going to discuss what is referential integrity in this article instead focus mainly on the foreign key. To understand the foreign key briefly let’s consider some examples. Let’s say that we have two tables as follows.
Roll No.NameAddress

Course_idCourse_nameRoll No.

Here in the first table, I have created three different columns and the first column of the first table is of Roll N. but it is special than all other columns. Here, roll no. is the primary key i.e. it cannot be null or be repeated. The main importance of the primary key is that it helps us uniquely identify the records even if other data are the same so it stands out in front of other data. So, no matter how many records we add data can be uniquely identified. Also here we have another table called “course”. In this table, we have three columns, course_id, course_name, and one common attribute’s column in both i.e. roll no. It is because these two tables are interrelated to each other. So to establish the relation at least one attribute must be common between them so that we can later join both the tables. So, we have roll no. common them. We can even make other attributes such as course_id common in both tables that are not a problem. Here, the same roll no is working as a foreign key in the second table that is taking the reference from the first table. Also to understand, we cannot put the value that’s not existing in the roll no. of table one in the second table as the second table is taking the reference from the roll no. of the first table. Also to note a point, the table second is also called the referencing table as it is taking the reference from the first table, and the first table is called the referenced table i.e. in the table where exists primary kay is called referenced table and the table where exists foreign key is called a referenced table.

Similar Posts

One Comment

  1. I really appreciate this post. I have been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *