- 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=#