---
title: "Primary key and foreign key"  
description: "Primary key is a key that uniquely identifies a record in a table. It cannot accepts a null value. We cannot have multiple primary key in a table. It"  
author: "Samuel Fernandes"  
published: 2017-01-20  
updated: 2018-03-17  
canonical: https://www.mindstick.com/blog/11276/primary-key-and-foreign-key  
category: "database"  
tags: ["sql"]  
reading_time: 2 minutes  

---

# Primary key and foreign key

[Primary key](https://www.mindstick.com/blog/214/primary-key) is a key that uniquely identifies a record in a table. It cannot accepts a null value. We cannot have multiple primary key in a table. **It is [clustered Index](https://www.mindstick.com/blog/337/clustered-non-clustered-indexing-in-sql-server) by default****.** Primary key contain unique value for each row of data. We cannot have [two primary](https://www.mindstick.com/forum/156008/how-do-you-set-two-primary-keys-in-access) key in a table. When we use multiple primary key in a table it is said to be composite key.

For declaring any row as unique by primary key we use Primacy key. Let us take an example to create primary key in a table

```
 [dbo].[Employee](          [Id] [int]
not null,          [S_Id] int
Primary Key,          [Name]
[varchar](50) CREATE TABLE NULL,          [Salary]
[varchar](50) NULL)  GO
```

Adding primary key when a table have been already created\

```
alter table Employee  add
primary key (Id) table_1 is the table name where we have to add primary
keyTo remove primary key alter table Employee  drop
primary key 
```

we can also add primary key by going in the [design view](https://answers.mindstick.com/qa/94814/design-view-is-not-getting-displayed-in-my-android-studio-what-should-i-do) of table and right click on the field which we have to make primary then add primary key and we can also remove primary key in the same manner .

**Foreign Key**

Foreign key is use to [connect two](https://answers.mindstick.com/qa/50257/what-would-you-use-to-connect-two-computers-without-using-switches) table .Foreign key is key that is primary key in another table. It accept Multiple [Null values](https://www.mindstick.com/interview/34072/optional-class-in-java-handling-null-values-more-effectively). A table can have multiple foreign key. It is also known as referencing key.

```
CREATE TABLE [dbo].[E_Salary](          [Id] [int]
Primary Key ,          [S_Id]
[int](50) NULL FOREIGN KEY
REFERENCES E_Salary(S_Id),//Declaring  Foreign Key          [Name]
[varchar](50) NULL,          [Salary]
[varchar](50) NULL) GO
```

Adding foreign key when table has been already created

```
ALTER TABLE E.SalaryADD FOREIGN KEY (S_Id)REFERENCES Employee(S_Id)
```

**Difference between primary key and foreign key**

**Primary key-**

- A table can have one primary key.
- It cannot accept [duplicate values](https://www.mindstick.com/forum/157764/how-to-find-duplicate-values-in-a-sql-table).
- It is default clustered index
- It cannot accept null value

**Foreign Key-**

Default is Non_Clustered Index

A table can have more than one foreign key

It can accept duplicate value.

It accept null value.

It provide link between data in two table.

## You can also visit related articles and [blogs](https://www.mindstick.com/developersection/blog) url

[Sql keys](https://www.mindstick.com/blog/371/sql-keys)

[Foreign Key (Self Reference) Constraint](https://www.mindstick.com/blog/174/foreign-key-self-reference-constraint)

## \

---

Original Source: https://www.mindstick.com/blog/11276/primary-key-and-foreign-key

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
