天天看点

pgsql数据类型:数字类型

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

整数

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

smallserial

serial

bigserial

类型不是真正的类型,它们只是为了创建唯一标识符列而存在的方便符号(类似其它一些数据库中支持的

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字节,值的范围也是很大

名字 存储尺寸 描述 范围

money

8 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