数值类型解释
PostgreSQL中的所有数值类型及其解释:
类型名称 | 存储空间 | 描述 |
smallint | 2字节 | 小范围整数。Oracle中没有此类型,使用number代替 |
int或integer | 4字节 | 常用的整数。Oracle中的integer等效于number(38),与此类型的意义不同 |
bigint | 8字节 | 大范围的整数。Oracle中没有此类型,使用number代替。 |
numeric或decimal | 变长 | 用户声明的精度,精确。注意Oracle中叫number,与PG中名称不一致。 |
real | 4字节 | 变精度,不精确。 |
double precision | 8字节 | 变精度,不精确。 |
serial | 4字节 | 自增整数。 |
bigserial | 8字节 | 大范围的自增整数。 |
整数类型
整数类型有三种: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.