【问题标题】:Why is UPDATE statement not working in SQL code?Why is UPDATE statement not working in SQL code?
【发布时间】:2022-12-02 01:00:06
【问题描述】:

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.

【问题讨论】:

  • Does a storeID of 'E50' exist in your products table?

标签: sql


【解决方案1】:

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 thatispresent 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.

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 2022-12-02
    • 2022-11-20
    • 2020-11-11
    • 2022-12-27
    • 2022-12-27
    • 2022-12-01
    • 2022-12-31
    • 2022-12-27
    相关资源
    最近更新 更多