网络地址类型
GaussDB提供用于存储IPv4、MAC地址的数据类型。
用这些数据类型存储网络地址比用纯文本类型好,因为这些类型提供输入错误检查和特殊的操作和功能(请参见网络地址函数和操作符)。
名称 |
存储空间 |
描述 |
---|---|---|
cidr |
7字节 |
IPv4网络 |
inet |
7字节 |
IPv4主机和网络 |
macaddr |
6字节 |
MAC地址 |
cidr
cidr(无类别域间路由,Classless Inter-Domain Routing)类型,保存一个IPv4网络地址。声明网络格式为address/y,address表示IPv4地址,y表示子网掩码的二进制位数。如果省略y,则掩码部分使用已有类别的网络编号系统进行计算,但要求输入的数据已经包括了确定掩码所需的所有字节。
cidr输入 |
cidr输出 |
abbrev(cidr) |
---|---|---|
192.168.100.128/25 |
192.168.100.128/25 |
192.168.100.128/25 |
192.168/24 |
192.168.0.0/24 |
192.168.0/24 |
192.168/25 |
192.168.0.0/25 |
192.168.0.0/25 |
192.168.1 |
192.168.1.0/24 |
192.168.1/24 |
192.168 |
192.168.0.0/24 |
192.168.0/24 |
10.1.2 |
10.1.2.0/24 |
10.1.2/24 |
10.1 |
10.1.0.0/16 |
10.1/16 |
10 |
10.0.0.0/8 |
10/8 |
10.1.2.3/32 |
10.1.2.3/32 |
10.1.2.3/32 |
示例:
openGauss=# CREATE TABLE cidr_test(id int, c cidr); CREATE TABLE openGauss=# INSERT INTO cidr_test VALUES (1, '192.168.100.128/25'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (2, '192.168/24'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (3, '192.168/25'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (4, '192.168.1'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (5, '192.168'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (6, '10.1.2'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (7, '10.1'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (8, '10'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (9, '2001:4f8:3:ba::/64'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (10, '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (11, '::ffff:127.0.0.0/120'); INSERT 0 1 openGauss=# INSERT INTO cidr_test VALUES (12, '::ffff:127.0.0.0/128'); INSERT 0 1 openGauss=# SELECT * FROM cidr_test ORDER BY id; id | c ----+-------------------------------------- 1 | 192.168.100.128/25 2 | 192.168.0.0/24 3 | 192.168.0.0/25 4 | 192.168.1.0/24 5 | 192.168.0.0/24 6 | 10.1.2.0/24 7 | 10.1.0.0/16 8 | 10.0.0.0/8 9 | 2001:4f8:3:ba::/64 10 | 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128 11 | ::ffff:127.0.0.0/120 12 | ::ffff:127.0.0.0/128 (12 rows) openGauss=# DROP TABLE cidr_test; DROP TABLE
inet
inet类型在一个数据区域内保存主机的IPv4地址,以及一个可选子网。主机地址中网络地址的位数表示子网(“子网掩码”)。如果子网掩码是32并且地址是IPv4,则这个值不表示任何子网,只表示一台主机。
该类型的输入格式是address/y,address表示IPv4地址,y是子网掩码的二进制位数。如果省略/y,则子网掩码对IPv4是32,所以该值表示只有一台主机。如果该值表示只有一台主机,/y将不会显示。
inet和cidr类型之间的基本区别是inet接受子网掩码,而cidr不接受。
示例:
openGauss=# CREATE TABLE inet_test(id int, i inet); CREATE TABLE openGauss=# INSERT INTO inet_test VALUES (1, '192.168.100.128/25'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (2, '192.168.100.128'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (3, '192.168.1.0/24'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (4, '192.168.1.0/25'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (5, '192.168.1.255/24'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (6, '192.168.1.255/25'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (7, '10.1.2.3/8'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (8, '11.1.2.3/16'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (9, '12.1.2.3/24'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (10, '13.1.2.3/32'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (11, '2001:4f8:3:ba::/64'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (12, '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (13, '::ffff:127.0.0.0/120'); INSERT 0 1 openGauss=# INSERT INTO inet_test VALUES (14, '::ffff:127.0.0.0/128'); INSERT 0 1 openGauss=# SELECT * FROM inet_test ORDER BY id; id | i ----+---------------------------------- 1 | 192.168.100.128/25 2 | 192.168.100.128 3 | 192.168.1.0/24 4 | 192.168.1.0/25 5 | 192.168.1.255/24 6 | 192.168.1.255/25 7 | 10.1.2.3/8 8 | 11.1.2.3/16 9 | 12.1.2.3/24 10 | 13.1.2.3 11 | 2001:4f8:3:ba::/64 12 | 2001:4f8:3:ba:2e0:81ff:fe22:d1f1 13 | ::ffff:127.0.0.0/120 14 | ::ffff:127.0.0.0 (14 rows) openGauss=# DROP TABLE inet_test; DROP TABLE
macaddr
macaddr类型存储MAC地址,也就是以太网卡硬件地址(尽管MAC地址还用于其它用途)。可以接受下列格式:
'08:00:2b:01:02:03' '08-00-2b-01-02-03' '08002b:010203' '08002b-010203' '0800.2b01.0203' '08002b010203'
这些示例都表示同一个地址。对于数据位a到f,大小写都行。输出时都是以第一种形式展示。
示例:
openGauss=# CREATE TABLE macaddr_test(id int, m macaddr); CREATE TABLE openGauss=# INSERT INTO macaddr_test VALUES (1, '08:00:2b:01:02:03'); INSERT 0 1 openGauss=# INSERT INTO macaddr_test VALUES (2, '08-00-2b-01-02-03'); INSERT 0 1 openGauss=# INSERT INTO macaddr_test VALUES (3, '08002b:010203'); INSERT 0 1 openGauss=# INSERT INTO macaddr_test VALUES (4, '08002b-010203'); INSERT 0 1 openGauss=# INSERT INTO macaddr_test VALUES (5, '0800.2b01.0203'); INSERT 0 1 openGauss=# INSERT INTO macaddr_test VALUES (6, '08002b010203'); INSERT 0 1 openGauss=# SELECT * FROM macaddr_test ORDER BY id; id | m ----+------------------- 1 | 08:00:2b:01:02:03 2 | 08:00:2b:01:02:03 3 | 08:00:2b:01:02:03 4 | 08:00:2b:01:02:03 5 | 08:00:2b:01:02:03 6 | 08:00:2b:01:02:03 (6 rows) openGauss=# DROP TABLE macaddr_test; DROP TABLE