PostgreSQL/Data Type/numeric

Материал из SQL эксперт
Перейти к: навигация, поиск

A numeric alternative to money

postgres=#
postgres=# -- A numeric alternative to money
postgres=#
postgres=# CREATE TABLE money_example (
postgres(#             money_cash money,
postgres(#             numeric_cash numeric(10,2));
CREATE TABLE
postgres=#
postgres=# INSERT INTO money_example VALUES ("$12.24", 12.24);
INSERT 0 1
postgres=#
postgres=# SELECT * FROM money_example;
 money_cash | numeric_cash
------------+--------------
     $12.24 |        12.24
(1 row)
postgres=#
postgres=#
postgres=# drop table money_example;
DROP TABLE
postgres=#



Use numeric column

postgres=#
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=# -- Updates all products that have a price of 5 to have a price of 10:
postgres=# UPDATE products SET price = 10 WHERE price = 5;
UPDATE 1
postgres=#
postgres=# select * from products;
 product_no |    name    | price
------------+------------+-------
          1 | Java       |  1234
          2 | SQL Server |  3421
          3 | Oracle     |  7623
          4 | DB2        |  9874
          5 | Access     |    10
(5 rows)
postgres=#
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#