0

I am trying to update one row of the storeID column. When I do the first code below, it runs but will not affect any row. However, the bottom two are producing the following error " The UPDATE statement conflicted with the REFERENCE constraint "Products_FK". The conflict occurred in database "group7", table "dbo.Products", column 'storeID'."

Could anyone help? Thanks!

Table in SQL

UPDATE Store
SET storeID='E50'
WHERE storeID='D50'
AND storeID is Null;
UPDATE Store
SET storeID='E50'
WHERE storeID='D50'
UPDATE Store
SET storeID='E50'
WHERE storeName='A Plus Cables'

Above is the code that I have tried and nothing is being updated.

3
  • Does a storeID of 'E50' exist in your products table? Commented Dec 1, 2022 at 16:47
  • @ Ryan. No it doesn't. It is messed up in all the tables. Our professor switched around data after we had placed them in SQL. Commented Dec 1, 2022 at 18:40
  • Then that is why it is failing. The provided answer gives enough details to explain this, but as the answer states, since that column has reference to the product table and that key doesn't exist in products, it won't allow you to update it to a value that doesn't exist. Commented Dec 1, 2022 at 18:43

1 Answer 1

2

The first one can't possibly match any rows, because a single value can't equal two different things at the same time:

WHERE storeID='D50'
AND storeID is Null

So that one is kind of a moot point. You'd need to update the WHERE clause to target the record(s) you want to target.

For the latter two, the error is telling you what's wrong. You're trying to write this value to a column:

SET storeID='E50'

But the error is telling you:

  1. That column is a foreign key to another table. (Products?)
  2. The value you're writing isn't presentin that other table.

So your options are:

  1. Use a value that is present in the other table.
  2. Use NULL (and update the column to allow NULL if necessary).
  3. Remove the foreign key constraint to that other table.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.