Update if different/changed

后端 未结 8 2025
野性不改
野性不改 2020-12-15 02:54

Is it possible to perform an update statement in sql, but only update if the updates are different?

for example

if in the database, col1 = \"hello\"

相关标签:
8条回答
  • 2020-12-15 03:29

    Old question but none of the answers correctly address null values.

    Using <> or != will get you into trouble when comparing values for differences if there are is potential null in the new or old value to safely update only when changed use the is distinct from operator in Postgres. Read more about it here

    0 讨论(0)
  • 2020-12-15 03:40

    This is possible with a before-update trigger. In this trigger you can compare the old with the new values and cancel the update if they don't differ. But this will then lead to an error on the caller's site.
    I don't know, why you want to do this, but here are several possibilities:

    1. Performance: There is no performance gain here, because the update would not only need to find the correct row but additionally compare the data.
    2. Trigger: If you want the trigger only to be fired if there was a real change, you need to implement your trigger like so, that it compares all old values to the new values before doing anything.
    0 讨论(0)
提交回复
热议问题