Primary and Foreign Keys

Share this topic

Primary and Foreign Keys

From the book I Am Datapedia!, published here free by the author.

Part of I Am Datapedia!

Primary Key is also known as Pkey, and Foreign Key is also known as Fkey. These are one of the most important terms without which a proper Data Model cannot be created.

A screen shot of a computer screen

Description automatically generated

We have understood what a table is. Now, the next step is to understand, if there is more than one table then how to join both. In the topic ‘Tables, Columns and Rows’ we used an example of table ‘Family’. Let’s use the same table here with an additional column i.e., Birth Location.

Table

Description automatically generatedTable Name: Family

Now let’s say, to save space in Family table as we can see ‘United State of America’ has been used many times so how about creating another table where let’s store lengthy country names and in Family tables, let use country code instead with there is another table called as Birth Location.

Table

Description automatically generatedTable Name: Birth Location

Now, let’s update the Family table to reflect country code instead of country name.

Table

Description automatically generatedTable Name: Family

Now, the problem is column ‘Birth Location’ have country code so how to find out which country name refer to ‘001’. Let’s declare ID in Birth_Location table as Pkey and Location_ID in Family table as Fkey.

Table

Description automatically generatedTable Name: Birth_Location

Table

Description automatically generatedTable Name: Family

Now, we can join both tables using Primary and Foreign Key columns and can extract the relevant full country names.

Express your reaction

Your Take on This Insight?

Share how this content resonates with you.

Comment
Useful0
Insightful0
Actionable0
Thought-provoking0
Inspiring0

0 reactionsSign in to react or comment

Comments (0)

No comments yet.

    Free account

    You can read every topic without one. A free account lets you react to this topic, comment under it and search the Knowledge Hub. Reacting and commenting need a confirmed e-mail address.