Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm kinda beginner in sql but am getting this error-
"The DELETE statement conflicted with the REFERENCE constraint "documents_fk15". The conflict occurred in database "RD_Doc", table "dbo.doc_source", column 'document_id'.
The statement has been terminated."

What I have tried:

The command I tried to run-
"DELETE from documents where document_id = 209965"
Posted
Updated 10-Mar-21 7:23am

You are trying to delete a record from the documents table, but you have related records in the doc_source table which depend on the record you are trying to delete.

You will need to delete the related records from the doc_source table before you can delete the record from the documents table.

If you always want the related records to be deleted, you can set the foreign key constraint to "cascade deletes":
Using DELETE CASCADE Option for Foreign Keys[^]
 
Share this answer
 
Comments
Maciej Los 10-Mar-21 15:55pm    
5ed!
To delete the related records manually, without changing the foreign key to use the CASCADE option, you can do it in two steps:
SQL
delete from doc_source where document_id = 209965;
delete from documents where document_id = 209965;
You may want to wrap that in a transaction and confirm that you've only deleted the records from doc_source that you intended to, before you delete from documents. If this is Important Data, then perhaps get a backup of the data before you proceed. If this is only a database you are using for educational purposes, and can repopulate easily, you might be able to afford to skip the backup.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900