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 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);

Recommended Readings
- Advanced SQL Queries For Practice With Solution
- SQL Queries For Practice With Solution
- SQL Interview Questions and Answers
- STORED PROCEDURE in SQL Server
- How To Join Tables Data in SQL Server
- How to use Transaction in SQL Stored Procedure
- Difference Between IN and NOT IN Operators in SQL
- How To Modify Date in SQL Using DATEADD
- How To Get Year From Date in SQL Server
- How To Get Month From Date in SQL Server
- How To Get Day From Date in SQL Server
- How To Use ROW_NUMBER Function in SQL
- Date and Time Functions in SQL Server
- How To Find Nth Highest Salary in SQL Server
- How to Backup Table Using SELECT INTO Statement
- How To Use HAVING Clause in SQL Server
- Aggregate Functions in SQL Server
- How To Group Data Using Group By in SQL Server
- How To Truncate Table in SQL Server
- How To Delete Data From Table in SQL Server
- How To Update Table Data in SQL Server
- How To Sort Data Using Order By Clause in SQL
- How To Select Distinct Records in SQL Server
- How to Filter Data From Table in SQL Server
- Round Off Values Using Ceiling and Floor in SQL
- How To Find Square Root Of A Number in SQL Server
- How To Select Data From Table in SQL Server
- How To Insert Data in SQL Server Table
- How To Add NOT NULL Constraint in SQL Server
- How To Add Check Constraint on SQL Server Table
- How To Add Default Constraint on SQL Server Table
- Unique Key Constraint in SQL Server
- How to add Foreign Key Constraint in SQL Server
- How To Add Identity To SQL Server Table Column
- How to add Primary Key Constraint in SQL Server
- How To Create Alter and Drop Table in SQL Server
- How To Create Alter and Drop Database in SQL