Delete the foreign key in SQL Server

Once you have created the FOREIGN KEY but no longer use the foreign key and want to delete it, you can use the ALTER TABLE command in SQL Server (Transact-SQL).

Syntax to delete foreign keys in SQL Server

 ALTER TABLE ten_ba ng 
DROP CONSTRAINT f k_ten;

Variable name or variable value

ten_bang

The name of the table to which the foreign key has been created.

fk_ten

The name of the foreign key you want to delete.

For example

 CREATE TABLE sanpham 
( id_sanpham INT PRIMARY KEY,
ten_sanpham VARCHAR(50) NOT NULL,
phan_loai VARCHAR(25)
);

CREATE TABLE hangtonkho
( id_hangtonkho INT PRIMARY KEY,
id_sanpham INT NOT NULL,
soluong INT,
luong_toithieu INT,
luong_toida INT,
CONSTRAINT fk_htk_id_sanpham
FOREIGN KEY (id_sanpham)
REFERENCES sanpham (id_sanpham)
);

In this example, we created the parent table, sanpham, with the primary key including the information field id_sanpham. Then there is a child table named hangtonkho with a foreign key with deletion constraint. The CREATE TABLE statement creates a foreign key on the hangtonkho table named fk_htk_id_sanpham. The foreign key forms the relationship between the id_sanpham column in the hangtonkho table and id_sanpham in the sanpham table.

  1. Activate foreign keys in SQL Server
  2. Foreign Key (Set Null) foreign key in SQL Server
  3. Foreign Key (Cascade Delete) in SQL Server

If you want to delete the foreign key fk_htk_id_sanpham, execute the command below.

 ALTER TABLE hangtonk ho 
DROP CONSTRAINT fk_ htk_id_sanpham;

The above ALTER TABLE statement will delete the constraint named fk_htk_id_sanpham in the hangtonkho table.

Last lesson: Foreign Key (Set Null) foreign key in SQL Server

The following article: Disable foreign key in SQL Server

4 ★ | 1 Vote

May be interested

  • The command bootcfg delete in WindowsThe command bootcfg delete in Windows
    the bootcfg delete command deletes the operating system entry in the [operating systems] section of the boot.ini file. the command applies to: windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012.
  • VIEW in SQL ServerVIEW in SQL Server
    the article explains how to create, update and delete view in sql server with syntax and examples.
  • PROCEDURE (Procedure) in SQL ServerPROCEDURE (Procedure) in SQL Server
    procedure is a program in the database that contains many statements that you save for later use but have different points from function. the article will give you the syntax and examples of how to create and delete procedures in sql server.
  • How to delete the database in MS SQL ServerHow to delete the database in MS SQL Server
    to delete the database in ms sql server, we use the drop command.
  • DISTINCT clause in SQL ServerDISTINCT clause in SQL Server
    the distinct clause is used to remove duplicates in the result set.
  • DROP LOGIN command in SQL ServerDROP LOGIN command in SQL Server
    the tutorial explains how to use the drop login command in sql server with syntax and examples.
  • 3 'legitimate' reasons to change DNS Server3 'legitimate' reasons to change DNS Server
    dns servers are an important component for a better web experience, but very few people understand what problems their work or security will cause. specifically, change dns to do? and how to change dns? invite you to read
  • Conditions NOT in SQL ServerConditions NOT in SQL Server
    the not condition in sql server (transact-server) is also called the not operator, which is used to negate conditions in select, insert, update, and delete statements.
  • DROP USER command in SQL ServerDROP USER command in SQL Server
    this article will show you in detail how to use the drop user command in sql server with specific syntax and examples to better visualize and capture commands.
  • How to connect to an FTP Server from Windows Explorer?How to connect to an FTP Server from Windows Explorer?
    windows allows users to easily connect to an ftp server and the shared ftp folder using add network location wizard. after running the wizard, you can easily manage (copy, move, delete, create or rename ...) all content on ftp server from windows explorer.