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

  • Boolean 類型
Paste_Image.png

如圖所示,常見的數(shù)據(jù)類型圖片所示。

  • 枚舉(enum)類型
    示例如下:

備注:其實(shí)和java里的枚舉一樣。

//創(chuàng)建一個枚舉類型
postgres=# CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TYPE
//創(chuàng)建 person 表,并使用該枚舉。
postgres=# CREATE TABLE person (name text,current_mood mood);
CREATE TABLE
//插入一條記錄:
postgres=# INSERT INTO person VALUES ('Moe', 'happy');
INSERT 0 1
//查詢記錄
postgres=# SELECT * FROM person WHERE current_mood = 'happy';
 name | current_mood 
------+--------------
 Moe  | happy
(1 row)


-- 輸入一個不存在的枚舉值, 將報(bào)錯
postgres=# SELECT * FROM person WHERE current_mood = 'happ';
ERROR:  invalid input value for enum mood: "happ"
LINE 1: SELECT * FROM person WHERE current_mood = 'happ';
-- 避免報(bào)錯的方法, 把枚舉轉(zhuǎn)換成text     
postgres=# SELECT * FROM person WHERE current_mood::text = 'happ';
 name | current_mood 
------+--------------
(0 rows)

postgres=# 

枚舉值每一個在行中占用4 bytes :
postgres=# select current_mood,pg_column_size(current_mood) from person;
current_mood | pg_column_size
--------------+----------------
happy | 4
枚舉的標(biāo)簽在定義中最大限制由NAMEDATALEN決定, 默認(rèn)是64-1=63. 前面已經(jīng)講過. 意味著 枚舉值不能超過 63個字符。
postgres=# \d pg_enum
    Table "pg_catalog.pg_enum"
    Column     | Type | Modifiers 
---------------+------+-----------
 enumtypid     | oid  | not null
 enumsortorder | real | not null
 enumlabel     | name | not null
//表名的長度也是NAMEDATALEN決定的。
postgres=# \d pg_class ;
         Table "pg_catalog.pg_class"
       Column        |   Type    | Modifiers 
---------------------+-----------+-----------
//表名也是一個name類型, 長度也不能超過63個字符。
 relname             | name      | not null
 relnamespace        | oid       | not null


查找枚舉的數(shù)據(jù)結(jié)構(gòu) :
postgres=# select oid,typname from pg_type where typname='mood';
oid | typname
---------+---------
3952969 | mood
postgres=# select * from pg_enum where enumtypid=3952969;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
3952969 | 1 | sad
3952969 | 2 | ok
3952969 | 3 | happy

枚舉類型變更(枚舉類型的插入和刪除)

ALTER TYPE name ADD VALUE new_enum_value [ { BEFORE | AFTER } existing_enum_value ]

This form adds a new value to an enum type. If the new value's place in the enum's ordering is not specified using BEFORE or AFTER, then the
new item is placed at the end of the list of values.

如果不指定 Before 和 AFTER 的話,默認(rèn)插到最后。
一般盡量插到最后,否則會對性能有影響。
好比java里的集合吧,有序集合插到最后屬組開銷較小。

  • money類型
postgres=# show lc_monetary;
 lc_monetary 
-------------
 C
(1 row)

//將12.345換算成money
postgres=# select '12.345'::money;
 money  
--------
 $12.35
(1 row)

postgres=# show lc_monetary;
 lc_monetary 
-------------
 C
(1 row)

關(guān)于 monetary ,都是有固定格式的。
//重新設(shè)置參數(shù) lc_monetary
postgres=# set lc_monetary='zh_CN';
SET
//將12.345換算成money
postgres=# select '12.345'::money;
  money  
---------
 ¥12.35
(1 row)

postgres=# 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,692評論 18 399
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,493評論 2 7
  • 說到寫作,平時點(diǎn)滴的積累是很重要的,每天在你靈感閃現(xiàn)的時候,在生活有所感悟的時候,在看到優(yōu)美的字句的時候,記錄下來...
    愛吃愛玩的悠嘻猴閱讀 262評論 1 1
  • 昨天聊了,我和幾個朋友相同項(xiàng)目的不同收入。催使我產(chǎn)生做《微排記》的想法。我這個人是一個說做就做的人,今天把我這幾年...
    3羊集團(tuán)閱讀 326評論 0 1

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