How to delete if Forign Key is there (asp.net,sql)
How to delete a Table data If forign key is there in asp.net
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Manoj BhoirRank #599 · 4K reputationPosted Apr 28, 2015, 11:18 PM EDT
You can disable and re-enable the foreign key constraints before and after deleting.
alter table TableName nocheck constraint all
delete from TableName
alter table TableName check constraint all
Or, If you wish the delete to be automatic, you need to change your schema so that the foreign key constraint is ON DELETE CASCADE.
For more details about CASCADE DELETE see this link :
http://www.techonthenet.com/sql_server/foreign_keys/foreign_delete.php
Or, you have to first delete records from Child table and then try to delete record from Parent table.
For more info refer this link :
http://stackoverflow.com/questions/8251146/delete-data-with-foreign-key-in-sql-server-table
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted Apr 28, 2015, 11:04 PM EDT
You need to delete the data from the child table first where it has foreign key relationship and then delete the data from the parent table
You may also try to look at "cascading delete in sql server"