Disable foreign key in SQL Server
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
- 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
May be interested
- The difference between web server and app serveryou have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
- 7 simple tips to help you learn new languages in just one weekextremely useful foreign language learning tips for two 10-speaking twins.
- Bkav released the W32.WeakPass anti-virus server inspection toolon the afternoon of february 19, bkav released a tool to help administrators check the security of the server against a targeted attack on vietnamese public servers from foreign hackers.
- ALTER LOGIN command in SQL Serverthe alter login command is used to modify the identity used to connect to sql server.
- Network basics: Part 3 - DNS Servera dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
- Instructions for fixing 'A referral was returned from the server'a referral was returned from the server error can occur on any windows version (windows 8, 7 and windows vista) while the user is running a program or an application without admin rights or if uac settings are enabled on the system.
- Good news: Learning a foreign language helps increase brain elasticitynew findings show that people who are exposed to foreign languages have better elasticity.
- Summary of some simple ways to disable USB ports on Windows computersyou can imagine that usb ports are like door scenes, and other users or malware, viruses can spread and access your system anytime through this path. so to protect important data on your windows computer, you can disable the usb port.
- How to disable USB ports on Windows, Mac and Linuxnow, you know that using a usb storage device on your computer has many potential security risks. if you fear the risk of being infected with malware, such as trojian, keyloggers or ransomware, you should completely disable the usb storage device if the system has a lot of sensitive data.
- Check constraints in SQL Serverwhat is the check constraint in sql server, what is it used for and what is it used for? this article will give you the answer.