Question

I am pretty new in Microsoft SQL Server and I am not so into DB and I have the following problem.

I have to delete all the records that are inside a table named VulnerabilityReference

So I executed this statment:

delete from VulnerabilityReference;

But I obtain this error message and no rows are deleted form my table:

Msg 547, Level 16, State 0, Line 1
The DELETE statement conflicted with the REFERENCE constraint "FK_AlertDocument_Reference_Reference". The conflict occurred in database "DB NAME", table "dbo.VulnerabilityAlertDocument_VulnerabilityReference", column 'VulnerabilityReferenceId'.
The statement has been terminated.

What it exactly means? Have I to delete all the records from the VulnerabilityAlertDocument_VulnerabilityReference table before delete the records into my VulnerabilityReference?

Tnx

Andrea

Was it helpful?

Solution

The table you are attempting to delete has it's primary key in another table. You must first remove (or set to null) the column in the other table BEFORE you can delete. This is called DB Referential integrity.

You can disable constraints (if you have adequate permissions) but I would suggest you not do that.

To remove the link for all records in that other table, you could do this:

UPDATE VulnerabilityAlertDocument_VulnerabilityReference
  SET VulnerabilityReferenceId = NULL

If the AlertDocument table does NOT ALLOW nulls for that column then you'll need to DELETE all the VulnerabilityAlertDocument_VulnerabilityReference records etc... OR ALTER the table to ALLOW NULL. Make sure you know what you are about to do...

I am assuming the name of the column in the VulnerabilityAlertDocument_VulnerabilityReference table, you obviously need to use the correct table and column names.

OTHER TIPS

This error is because you have an id of a record in VulnerabilityReference related to a AlerDocument table. The error is warning you of this problem and that you can't delete this record if you don't delete first the related records.

I mean the tables in the relation named as FK_AlertDocument_Reference_Reference You have a relationship between VulnerabilityAlertDocument_VulnerabilityReference and other table. You can go to this table and find the relationships, find the name beginning with FK. In the properties of this relationship you will see the name of the other table and columns used in this relationship. If you want you can create a diagram, then drop this two tables and you will see in a visual way how the relationship is built

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top