加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL数值类型--浮点类型和序列

发布时间:2020-12-13 16:45:47 所属栏目:百科 来源:网络整理
导读:PostgreSQL包括整数类型和浮点数类型。 整数类型包括3种,分别是smallint、int和bigint。别名分别是int2、int(int4)和int8.常用数据类型是int(integer)。 浮点类型分为精确浮点数类型numeric和不精确浮点数类型real(单精度浮点数据类型)和 double prec

PostgreSQL包括整数类型和浮点数类型。

整数类型包括3种,分别是smallint、int和bigint。别名分别是int2、int(int4)和int8.常用数据类型是int(integer)。

浮点类型分为精确浮点数类型numeric和不精确浮点数类型real(单精度浮点数据类型)和 double precision(双精度浮点数据类型)。

精确浮点数类型可以用numeric(precision,scale)表示。

精度(precision)必须是正值,标度(scale)为零或正值。类型numeric和decimal等价,都符合SQL标准。 numeric不带精度和标度,则系统使用任意精度,不会超过数据库系统储存范围。创建表显示声明精度最大值为1000。

numeric(precision,0)等价于numeric(precision)。

---不限定精度和标度
postgres=# create table testdecimal(id int,testvalue decimal);
CREATE TABLE
postgres=# insert into testdecimal values(1,777777777.77777777);
INSERT 0 1
postgres=# insert into testdecimal values(1,777777777.7777777788888888888888);
INSERT 0 1
postgres=# select * from testdecimal;
 id |            testvalue
----+----------------------------------
  1 |               777777777.77777777
  1 | 777777777.7777777788888888888888
(2 行记录)
postgres=#
---限定精度
postgres=# create table testnumeric(id int,testnumeric numeric(3));
CREATE TABLE
postgres=# insert into testnumeric values(1,2.777);
INSERT 0 1
postgres=#
postgres=#
postgres=# insert into testnumeric values(1,2.7777);
INSERT 0 1
postgres=# insert into testnumeric values(1,2.77777);
INSERT 0 1
postgres=# select * from testnumeric;
 id | testnumeric
----+-------------
  1 |           3
  1 |           3
  1 |           3
(3 行记录)
---限定标度
postgres=# create table testnumeric(id int,testnumeric numeric(2,2));
CREATE TABLE
postgres=# insert into testnumeric values(1,0);
INSERT 0 1
postgres=# insert into testnumeric values(1,0.1);
INSERT 0 1
postgres=# insert into testnumeric values(1,1.1);
错误:  数字字段溢出
描述:  精度为2,范围是2的字段必须四舍五入到小于1的绝对值.
postgres=#

根据查询结果可知,不限制精度和标度,数据库系统会“原样储存”,限定精度,将导致四舍五入运算。限定标度,插入超范围数值将导致错误。

numeric类型允许特殊值NaN, 表示"不是一个数字(not a number)"。插入NaN时需要使用单引号,不区分大小写。PostgreSQL把NaN值视为相等,并且比所有非NaN值都要大。

---练习NaN使用。
postgres=# create table testnumeric(id int,testnumeric numeric);
CREATE TABLE
postgres=# insert into testnumeric values(2,'NaN');
INSERT 0 1
postgres=# insert into testnumeric values(2,'Nan');
INSERT 0 1
postgres=# insert into testnumeric values(2,'nan');
INSERT 0 1
postgres=# select * from testnumeric;
 id | testnumeric
----+-------------
  2 |         NaN
  2 |         NaN
  2 |         NaN
(3 行记录)

postgres=# select 'NaN'::decimal >'Nan'::decimal;
 ?column?
----------
 f
(1 行记录)

postgres=# select 'NaN'::decimal <'Nan'::decimal;
 ?column?
----------
 f
(1 行记录)

postgres=# select 'NaN'::decimal ='Nan'::decimal;
 ?column?
----------
 t
(1 行记录)

浮点数据类型分为real(单精度浮点数据类型)和 double precision(双精度浮点数据类型),都是不准确和变精度数据类型。不准确意味着某些值不能被正确转换或被近似处理,在检索或储存时出现缺失,如何处理本文不再详加讨论。不过我们需要注意以下几点

1、要求精度数据(例如货币)使用numeric数据类型。

2、使用不精确可变精度数据类型需要详细评估。

3、用两个浮点数值进行等值比较不可能总是按照期望地进行。

不精确可变精度浮点数据类型除支持一般浮点数以外,还支持3个特殊值,分别是infinity,-infinity和NaN。

严格意义来讲,序列(serial)不是数据类型,只是为表示序数方便所创造。

---创建序列
postgres=# create table testserial (id serial);
CREATE TABLE
postgres=#

---另一种等价创建序列
postgres=# create sequence testserial_id_sequence;
CREATE SEQUENCE
postgres=#
postgres=# create table testserial(id int default nextval('testserial_id_sequence'));
CREATE TABLE
postgres=#
postgres=# alter sequence testserial_id_sequence owned by testserial.id;
ALTER SEQUENCE
postgres=#

数据类型serial和serial4等效。当预期序号系列超过2^31时,使用bigserial(serial8)。

使用序列时应避免以下情况。

postgres=# drop table testserial;
DROP TABLE
postgres=# create table testserial(id serial4,name text);
CREATE TABLE
postgres=# insert into testserial values(1,'Tom'),(100,'Bill'),(2,'Lily');
INSERT 0 3
postgres=#
postgres=# select id,name from testserial;
 id  | name
-----+------
   1 | Tom
 100 | Bill
   2 | Lily
(3 行记录)

postgres=#

建议使用

postgres=# insert into testserial(name) values('Tom'),('Bill'),('Lily');
INSERT 0 3
postgres=# select id,name from testserial;
 id  | name
-----+------
   1 | Tom
 100 | Bill
   2 | Lily
   1 | Tom
   2 | Bill
   3 | Lily
(6 行记录)

postgres=#

更多细节,建议参考PostgreSQL手册。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读