pgsql数据类型:数字类型

smallint2字节小范围整数-32768 to +32767
integer4字节整数的典型选择-2147483648 to +2147483647
bigint8字节大范围整数-9223372036854775808 to +9223372036854775807
decimal可变用户指定精度,精确最高小数点前131072位,以及小数点后16383位
numeric可变用户指定精度,精确最高小数点前131072位,以及小数点后16383位
real4字节可变精度,不精确6位十进制精度
double precision8字节可变精度,不精确15位十进制精度
smallserial2字节自动增加的小整数1到32767
serial4字节自动增加的整数1到2147483647
bigserial8字节自动增长的大整数1到9223372036854775807

整数

smallint、integer(或者int)、bigint。对应的扩展是int2、int4、int8

db=# create table demo_int(
db(# int2 int2,
db(# int4 int4,
db(# int8 int8,
db(# smallint smallint,
db(# integer integer,
db(# bigint bigint);

db=# insert into demo_int values (2, 4, 8, 2, 4, 8)
;
INSERT 0 1
db=# select * from demo_int;
 int2 | int4 | int8 | smallint | integer | bigint
------+------+------+----------+---------+--------
    2 |    4 |    8 |          |         |
    2 |    4 |    8 |        2 |       4 |      8

定点数

numeric类型,这个用法如下,该类型是用在对于精确描述的数字上面,比如货币金额

numeric(precision, scale)
numeric(precision)
numeric

说明:
precision:精度,就是小数点的左右共有多少个数字
scale:刻度,就是小数点的右边有多少个数字
比如:
number(3,2):表示的就是2.12
number(3):表示的就可以是整数:123
number:表示的就不限制了:1233,432, 2212876

注意:
1.虽然该类型功能看着很牛逼,但是该值进行计算的时候,要比整数和浮点数慢得多
2.该类型同decimal是同效的,两个都是sql规范中要求的
3.其中插入的时候是有限制的,整数部分的位数一定要小于等于precision-scale。否则就会失败,不过小数分部插入时候不关心,但是显示的时候就有区别了

-- 创建表
create table demo_numeric(num numeric(2,3));

-- 插入 OK
insert into demo_numeric values(12.3);
insert into demo_numeric values(12.332);
insert into demo_numeric values(1.332123123123);

-- 插入 异常
insert into demo_numeric values(123.332);

4.该类型还支持NaN,但是使用的时候要添加上’’,这样的一个引号才行

浮点数

浮点数这里有这么几种类型:
real
double precision
float§
float4
float8

real

这个跟float4是等价的

db=# create table demo_real(
db(# real real);
CREATE TABLE
db=# insert into demo_real values(12.323);
INSERT 0 1
db=# insert into demo_real values(17879234.323);
INSERT 0 1
db=# select * from demo_real;
     real
---------------
        12.323
 1.7879234e+07
double precision

这个跟float8是等价的

db=# create table demo_double(
db(# double double precision);
CREATE TABLE
db=# insert into demo_double values(123.123123);
INSERT 0 1
db=# insert into demo_double values(123.123123879987);
INSERT 0 1
db=# select * from demo_double;
      double
------------------
       123.123123
 123.123123879987
float

这个跟double precision是等价的

db=# create table demo_float(
db(# float float);
CREATE TABLE
db=# insert into demo_float values(123.3123);
INSERT 0 1
db=# insert into demo_float values(123.312808981233);
INSERT 0 1
  

db=# create table demo_float_n(
db(# float float4);
CREATE TABLE
db=# insert into demo_float_n values(1.333);
INSERT 0 1

发现上面三种类型都可以表示不定点的精度数(也叫浮点数),那么有什么区别呢
real:这个跟float§中的p在1~24是一样的,其实也有别名是:float4
double precision:这个是float§中的p为24~53,其实也有别名:float8

其中float§中的p不是表示小数点后面有多少个数,而是表示的当前这个小数可以用多少个bit表示,说是在pg中这个没有什么意义,其中用类型表示

db=# select pg_typeof(1.33333::float(1));
 pg_typeof
-----------
 real
(1 row)

db=# select pg_typeof(1.33333::float(24));
 pg_typeof
-----------
 real
(1 row)

db=# select pg_typeof(1.33333::float(25));
    pg_typeof
------------------
 double precision
(1 row)

db=# select pg_typeof(1.33333::float(53));
    pg_typeof
------------------
 double precision
(1 row)

db=# select pg_typeof(1.33333::float(54));
ERROR:  precision for type float must be less than 54 bits
LINE 1: select pg_typeof(1.3)

-- 其中float4就是real
db=# select pg_typeof(1.33333::float4);
 pg_typeof
-----------
 real
(1 row)
db=# select pg_typeof(1.33333::float8);
    pg_typeof
------------------
 double precision
(1 row)

其中默认的float,如果没有指定点数,则float表示的数就是double precision

注意:
浮点数如果插入的数据比表示的范围大,则会产生圆整错误

序列类型

这里有这么几种类型
smallserial:等效serial2
serial:等效serial4
bigserial:等效serial8
serial2
serial4
serial8

smallserialserialbigserial类型不是真正的类型,它们只是为了创建唯一标识符列而存在的方便符号(类似其它一些数据库中支持的AUTO_INCREMENT属性)。这个只是一个简化写法而已

db=# create table demo_serial(
db(# se serial,
db(# int int);
CREATE TABLE
db=# insert into demo_serial(int) values (22);
INSERT 0 1
db=# insert into demo_serial(int) values (22);
INSERT 0 1
db=# insert into demo_serial(int) values (22);
INSERT 0 1
db=# insert into demo_serial(int) values (22);
INSERT 0 1
db=# select * from demo_serial;
 se | int
----+-----
  1 |  22
  2 |  22
  3 |  22
  4 |  22
(4 rows)

货币类型

money类型存储固定小数精度的货币数字,在我们自己的金钱中,小数点后面是有两位的(元.角分)。小数的精度由数据库的lc_monetary设置决定。表中展示的范围假设有两个小数位。可接受的输入格式很多,包括整数和浮点数文字,以及常用的货币格式,如’$1,000.00’。 输出通常是最后一种形式,但和区域相关。

money,这个其实是专门用来表示货币的,是8字节,值的范围也是很大

名字存储尺寸描述范围
money8 bytes货币额-92233720368547758.08到+92233720368547758.07
db=# create table demo_money(
db(# money money);
CREATE TABLE
db=# insert into demo_money values(123.2312);
INSERT 0 1
db=# insert into demo_money values(123.23129808098);
INSERT 0 1
db=# insert into demo_money values(12376287348234.23);
INSERT 0 1
db=# select * from demo_money;
         money
------------------------
                $123.23
                $123.23
 $12,376,287,348,234.23
(3 rows)

也可以通过字符串的方式插入

db=# insert into demo_money values('$12.09');
INSERT 0 1
db=# select * from demo_money;
         money
------------------------
                $123.23
                $123.23
 $12,376,287,348,234.23
                 $12.09
(4 rows)

参考:

官网:http://postgres.cn/docs/11/datatype-numeric.html

  • 0
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值