How to test an SQL Update statement before running it?

前端 未结 9 1221
野趣味
野趣味 2020-12-23 20:29

In some cases, running an UPDATE statement in production can save the day. However a borked update can be worse than the initial problem.

Short of using a test datab

9条回答
  •  情话喂你
    2020-12-23 20:57

    In addition to using a transaction as Imad has said (which should be mandatory anyway) you can also do a sanity check which rows are affected by running a select using the same WHERE clause as the UPDATE.

    So if you UPDATE is

    UPDATE foo
      SET bar = 42
    WHERE col1 = 1
      AND col2 = 'foobar';
    

    The following will show you which rows will be updated:

    SELECT *
    FROM foo
    WHERE col1 = 1
      AND col2 = 'foobar';
    

提交回复
热议问题