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

  • bytea類型

該類型存儲的是一個個的字節(jié)流,也就是這個類型什么都可以存儲。
比如,你要想在字符串里存空字符就沒法存儲。

類似于java里的字節(jié)流吧,可以存儲任何類型。

Paste_Image.png

The bytea data type allows storage of binary strings
bytea 類型 允許存儲二進(jìn)制串
A binary string is a sequence of octets (or bytes)

這里的binary string 是一串 octets ,也就是存儲最小單位是 octet.

備注: octets 和 byte 的區(qū)別

最近項(xiàng)目中寫文檔,由于跟老外合作,所有都是英文的。經(jīng)常遇到octet這個詞,我只是知道byte是一個由8 bits構(gòu)成的字節(jié),那么octet是什么呢?原來,不同計(jì)算機(jī)中字節(jié)的長度不同,為了不引起歧義,用octet專指8 bits構(gòu)成的字節(jié)。

bytea與字符類型的區(qū)別

binary strings specifically allow storing octets of value zero and other "non-printable" octets.
個人理解:
bytea可以允許存儲 大小為0個octets的二進(jìn)制串,或者其余的無法打印出來的二進(jìn)制串。
但是 字符類型卻不可以。

Character strings disallow zero octets, and also disallow any other octet values and sequences of octet values that are invalid according to the database's selected character set encoding.
字符串類型不允許存儲 大小為 0個 octet的二進(jìn)制對象,同樣不允許存儲那些 數(shù)據(jù)庫字符集不允許存在(無法打印的)字符。
比如說 你發(fā)明了一個文字,這個文字你的字符集里并沒有,那么你就不能用字符串存儲這個文字,你可以將其存儲為 bytea類型。 不懂可以留言。
Second, operations on binary strings process the actual bytes, whereas the processing of character strings depends on locale settings. In short, binary strings are appropriate for storing data that the programmer thinks of as "raw bytes", whereas character strings are appropriate for storing text.

關(guān)于如何使用 bytea的問題,我會寫一篇獨(dú)立博客來闡述的

-幾何類型

幾何類型概述.png

可以存儲的幾何類型有:

依次為: 點(diǎn)、線、線段、矩陣、路徑(可以是閉環(huán)) 路徑(打開的環(huán))
任意幾何類型類似于 閉環(huán))、 圓 。

  • 網(wǎng)絡(luò)地址類型
網(wǎng)絡(luò)地址類型

支持
cidr 、 inet (可以存儲主機(jī),也可以存儲網(wǎng)絡(luò))、macaddr

postgres=# create table tbl_ip_info (id integer , province character varying(10),start_ip inet,end_ip inet);
CREATE TABLE
postgres=# 
postgres=# 
postgres=# insert into tbl_ip_info values (1,'浙江','192.168.1.254','192.168.2.5');
INSERT 0 1
postgres=# insert into tbl_ip_info values (2,'廣東','192.168.2.254','192.168.3.5');
INSERT 0 1
postgres=# insert into tbl_ip_info values (3,'湖南','192.168.3.254','192.168.4.5');
INSERT 0 1
postgres=# select * from tbl_ip_info ;
 id | province |   start_ip    |   end_ip    
----+----------+---------------+-------------
  1 | 浙江     | 192.168.1.254 | 192.168.2.5
  2 | 廣東     | 192.168.2.254 | 192.168.3.5
  3 | 湖南     | 192.168.3.254 | 192.168.4.5
(3 rows)
//可以通過創(chuàng)建一個序列,填充區(qū)間內(nèi)的IP 
postgres=#  select id,generate_series(0,end_ip-start_ip)+start_ip
postgres-# from tbl_ip_info ;
 id |   ?column?    
----+---------------
  1 | 192.168.1.254
  1 | 192.168.1.255
  1 | 192.168.2.0
  1 | 192.168.2.1
  1 | 192.168.2.2
  1 | 192.168.2.3
  1 | 192.168.2.4
  1 | 192.168.2.5
  2 | 192.168.2.254
  2 | 192.168.2.255
  2 | 192.168.3.0
  2 | 192.168.3.1
  2 | 192.168.3.2
  2 | 192.168.3.3
  2 | 192.168.3.4
  2 | 192.168.3.5
  3 | 192.168.3.254
  3 | 192.168.3.255
  3 | 192.168.4.0
  3 | 192.168.4.1
  3 | 192.168.4.2
  3 | 192.168.4.3
  3 | 192.168.4.4
  3 | 192.168.4.5
(24 rows)

  • 比特類型: 支持變長的比特類型和定長的比特類型

Bit strings are strings of 1's and 0's. They can be used to store or visualize bit masks. There are two SQL bit types: bit(n) and bit
varying(n), where n is a positive integer.

a字段為定長的類型、b為可變長度。
postgres=# CREATE TABLE test (a BIT(3), b BIT VARYING(5));
CREATE TABLE


postgres=# INSERT INTO test VALUES (B'101', B'00');
INSERT 0 1

備注: a字段為固定長度, B'10' 為長度為2,不符合定長,所以報(bào)錯。
postgres=# INSERT INTO test VALUES (B'10', B'101');
ERROR:  bit string length 2 does not match type bit(3)

postgres=# INSERT INTO test VALUES (B'10'::bit(3), B'101');
INSERT 0 1
備注:B'10'::bit(3)  強(qiáng)制轉(zhuǎn)換后,雖然長度不夠,但是還是強(qiáng)制在末尾添加一個0,存入成功。
postgres=# SELECT * FROM test;
  a  |  b  
-----+-----
 101 | 00
 100 | 101
(2 rows)

  • 全文檢索類型
    其實(shí)里面包含了兩個類型
  1. tsvector
    去除重復(fù)分詞后按分詞順序存儲
    可以存儲位置信息和權(quán)重信息
  2. tsquery
    存儲查詢的分詞, 可存儲權(quán)重信息

關(guān)于全文檢索類型如何使用,我會有一篇專門的博客來解釋的。

最后編輯于
?著作權(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)容

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