PostgreSQL 數(shù)據(jù)類型介紹(一)

  • 查看當(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
  • 常見(jiàn)的數(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

  • 常見(jiàn)的字符類型
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)度。如果沒(méi)有長(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é)??!
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 類型
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容