- Boolean 類型
Paste_Image.png
如圖所示码倦,常見的數(shù)據(jù)類型圖片所示终畅。
- 枚舉(enum)類型
示例如下:
備注:其實(shí)和java里的枚舉一樣宏所。
//創(chuàng)建一個(gè)枚舉類型
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)
-- 輸入一個(gè)不存在的枚舉值, 將報(bào)錯(cuò)
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)錯(cuò)的方法, 把枚舉轉(zhuǎn)換成text
postgres=# SELECT * FROM person WHERE current_mood::text = 'happ';
name | current_mood
------+--------------
(0 rows)
postgres=#
枚舉值每一個(gè)在行中占用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個(gè)字符乙各。
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
---------------------+-----------+-----------
//表名也是一個(gè)name類型, 長度也不能超過63個(gè)字符耳峦。
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)插到最后。
一般盡量插到最后蹲坷,否則會(huì)對性能有影響驶乾。
好比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=#