您的位置:首页 > 数据库

PostgreSQL 数据类型介绍(三)

2017-07-02 21:14 489 查看
bytea类型

该类型存储的是一个个的字节流,也就是这个类型什么都可以存储。

比如,你要想在字符串里存空字符就没法存储。

类似于java里的字节流吧,可以存储任何类型。



The bytea data type allows storage of binary strings

bytea 类型 允许存储二进制串

A binary string is a sequence of octets (or bytes)

这里的binary string 是一串 octets ,也就是存储最小单位是 octet.

备注: octets 和 byte 的区别

最近项目中写文档,由于跟老外合作,所有都是英文的。经常遇到octet这个词,我只是知道byte是一个由8 bits构成的字节,那么octet是什么呢?原来,不同计算机中字节的长度不同,为了不引起歧义,用octet专指8 bits构成的字节。

bytea与字符类型的区别

binary strings specifically allow storing octets of value zero and other “non-printable” octets.

个人理解:

bytea可以允许存储 大小为0个octets的二进制串,或者其余的无法打印出来的二进制串。

但是 字符类型却不可以。

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的二进制对象,同样不允许存储那些 数据库字符集不允许存在(无法打印的)字符。

比如说 你发明了一个文字,这个文字你的字符集里并没有,那么你就不能用字符串存储这个文字,你可以将其存储为 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.

关于如何使用 bytea的问题,我会写一篇独立博客来阐述的

几何类型



可以存储的几何类型有:

*依次为: 点、线、线段、矩阵、路径(可以是闭环) 路径(打开的环)

任意几何类型类似于 闭环)、 圆 *

网络地址类型



支持

cidr 、 inet (可以存储主机,也可以存储网络)、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)
//可以通过创建一个序列,填充区间内的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,不符合定长,所以报错。
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)  强制转换后,虽然长度不够,但是还是强制在末尾添加一个0,存入成功。
postgres=# SELECT * FROM test;
a  |  b
-----+-----
101 | 00
100 | 101
(2 rows)


全文检索类型

其实里面包含了两个类型

tsvector

去除重复分词后按分词顺序存储

可以存储位置信息和权重信息

tsquery

存储查询的分词, 可存储权重信息

关于全文检索类型如何使用,我会有一篇专门的博客来解释的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: