114

How can I test an UPDATE statement for example to see if it would work, for example if it would actually update rows etc?

Is there a way to simulate it easily?

0

7 Answers 7

188

Use a transaction to wrap your update statement and a select query (to test the update) and then always roll it back.

Example:

BEGIN;

UPDATE accounts SET balance = balance - 100.00
    WHERE name = 'Alice';

SELECT balance FROM accounts WHERE name = 'Alice';

ROLLBACK; -- << Important! Un-does your UPDATE statement above!

A transaction typically ends with a commit but since you're just testing and do not want the changes to be permanent you will just roll back.

3
  • Why are you making an update, you don't trust to psql ?:) Jun 8, 2022 at 22:28
  • Does this TRANSACTION applies to all SQL versions?
    – Robin
    Oct 4, 2022 at 18:00
  • @ДавидШико because I didn't write the statement.
    – RonJohn
    Jun 23 at 3:04
58

Wrap it in a transaction, test the results with a SELECT and rollback at the end.

BEGIN;

UPDATE ...;

SELECT ...;

ROLLBACK;
2
  • 8
    You could use RETURNING in the UPDATE query to skip the SELECT query. Feb 9, 2012 at 6:29
  • 7
    If you decide to keep the changes, use COMMIT instead of ROLLBACK.
    – n.st
    Dec 1, 2013 at 19:28
31

Prepend your SQL UPDATE command with EXPLAIN [ref], and it will not perform the actual update. E.g.:

EXPLAIN UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Alice';

It will not (always) tell you the number of rows which would be updated but rather tell you how many rows match the WHERE clause. In the above example those numbers are the same though.

You can refer to Paul Sasik's answer for a rollback approach.

1
  • 3
    "it will tell you how many lines will be affected by your command" - I don't think this is true; it just tells you how many rows it will scan. Feb 2 at 10:52
5

You could always build up a sample database on SQL Fiddle and try out your update statements there.

Full disclosure: I am the author of sqlfiddle.com

1
  • 1
    SQL Fiddle is a great learning tool, but setting up a multi-table DB there, just to test a query, it is not the best option. Kudos for the site tho! May 26 at 8:31
1

With Postgres you can use the UPDATE clause RETURNING to show which rows have been modificated.

-- example data
CREATE TABLE data(id int, text text);
INSERT INTO DATA VALUES(1,'aaa'),(2,'bbb'),(3,'ccc'),(4,'ddd');

-- original data
SELECT * from data;

-- dry-run update
BEGIN;

UPDATE
  data
SET
  text = 'modified'
WHERE
  id > 2
RETURNING
  id, text;

ROLLBACK;

-- data after dry-run update
SELECT * from data;
1
  • 1
    rollback is avoiding the commit of any changes
    – adrianlzt
    Apr 26, 2021 at 14:13
0

Run the same check with a SELECT statement first: the rows returned by SELECT will be the rows modified by UPDATE

1
  • 2
    Not sufficient. There could be FKs, CHECK constraints, ... that a simple SELECT wouldn't hit. A SELECT would only test the WHERE clause but the UPDATE could fail even though the SELECT succeeds. Feb 8, 2012 at 22:04
0

Given this simple update:

UPDATE Products
   SET price_including_vat = price * 1.05
 WHERE product_type = 'Food';

I would test it using something like this:

 SELECT price_including_vat AS price_including_vat__before, 
        price * 1.05 AS price_including_vat__after, 
        *
   FROM Products
 WHERE product_type = 'Food';

Actually, I'd proably engage brain and do analysis more like this:

WITH updated AS 
   (
    SELECT price_including_vat AS price_including_vat__before, 
           price * 1.05 AS price_including_vat__after, 
           *
      FROM Products
    WHERE product_type = 'Food'
   )
SELECT * 
  FROM updated
 WHERE price_including_vat__before = price_including_vat__after;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.