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_bang
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 TABLEsanpham
( 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.
- Activate foreign keys in SQL Server
- Foreign Key (Set Null) foreign key in SQL Server
- 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 hangtonkho
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
You should read it
- Foreign Key with Set Null in SQL Server
- Foreign Key foreign key in SQL Server
- Disable foreign key in SQL Server
- Need to be wary of foreign objects in the ear
- The 10 best 'jobs' are available only to those who know a foreign language
- 7 compelling reasons for you to definitely learn a foreign language
- 7 simple tips to help you learn new languages in just one week
- DELETE command in SQL Server
May be interested
- Disable foreign key in SQL Serverif you want to disable foreign keys in sql server, this is how.
- Activate foreign keys in SQL Serverthis article explains how to enable a foreign key created in sql server.
- How to completely uninstall SQL Serverthis article will show you how to uninstall sql server completely. follow the steps in this article, you also need to prepare the system to be able to reinstall sql server.
- IN conditions in SQL Serverthe in condition is used in sql server (transact-sql) to minimize the need to use too many or conditions.
- IS NULL condition in SQL Serverthe is null condition is used to check null values in sql server.
- IS NOT NULL condition in SQL Serverthis sql server tutorial shows how to use the is not null condition along with specific syntax and examples.