- 查看當(dāng)前數(shù)據(jù)庫(kù)的數(shù)據(jù)類型
postgres=# \d pg_type ;
Table "pg_catalog.pg_type"
Column | Type | Modifiers
----------------+--------------+-----------
typname | name | not null
typnamespace | oid | not null
----
//顯示所有的type類型以及對(duì)應(yīng)的存儲(chǔ)類型:
postgres=# select typname, typstorage from pg_type ;
typname | typstorage
---------------------------------------+------------
bool | p
bytea | x
char | p
name | p
int8 | p
int2 | p
int2vector | p
int4 | p
regproc | p
text | x
oid | p
tid | p
xid | p
cid | p
oidvector | p
pg_type | x
pg_attribute | x
pg_proc | x
pg_class | x
json | x
xml | x
_xml | x
關(guān)于存儲(chǔ)類型 p x e m 的含義 ,請(qǐng)自行搜索括改,代表了各自 不同的存儲(chǔ)方式腻豌。
左側(cè)為數(shù)據(jù)類型的分類
Paste_Image.png
- 常見的數(shù)據(jù)類型,數(shù)字
Paste_Image.png
關(guān)于 serial類型嘱能,效果其實(shí)和integer + next sequence 一樣吝梅。
當(dāng)你創(chuàng)建了 serial 數(shù)據(jù)類型,其實(shí)也幫你自動(dòng)創(chuàng)建了 序列
postgres=# create table t (id serial);
CREATE TABLE
postgres=# \d+ t
Table "public.t"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+------------------------------------------------+---------+--------------+-------------
id | integer | not null default nextval('t_id_seq'::regclass) | plain | |
可以看出焰檩,自動(dòng)創(chuàng)建了 t_id_seq 這個(gè)序列憔涉,同時(shí)添加了 not null 約束。
研究下這個(gè)序列:
postgres=# \d+ t_id_seq
Sequence "public.t_id_seq"
Column | Type | Value | Storage
---------------+---------+---------------------+---------
sequence_name | name | t_id_seq | plain
last_value | bigint | 1 | plain
start_value | bigint | 1 | plain
increment_by | bigint | 1 | plain
max_value | bigint | 9223372036854775807 | plain
min_value | bigint | 1 | plain
cache_value | bigint | 1 | plain
log_cnt | bigint | 0 | plain
is_cycled | boolean | f | plain
is_called | boolean | f | plain
Owned by: public.t.id
- 常見的字符類型
Paste_Image.png
SQL 定義了兩種基本的字符類型析苫,varchar(n)和char(n)兜叨,這里的n是一個(gè)正整數(shù)穿扳。兩種類型都可以存儲(chǔ)最多n個(gè)字符長(zhǎng)的字串,試圖存儲(chǔ)更長(zhǎng)的字串到這些類型的字段里會(huì)產(chǎn)生一個(gè)錯(cuò)誤国旷,除非超出長(zhǎng)度的字符都是空白矛物,這種情況下該字串將被截?cái)酁樽畲箝L(zhǎng)度。如果沒有長(zhǎng)度聲明跪但,char等于char(1)履羞,而varchar則可以接受任何長(zhǎng)度的字串。
char 類型屡久,如果不夠長(zhǎng)度用 空格填充忆首。
注意 無(wú)論是那種字符集,這里和的單位是 字符被环,而不是字節(jié)糙及,和 ORACLE不同。
text:表面是無(wú)限長(zhǎng)度筛欢,其實(shí)最大可以支持到1個(gè)GB(依據(jù)版本而定)
下面都是和字節(jié)有關(guān)的類型浸锨,上面則是和字符有關(guān)的類型
“char” 單字節(jié)的內(nèi)部使用的類型
name 內(nèi)部使用的類型
postgres=# create table t2(c1 varchar(3));
字段 c1 最多允許3個(gè)“字符”,不是字節(jié)0婀谩柱搜!
CREATE TABLE
postgres=# insert into t2 values ('你好呀');
INSERT 0 1
postgres=# insert into t2 values ('abc');
INSERT 0 1
postgres=# insert into t2 values ('abcd');
這時(shí)候就報(bào)錯(cuò)了,因?yàn)閍bcd是4個(gè)字符剥险。
ERROR: value too long for type character varying(3)
postgres=# select pg_column_size (c1),c1 from t2 ;
pg_column_size | c1
----------------+--------
10 | 你好呀
4 | abc
(2 rows)
-常用的事件類型
Paste_Image.png
interval :是一個(gè)時(shí)間間隔類型聪蘸。
Paste_Image.png
時(shí)間的輸出格式
Paste_Image.png
postgres=# show datestyle;
DateStyle
-----------
ISO, MDY
(1 row)
postgres=# select now() ;
now
-------------------------------
2017-05-26 09:32:35.197556+08 指的是8區(qū)
(1 row)
interval 類型:
postgres=# select now()-current_date;
?column?
-----------------
09:35:29.864559
(1 row)
Paste_Image.png
- Boolean 類型