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
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Manoj BhoirPosted Apr 29, 2015, 3:18 AM
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 DuranoPosted Apr 29, 2015, 3:04 AM
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"