smallint |
2字節(jié) | 小范圍整數(shù) | -32768 to +32767 |
---|---|---|---|
integer |
4字節(jié) | 整數(shù)的典型選擇 | -2147483648 to +2147483647 |
bigint |
8字節(jié) | 大范圍整數(shù) | -9223372036854775808 to +9223372036854775807 |
decimal |
可變 | 用戶指定精度,精確 | 最高小數(shù)點前131072位牵啦,以及小數(shù)點后16383位 |
numeric |
可變 | 用戶指定精度病附,精確 | 最高小數(shù)點前131072位糜芳,以及小數(shù)點后16383位 |
real |
4字節(jié) | 可變精度讳苦,不精確 | 6位十進制精度 |
double precision |
8字節(jié) | 可變精度勉躺,不精確 | 15位十進制精度 |
smallserial |
2字節(jié) | 自動增加的小整數(shù) | 1到32767 |
serial |
4字節(jié) | 自動增加的整數(shù) | 1到2147483647 |
bigserial |
8字節(jié) | 自動增長的大整數(shù) | 1到9223372036854775807 |
整數(shù)
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
定點數(shù)
numeric類型目木,這個用法如下,該類型是用在對于精確描述的數(shù)字上面,比如貨幣金額
numeric(precision, scale)
numeric(precision)
numeric
說明:
precision:精度刽射,就是小數(shù)點的左右共有多少個數(shù)字
scale:刻度军拟,就是小數(shù)點的右邊有多少個數(shù)字
比如:
number(3,2):表示的就是2.12
number(3):表示的就可以是整數(shù):123
number:表示的就不限制了:1233,432誓禁, 2212876
注意:
1.雖然該類型功能看著很牛逼懈息,但是該值進行計算的時候,要比整數(shù)和浮點數(shù)慢得多
2.該類型同decimal是同效的摹恰,兩個都是sql規(guī)范中要求的
3.其中插入的時候是有限制的辫继,整數(shù)部分的位數(shù)一定要小于等于precision-scale。否則就會失敗俗慈,不過小數(shù)分部插入時候不關(guān)心姑宽,但是顯示的時候就有區(qū)別了
-- 創(chuàng)建表
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,但是使用的時候要添加上''闺阱,這樣的一個引號才行
浮點數(shù)
浮點數(shù)這里有這么幾種類型:
real
double precision
float(p)
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
發(fā)現(xiàn)上面三種類型都可以表示不定點的精度數(shù)(也叫浮點數(shù))炮车,那么有什么區(qū)別呢
real:這個跟float(p)中的p在1~24是一樣的,其實也有別名是:float4
double precision:這個是float(p)中的p為24~53酣溃,其實也有別名:float8
其中float(p)中的p不是表示小數(shù)點后面有多少個數(shù)瘦穆,而是表示的當前這個小數(shù)可以用多少個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难审,如果沒有指定點數(shù),則float表示的數(shù)就是double precision
注意:
浮點數(shù)如果插入的數(shù)據(jù)比表示的范圍大亿絮,則會產(chǎn)生圓整錯誤
序列類型
這里有這么幾種類型
smallserial:等效serial2
serial:等效serial4
bigserial:等效serial8
serial2
serial4
serial8
smallserial
告喊、serial
和bigserial
類型不是真正的類型,它們只是為了創(chuàng)建唯一標識符列而存在的方便符號(類似其它一些數(shù)據(jù)庫中支持的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類型存儲固定小數(shù)精度的貨幣數(shù)字黔姜,在我們自己的金錢中,小數(shù)點后面是有兩位的(元.角分)蒂萎。小數(shù)的精度由數(shù)據(jù)庫的lc_monetary設置決定秆吵。表中展示的范圍假設有兩個小數(shù)位∥宕龋可接受的輸入格式很多纳寂,包括整數(shù)和浮點數(shù)文字,以及常用的貨幣格式泻拦,如'$1,000.00'毙芜。 輸出通常是最后一種形式,但和區(qū)域相關(guān)争拐。
money腋粥,這個其實是專門用來表示貨幣的,是8字節(jié),值的范圍也是很大
名字 | 存儲尺寸 | 描述 | 范圍 |
---|---|---|---|
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)