postgres 数据类型--数字类型

01, 数字类型

名字存储尺寸描述范围
smallint 2字节 小范围整数 -32768 to +32767
integer 4字节 整数的典型选择 -2147483648 to +2147483647
bigint 8字节 大范围整数 -9223372036854775808 to +9223372036854775807
decimal 可变 用户指定精度,精确 最高小数点前131072位,以及小数点后16383位
numeric 可变 用户指定精度,精确 最高小数点前131072位,以及小数点后16383位
real 4字节 可变精度,不精确 6位十进制精度
double precision 8字节 可变精度,不精确 15位十进制精度
smallserial 2字节 自动增加的小整数 1到32767
serial 4字节 自动增加的整数 1到2147483647
bigserial 8字节 自动增长的大整数 1到9223372036854775807

02,类型介绍

  smallint、integerbigint都是整数类型,存储定范围的整数,超出范围将会报错small int存储2字节整数,字段定义时可写成int2,integer存储4节整数,支持的数值范围比smallint大,宇段定义时可写成int4,是最常用的整数类型bigint存储8字节整数,支持的数值范围integer大,字段定义时可写成int8对于大多数使用整数类型的场景使integer就够了,除非integer范围不够用的情况下才使用bigint

  例:

 

kingledb=> CREATE TABLE Kingle_Study_1 (a INTEGER,b int4);
CREATE TABLE
Time: 8.888 ms
kingledb=> \dt Kingle_Study_1
            List of relations
 Schema |      Name      | Type  | Owner
--------+----------------+-------+--------
 kingle | kingle_study_1 | table | kingle
(1 row)

kingledb=> \d kingle_study_1
           Table "kingle.kingle_study_1"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 a      | integer |           |          |
 b      | integer |           |          |

 

  decimalnumeric是等效的,可以存储指定精度的多位数据,比如带小数位的数据,适用于要求计算准确的数值运

 

kingledb=> CREATE TABLE Kingle_Study_2 (a numeric(4,2),b decimal(5,3));
CREATE TABLE
Time: 1.801 ms
kingledb=> \d kingle_study_2
             Table "kingle.kingle_study_2"
 Column |     Type     | Collation | Nullable | Default
--------+--------------+-----------+----------+---------
 a      | numeric(4,2) |           |          |
 b      | numeric(5,3) |           |          |

kingledb=> insert  into   Kingle_Study_2 values (44.55,22.333);
INSERT 0 1
Time: 0.981 ms
kingledb=> select * from Kingle_Study_2
kingledb-> ;
   a   |   b
-------+--------
 44.55 | 22.333
(1 row)

Time: 0.622 ms

但要注意NUMERIC(precision, scale)  precision是指numeric数字里的全部位数scale指小数部分的数字位数,例如18.222precision5,而scale3; precision须为正整数,scale可以是0整数,由numeric类型上的算术运算相比整数类型性能低,因此,如果两种数据类型都能满足业务需求,从性能上考虑不建议使用numeric数据类型

  

kingledb=> insert  into   Kingle_Study_2 values (442.55,223.333);  --如果整数位比较大是不能插入成功的
ERROR:  numeric field overflow
DETAIL:  A field with precision 4, scale 2 must round to an absolute value less than 10^2.
Time: 0.645 ms
kingledb=> insert  into   Kingle_Study_2 values (44.2255,22.22333); --小数位多了 插入的数据也是只保留对应的位数
INSERT 0 1
Time: 0.952 ms
kingledb=> select * from Kingle_Study_2
;
   a   |   b
-------+--------
 44.55 | 22.333
 44.23 | 22.223
(2 rows)

Time: 0.380 ms

  realdoubleprecision指浮点数据类型,real支持4节,doubleprecision支持8节,浮点数据类型在实际生产案例的使用相比整数类型会少些

  smallserialserialbigserial类型是指自增serial类型,严格意义上不能称之为种数据类型

03,数学上的一些数字类型操作

kingledb=> select 1+2 as a,2*3 as b,4/2 as c,8/3 as d;  --加减乘除
 a | b | c | d
---+---+---+---
 3 | 6 | 2 | 2
(1 row)
kingledb=> SELECT "mod"(1116,9);  --取模
 mod
-----
   0
(1 row)

Time: 0.654 ms
-- 返回大于或等于给出参数的最小整数
kingledb=> SELECT "ceil"(3.9999),"ceil"(-4.1111); ceil | ceil ------+------ 4 | -4 (1 row) Time: 0.470 ms
kingledb=> select round(10.23) as a,round(11.9) as b; --四舍五入
 a  | b
----+----
 10 | 12
(1 row)

Time: 0.800 ms
kingledb=> SELECT floor(3.6), floor(-3.6); --返回小于或等于给出参数的最大整数
 floor | floor
-------+-------
     3 |    -4
(1 row)

Time: 0.480 ms


官网: http://www.postgres.cn/docs/10/datatype-numeric.html#DATATYPE-INT
书籍学习来自:postgres 实战
posted on 2020-05-26 11:25  kingle-l  阅读(3237)  评论(0编辑  收藏  举报

levels of contents