You are currently viewing How to add Foreign Key Constraint in SQL Server

How to add Foreign Key Constraint in SQL Server

How to add Foreign Key Constraint in SQL Server

A foreign Key is used to enforce the Referential Integrity between tables. A Foreign Key in one table is the Primary Key in another table. It does not allow to destroy the relationship between tables while inserting values. The table with the Primary Key is called the Parent table, and the table with the Foreign key is called the Child table.

The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the parent table.

Foreign Key Constraint on Create Table

First, we will create a parent table tblDepartment with a Primary Key column DeptID, then we create tblEmployee with a foreign key column EmpDeptID column and reference it as a Foreign Key.

CREATE TABLE tblDepartment
(

DeptID INT PRIMARY KEY NOT NULL,
DeptName NVARCHAR(50) NULL

)

GO

CREATE TABLE tblEmployee
(

EmpID INT PRIMARY KEY NOT NULL,
EmpFirstName NVARCHAR(20) NULL,
EmpLastName NVARCHAR(20) NULL,
EmpEmail NVARCHAR(50) NULL,
EmpPhone NVARCHAR(15) NULL,
EmpDeptID INT FOREIGN KEY REFERENCES tblDepartment(DeptID),
EmpSalary INT NULL,
EmpJoiningDate DATETIME NULL

)

After creating the above tables, Primary Key and Foreign Key relationship between tables will be created. We can see the above relationship in the following image.

Foreign Key Constraint on Alter Table

We can add Foreign Key constraint on already created table as below.

ALTER TABLE tblEmployee
ADD FOREIGN KEY(EmpDeptID) REFERENCES tblDepartment(DeptID)
    

As we have seen above, how to create Foreign Key constraint at the time of table creation or using ALTER TABLE command on already created table.

If we look the Foreign Key constraint in the Object Explorer, the name of the Foreign Key is shown with some random characters at the end. SQL Server created this name automatically for us as shown in below image.

Foreign key constraint with random name

Foreign Key Constraint with Custom Name

To give a sensible name, we can add Foreign Key constraint at the time of table creation as well as after table creation with ALTER command.

CREATE TABLE tblDepartment
(

DeptID INT PRIMARY KEY NOT NULL,
DeptName NVARCHAR(50) NULL

)

GO

CREATE TABLE tblEmployee
(

EmpID INT PRIMARY KEY NOT NULL,
EmpFirstName NVARCHAR(20) NULL,
EmpLastName NVARCHAR(20) NULL,
EmpEmail NVARCHAR(50) NULL,
EmpPhone NVARCHAR(15) NULL,
EmpDeptID INT CONTRAINT FK_tblEmployee_Department FOREIGN KEY REFERENCES tblDepartment(DeptID),
EmpSalary INT NULL,
EmpJoiningDate DATETIME NULL

)

Now, if we add the Foreign Key at the time of table creation with some sensible name FK_tblEmployee_Department will be created as shown in the below image.

We can also give sensible name after the table is created using ALTER commands.

ALTER TABLE tblEmployee
ADD CONSTRAINT FK_tblEmployee_Department
FOREIGN KEY (EmpDeptID) REFERENCES tblDepartment(DeptID);
    
Foreign Key Constraint with name assigned

Recommended Readings

Buy Best Stationery Products in Pakistan

Piano Ballpoint Pen

Piano Ballpoint Pen

ORO Trimetal Pencil

ORO Trimetal

Dollar Clipper Pen

UHU 10×7 ml Pack of 10 PCs

UHU 10×7 ml Pack of 10 PCs

Glue Stick 8g Pack Of 12 PCs

Glue Stick 8g

Color Pencils Box of 12 PCs

Deer Multi Color Pencils