TExcelTAD
Home › PostgreSQL › PostgreSQL UPDATE

PostgreSQL UPDATE

UPDATE changes values in rows that already exist. You say which table to change, which columns get new values (SET), and which rows to touch (WHERE).

text
UPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;

Updating one row

Customer 3 (Chloé Dubois) has no email address yet. Let's add one:

pgsql · try it yourself
UPDATE customers
SET email = 'chloe.d@example.com'
WHERE id = 3;

SELECT id, first_name, email FROM customers WHERE id = 3;
UPDATE 1
 id | first_name |        email
----+------------+---------------------
  3 | Chloé      | chloe.d@example.com
(1 row)

The tag UPDATE 1 reports how many rows were changed. If it says UPDATE 0, your WHERE clause matched nothing, which is worth checking.

Updating many rows with an expression

The new value can be computed from the old one. The shop raises the price of every Programming book (category 4) by 10 %, rounded to cents:

pgsql · try it yourself
SELECT title, price FROM books WHERE category_id = 4 ORDER BY id;

UPDATE books
SET price = round(price * 1.10, 2)
WHERE category_id = 4;

SELECT title, price FROM books WHERE category_id = 4 ORDER BY id;
           title           | price
---------------------------+-------
 Learning SQL the Calm Way | 39.90
 Postgres in Practice      | 44.00
 Rust for Curious Minds    | 42.50
 Algorithms on a Napkin    | 35.00
(4 rows)

UPDATE 4
           title           | price
---------------------------+-------
 Learning SQL the Calm Way | 43.89
 Postgres in Practice      | 48.40
 Rust for Curious Minds    | 46.75
 Algorithms on a Napkin    | 38.50
(4 rows)

Changing several columns at once

Separate the assignments with commas. All expressions on the right-hand side see the old values of the row, so this swap really swaps:

pgsql · try it yourself
CREATE TABLE swap_demo (a integer, b integer);
INSERT INTO swap_demo VALUES (1, 2);

UPDATE swap_demo SET a = b, b = a;
SELECT * FROM swap_demo;
CREATE TABLE
INSERT 0 1
UPDATE 1
 a | b
---+---
 2 | 1
(1 row)

Using another table: UPDATE … FROM

Sometimes the new value or the condition depends on another table. PostgreSQL's UPDATE … FROM joins the tables. Here every order of a customer from Leeds becomes shipped if it is still pending:

pgsql · try it yourself
UPDATE orders o
SET status = 'shipped'
FROM customers c
WHERE c.id = o.customer_id
  AND c.city = 'Leeds'
  AND o.status = 'pending';

SELECT id, customer_id, status FROM orders WHERE customer_id IN (2, 15) ORDER BY id;
UPDATE 1
 id | customer_id |  status
----+-------------+-----------
  2 |           2 | delivered
  8 |           2 | delivered
 18 |           2 | shipped
(3 rows)

The most dangerous mistake

An UPDATE without WHERE changes every row in the table. There is no undo (unless you are inside a transaction, see Transactions):

pgsql · try it yourself
UPDATE books SET stock = 0;
SELECT count(*) AS books_with_zero_stock FROM books WHERE stock = 0;
UPDATE 20
 books_with_zero_stock
-----------------------
                    20
(1 row)

Tip: Before running an UPDATE, run a SELECT with the same WHERE clause first and check that it returns exactly the rows you mean to change.

Constraints still apply to updates. Setting a price to zero breaks CHECK (price > 0), so nothing is changed:

pgsql · try it yourself
UPDATE books SET price = 0 WHERE id = 1;
SELECT price FROM books WHERE id = 1;
ERROR:  new row for relation "books" violates check constraint "books_price_check"
DETAIL:  Failing row contains (1, The Salt Road, 1, 1, 0.00, 12, 2019-03-14, {travel,family}, {"pages": 312, "format": "paperback", "language": "en"}).
 price
-------
 14.99
(1 row)

Test yourself

What happens when you run UPDATE books SET stock = 5;?

  1. Only the first book changes
  2. Nothing, WHERE is required
  3. Every book's stock becomes 5
  4. A syntax error

What does UPDATE 0 mean?

  1. The update failed with an error
  2. No row matched the WHERE condition
  3. The first row was updated
  4. All rows were updated

In UPDATE t SET a = b, b = a, which values do the right-hand sides use?

  1. The new values, so both end up equal
  2. The old values of the row, so a and b are swapped
  3. It's an error
  4. Random