PostgreSQL包括整数类型和浮点数类型。数据库
整数类型包括3种,分别是smallint、int和bigint。别名分别是int二、int(int4)和int8.经常使用数据类型是int(integer)。post
浮点类型分为精确浮点数类型numeric和不精确浮点数类型real(单精度浮点数据类型)和 double precision(双精度浮点数据类型)。code
精确浮点数类型能够用numeric(precision, scale)表示。ci
精度(precision)必须是正值,标度(scale)为零或正值。类型numeric和decimal等价,都符合SQL标准。 numeric不带精度和标度,则系统使用任意精度,不会超过数据库系统储存范围。建立表显示声明精度最大值为1000。it
numeric(precision, 0)等价于numeric(precision)。io
---不限定精度和标度 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=#
根据查询结果可知,不限制精度和标度,数据库系统会“原样储存”,限定精度,将致使四舍五入运算。限定标度,插入超范围数值将致使错误。table
numeric类型容许特殊值NaN, 表示"不是一个数字(not a number)"。插入NaN时须要使用单引号,不区分大小写。PostgreSQL把NaN值视为相等,而且比全部非NaN值都要大。test
---练习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(双精度浮点数据类型),都是不许确和变精度数据类型。不许确意味着某些值不能被正确转换或被近似处理,在检索或储存时出现缺失,如何处理本文再也不详加讨论。不过咱们须要注意如下几点select
一、要求精度数据(例如货币)使用numeric数据类型。数据类型
二、使用不精确可变精度数据类型须要详细评估。
三、用两个浮点数值进行等值比较不可能老是按照指望地进行。
不精确可变精度浮点数据类型除支持通常浮点数之外,还支持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手册。