Home MySql What is the difference between SQL Primary and Foreign key

What is the difference between SQL Primary and Foreign key


In this article I will give you details on primary and foreign key and will tell how they are differ from each other.

In SQL Server we have two keys a primary key and a foreign key which your might think that both are identical, but actually both are different in features and behaviours.

What is Primary Key?

A primary key is a particular relational table column or combination of columns designated to uniquely identify all table records.

Primary key should contain a unique value for each row of data. It cannot contain null values.

Primary key allows each row in a table to be uniquely identified and made sure that no duplicate rows exist and no null values are entered in the primary key column.

To manage relationship between two tables in the database primary key is necessary without having primary key in SQL we can not have relational database.

What is Foreign Key?

So Foreign key is basically a primary key from the another table.

We can have one or more columns in a table that refers to the primary key in another table.

So by defining foreign key in the table it specifies that the records from this table is associated with records from another table.

Key differences between primary and foreign key

Primary KeyForeign Key
Primary key is unique record in the table.Foreign key is a column in the table that is primary key in another table
Primary Key can not have null values.Foreign key will accept multiple null value.
We can only have single Primary key in a TableWe can have multiple primary keys in a table.
Primary Key can not duplicate values.Foreign key can have duplicate values
Primary key is automatically indexedWhere Foreign key does not create index by itself.

Read – How to Implement Laravel Eloquent One to One Relationship

Example of using Primary key and Foreign key

Let’s create users table, execute following query:

Now lets add new posts table so it is going to be associated with user for example user can have multiple blog posts:

Additionally add one more table just so you understand that primary key can attach to multiple tables as foreign key:

Now if you see your database relationship this how it is going to look alike:

Primary and Foreign Key EER Diagram
Primary and Foreign Key EER Diagram

That’s all about primary and foreign key if your laravel developer I would suggest to read following article on Eloquent database relationships:

Read – How to Implement Laravel Eloquent One-to-Many Relationship

Enjoy this tutorial? Get iTech Empires updates delivered straight to your inbox.

No Spam, ever. We'll never share your email address and you can opt out at any time.

Yogesh Kolihttp://www.itechempires.com
Yogesh Koli is a software engineer & Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 6+ years of experience working with the front-end and back-end web application development.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of

Tutorials You May Like!

- Advertisement -