Disable foreign key in SQL Server
If you want to disable foreign keys in SQL Server, this is how.
Once you have created the FOREIGN KEY foreign key in SQL Server, there will be instances where you need to disable them. Then use the ALTER TABLE command.
The syntax to disable foreign keys in SQL Server
ALTER TABLE ten_bang
NOCHECK C
ONSTRAINT fk_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 disable.
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.
- Foreign Key (Cascade Delete) in SQL Server
- Foreign Key (Set Null) foreign key in SQL Server
If you want to disable the foreign key created, run the command below.
ALTER TABLE hangtonkho
NOCHECK CONSTRAINT
fk_htk_id_sanpham;
The example above uses the ALTER TABLE command to disable the foreign key fk_htk_id_sanpham created in the hangtonkho table.
Previous post: Delete the foreign key in SQL Server
The following article: Enable foreign key in SQL Server
You should read it
- Activate foreign keys in SQL Server
- Foreign Key foreign key in SQL Server
- Delete the foreign key in SQL Server
- Foreign Key with Cascade Delete in SQL Server
- Foreign Key with Set Null 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
- Primary key PRIMARY KEY in SQL Server
- Good news: Learning a foreign language helps increase brain elasticity
- Summary of some simple ways to disable USB ports on Windows computers
Maybe you are interested
Instructions for using the Office Lens application to convert image files into written words on Android Is Tiross vacuum cleaner good? How to pin messages to chat groups on Viber Blind prophet Vanga and the predictions of disaster came in 3797 Discover the strange green planet HD 189733b outside the solar system 'Aliens will arrive on Earth in September 2017' - Nostradamus' prediction