SQL Foreign Key

SQL Server Foreign Key constraint is used to implement the relationship between tables. To normalize the data, we split the tables using this relation. While splitting the data, we need a reference, and we call it a SQL Server foreign key relationship.

The list of points you should remember before going to the example of the SQL Server Foreign Key.

  • You can link a SQL Server Foreign key column with a Primary constraint or a Unique Constraint column in another column.
  • Values you enter in the constraint column should match with the referenced column. Otherwise, it will throw an error.
  • You can define the SQL Server foreign key relationship with tables in the same database and server. You can use Triggers to provide cross-referential integrity.
  • It allows you to define the relationship with another column in the same table. This type of relationship is a self-reference.
  • You can’t delete a table that has this relationship

For example, the Employee table has Employee ID, and the Orders table has Orders information and Emp ID. Remember, this Emp ID relates to the Employee ID column in the Employee. For this demonstration, we are using the Department.

Create SQL Server Foreign Key

We can create a SQL Server Foreign key using the Transact query and the Management Studio.

Create using transact query

How to create a SQL Server Foreign Key using Transact statement? Here, we will create this constraint connecting with the tblDepartment to create a table.

CREATE TABLE [dbo].[tblEmployee](
	[EmpID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[FirstName] [nvarchar](255) NULL,
	[LastName] [nvarchar](255) NULL,
	[Education] [nvarchar](255) NULL,
	[YearlyIncome] [float] NULL,
	[Sales] [float] NULL,
	[DeptID] [int] NULL,
	CONSTRAINT [FK_tblDepartment_tblEmployee_DeptID] FOREIGN KEY([DeptID])
		REFERENCES [dbo].[tblDepartment] ([ID])
) 
GO

We added the CONSTRAINT statement at the end of the table creation. Refer to Create Table, Primary, Unique Constraint, Triggers, and referential integrity in SQL Server.

Let me use the SP_HELP procedure to check the table definition and the constraints.

SQL Foreign Key 3

See our newly created one. You can also use Object Explorer to achieve the same. To do so, Expand the tblEMployee, and expand the Keys folder.

view SQL foreign Key in Object Explorer 4

Create on the Existing table

This example shows you the steps to add Foreign Key in SQL Server on an existing table. For this, we are going to create a new table.

CREATE TABLE [dbo].[tblEmployee](
	[EmpID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
	[FirstName] [nvarchar](255) NULL,
	[LastName] [nvarchar](255) NULL,
	[Education] [nvarchar](255) NULL,
	[YearlyIncome] [float] NULL,
	[Sales] [float] NULL,
	[DeptID] [int] NULL
) 
GO
Messages
--------
Command(s) completed successfully.

Now let me add the constraint. Here, we used the Alter Table to alter the content and used the ADD Constraint statement to add the constraint.

ALTER TABLE [dbo].[tblEmployee]  
ADD CONSTRAINT [FK_tblDepartment_tblEmployee_DeptID] FOREIGN KEY([DeptID])
			REFERENCES [dbo].[tblDepartment] ([ID])
Messages
--------
Command(s) completed successfully.

Delete SQL Foreign Key using Query

Remember, You can’t alter the foreign constraint using the query. To modify the existing one, you must drop (or delete) and recreate it. Please use the SQL Server DROP Constraint and ALTER TABLE statements to delete or drop the Foreign key.

ALTER TABLE [dbo].[tblEmployee]  
DROP CONSTRAINT [FK_tblDepartment_tblEmployee_DeptID]

In this example, we will show you how to create using the Management Studio. To do so, please go to Object Explorer. Next, please Expand the Database folder, and select the table which you want to create. Next, Right-click on it will open the context menu. Please choose the Design option.

Table Design option

It will open the corresponding table in design mode. Right-click on the DeptID column and select the Relationships option.

Assign Relationships to Columns

As you can see, there are no existing relationships. Please click on the Add button to add a new relationship.

Add Button to Select the Relationship

Once you select the Add option, SSMS will create it for you.

SQL Foreign Key 14

Next, click the Browse button beside the Tables and Columns Specification option. This property is used to specify the relation between the two.

Choose the Table and Columns Specific Options

We selected the Primary as the tblDepartment and Column as ID.

SQL Primary Table Column and Foreign Key table Column

Next, we selected the tblEmployee, and column as the Dept ID

Tables and Columns Relationship

Modify SQL Foreign Key using Management Studio

To modify in Management Studio, go to the Database folder, select the table, and then go to the Keys folder. Next, Right-click on the name and select the Modify option.

Modify the Sql Foreign Key

Clicking the Modify option will open the corresponding table in design mode, along with the Relationships window.

Relationships

Delete SQL Foreign Key Constraints using Management Studio

Expand the Database folder, choose the table your constraint holds, then go to the Keys folder. Next, Right-click on the name and select the Delete option

Delete SQL Foreign Key Constraint in Object Explorer

Once you select the Delete option, the Delete Object window will open. Click OK to delete the constraint.

Delete Object Window

You can see that there is no Foreign Key in the Object explorer

Open Object Explorer to see the Constraints

To delete a SQL Server foreign key, Expand the Database folder within the object explorer. Right-click on the table name, and click the Design option. Next, select the Relationships option to open this window. Now, Please select it, and click on the Delete button.

Delete the SQL Server Foreign Key
Categories SQL