您的位置:首页 > 数据库

PostgreSQL学习篇9.2 数值类型

2016-12-19 11:16 761 查看
数值类型解释PostgreSQL中的所有数值类型及其解释:
类型名称存储空间描述
smallint2字节小范围整数。Oracle中没有此类型,使用number代替
int或integer4字节常用的整数。Oracle中的integer等效于number(38),与此类型的意义不同
bigint8字节大范围的整数。Oracle中没有此类型,使用number代替。
numeric或decimal变长用户声明的精度,精确。注意Oracle中叫number,与PG中名称不一致。
real4字节变精度,不精确。
double precision8字节变精度,不精确。
serial4字节自增整数。
bigserial8字节大范围的自增整数。
整数类型
整数类型有三种:smallint、int、bigint
int、integer、int4等效;int2、smallint等效;bigint、int8等效。

精确的小数类型
精确的小树类型可用numeric、numeric(m,n)、numeric(m)表示。
numeric与decimal等效,可以存储最多1000位精度的数字,并且可以进行准确的计算。适合货币金额和其他要求精确计算的场合。不过,numeric类型上的算术运算比证书类型或浮点数类型慢很多(类似于Oracle里面的number,但是,Oracle中int相当于number(m,0))。

示例:
postgres=# create table test(col1 numeric(3),col2 numeric(3,0),col3 numeric(3,2),col4 numeric);
CREATE TABLE
postgres=#
postgres=# insert into test values (2,2,2.222,2.222);
INSERT 0 1
postgres=# select * from test;
col1 | col2 | col3 | col4
------+------+------+-------
2 |    2 | 2.22 | 2.222
(1 row)

postgres=# insert into test values(22,22,22.222,22.222);
ERROR:  numeric field overflow
DETAIL:  A field with precision 3, scale 2 must round to an absolute value less than 10^1.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: