Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove foreign key constraint in sql server?

I want to remove foreign key from another table so i can insert values of my choice.

I am new in databases so please tell me correct sql query to drop or remove foreign key value.

like image 645
Ammar Asjad Avatar asked Sep 19 '12 06:09

Ammar Asjad


People also ask

How do I delete a foreign key constraint in SQL Server?

In the INSERT and UPDATE specifications, select Cascade for the delete rule. Click on Close and save the table in the designer. Click Yes in the warning message window. Once you click on Yes, a foreign key with delete rule is created.

How do I remove a foreign key from a constraint?

To drop a foreign key from a table, use the ALTER TABLE clause with the name of the table (in our example, student ) followed by the clause DROP CONSTRAINT with the name of the foreign key constraint.


2 Answers

Try following

ALTER TABLE <TABLE_NAME> DROP CONSTRAINT <FOREIGN_KEY_NAME> 

Refer : http://www.w3schools.com/sql/sql_foreignkey.asp

like image 93
Prasanna Avatar answered Sep 21 '22 17:09

Prasanna


Its wrong to do that in refer to referential integrity, because once its broken its not easy to turn it on again without having to go through the records and delete the ones which breaks the constraints.

Anyway the Syntax is as follows:

ALTER TABLE Tablename DROP CONSTRAINT ContName; 

See MSDN:

  • Delete Primary Keys
  • Delete Foreign Key Relationships
like image 45
CloudyMarble Avatar answered Sep 18 '22 17:09

CloudyMarble