postgresql 浮点数函数_PostgreSQL数值类型--浮点类型和序列

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

?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手册。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值