Update two columns in one single statement : Update « Insert Delete Update « PostgreSQL






Update two columns in one single statement


postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer,
postgres(#    name text,
postgres(#    price numeric
postgres(# );
CREATE TABLE
postgres=#
postgres=# INSERT INTO products (product_no, name, price) VALUES (1, 'Java', 1234);
INSERT 0 1
postgres=# INSERT INTO products (product_no, name, price) VALUES (2, 'SQL Server', 3421);
INSERT 0 1
postgres=# INSERT INTO products (product_no, name, price) VALUES (3, 'Oracle', 7623);
INSERT 0 1
postgres=# INSERT INTO products (product_no, name, price) VALUES (4, 'DB2', 9874);
INSERT 0 1
postgres=# INSERT INTO products (product_no, name, price) VALUES (5, 'Access', 5);
INSERT 0 1
postgres=#
postgres=#
postgres=# select * from products;
 product_no |    name    | price
------------+------------+-------
          1 | Java       |  1234
          2 | SQL Server |  3421
          3 | Oracle     |  7623
          4 | DB2        |  9874
          5 | Access     |     5
(5 rows)

postgres=#
postgres=# UPDATE products SET product_no = 999, name = 'new Name', price = 1 WHERE price > 0;
UPDATE 5
postgres=#
postgres=# select * from products;
 product_no |   name   | price
------------+----------+-------
        999 | new Name |     1
        999 | new Name |     1
        999 | new Name |     1
        999 | new Name |     1
        999 | new Name |     1
(5 rows)

postgres=#
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#
           
       








Related examples in the same category

1.Update a single row
2.Using expression in update statement
3.A simple UPDATE: change one cell
4.Updating entire columns
5.Updating Several Columns
6.Using UPDATE with several Employees
7.Update in a slice
8.Using user defined function to update table data