Using expression in update statement : Update « Insert Delete Update « PostgreSQL






Using expression in update 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 price = price * 1.10;
UPDATE 5
postgres=#
postgres=# select * from products;
 product_no |    name    |  price
------------+------------+----------
          1 | Java       |  1357.40
          2 | SQL Server |  3763.10
          3 | Oracle     |  8385.30
          4 | DB2        | 10861.40
          5 | Access     |     5.50
(5 rows)

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








Related examples in the same category

1.Update a single row
2.Update two columns in one single 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