PostgreSQL数据类型:网络地址类型

前端之家收集整理的这篇文章主要介绍了PostgreSQL数据类型:网络地址类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

感谢原作者整理分享。在此mark一下。

官方文档:http://www.postgresql.org/docs/9.4/interactive/datatype-net-types.html

一、cidr

postgres=#createtabletest(idint,nametext);
CREATETABLE
postgres=#\dtest
Table"public.test"
Column|Type|Modifiers
--------+---------+-----------
id|integer|
name|text|

postgres=#altertabletestaddcolumnipcidr;
ALTERTABLE
postgres=#\dtest
Table"public.test"
Column|Type|Modifiers
--------+---------+-----------
id|integer|
name|text|
ip|cidr|
postgres=#insertintotestvalues(1,'a','192.168.1.100');
INSERT01
postgres=#select*fromtest;
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
(1row)
postgres=#insertintotestvalues(2,'b','192.168.0.0/16');
INSERT01
postgres=#select*fromtest;
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
2|b|192.168.0.0/16
(2rows)
postgres=#insertintotestvalues(3,'c','192.168.1.0/24');
INSERT01
postgres=#select*fromtest;
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
2|b|192.168.0.0/16
3|c|192.168.1.0/24
(3rows)

查询使用

postgres=#select*fromtestwhereip='192.168.1.100';
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
(1row)
postgres=#select*fromtestwhereip>='192.168.1.0/24';
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
3|c|192.168.1.0/24
(2rows)

postgres=#select*fromtestwhereip>='192.168.0.0/16';
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
2|b|192.168.0.0/16
3|c|192.168.1.0/24
(3rows)
postgres=#updatetestsetip='192.168.1.101/32'whereid=2;
UPDATE1
postgres=#updatetestsetip='192.168.1.102/32'whereid=3;
UPDATE1
postgres=#select*fromtest;
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
2|b|192.168.1.101/32
3|c|192.168.1.102/32
(3rows)
postgres=#select*fromtestwhereipbetween'192.168.1.100'and'192.168.1.101';
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
2|b|192.168.1.101/32
(2rows)

postgres=#select*fromtestwhereipbetween'192.168.1.100'and'192.168.1.102';
id|name|ip
----+------+------------------
1|a|192.168.1.100/32
2|b|192.168.1.101/32
3|c|192.168.1.102/32
(3rows)

二、inet

将cidr修改为inet

postgres=#\dtest
Table"public.test"
Column|Type|Modifiers
--------+---------+-----------
id|integer|
name|text|
ip|cidr|
postgres=#altertabletestaltercolumniptypeinet;
ALTERTABLE
postgres=#\dtest
Table"public.test"
Column|Type|Modifiers
--------+---------+-----------
id|integer|
name|text|
ip|inet|
postgres=#select*fromtest;
id|name|ip
----+------+---------------
1|a|192.168.1.100
2|b|192.168.1.101
3|c|192.168.1.102
(3rows)

postgres=#updatetestsetip='192.168.0.0/16'whereid=3;
UPDATE1
postgres=#select*fromtest;
id|name|ip
----+------+----------------
1|a|192.168.1.100
2|b|192.168.1.101
3|c|192.168.0.0/16
(3rows)

postgres=#updatetestsetip='192.168.1.0/24'whereid=2;
UPDATE1
postgres=#select*fromtest;
id|name|ip
----+------+----------------
1|a|192.168.1.100
3|c|192.168.0.0/16
2|b|192.168.1.0/24
(3rows)

可见,inet默认32位掩码的ip是不带'/32'的

postgres=#select*fromtestwhereip>='192.168.1.100';
id|name|ip
----+------+---------------
1|a|192.168.1.100
(1row)

postgres=#select*fromtestwhereip>='192.168.1.1';
id|name|ip
----+------+---------------
1|a|192.168.1.100
(1row)

postgres=#select*fromtestwhereip>='192.168.1.101';
id|name|ip
----+------+----
(0rows)
postgres=#select*fromtestwhereip>='192.168.1.0/32';
id|name|ip
----+------+---------------
1|a|192.168.1.100
(1row)
postgres=#select*fromtestwhereip>='192.168.1.0/16';
id|name|ip
----+------+----------------
1|a|192.168.1.100
2|b|192.168.1.0/24
(2rows)

postgres=#select*fromtestwhereip>='192.168.0.0/16';
id|name|ip
----+------+----------------
1|a|192.168.1.100
3|c|192.168.0.0/16
2|b|192.168.1.0/24
(3rows)

使用跟cidr差不多

三、macaddr

postgres=#\dtest
Table"public.test"
Column|Type|Modifiers
--------+---------+-----------
id|integer|
name|text|
ip|inet|

postgres=#altertabletestaddcolumnmacmacaddr;
ALTERTABLE
postgres=#\dtest
Table"public.test"
Column|Type|Modifiers
--------+---------+-----------
id|integer|
name|text|
ip|inet|
mac|macaddr|

postgres=#select*fromtest;
id|name|ip|mac
----+------+----------------+-----
1|a|192.168.1.100|
3|c|192.168.0.0/16|
2|b|192.168.1.0/24|
(3rows)
postgres=#updatetestsetmac='08:00:2b:01:02:03'whereid=1;
UPDATE1
postgres=#select*fromtest;
id|name|ip|mac
----+------+----------------+-------------------
3|c|192.168.0.0/16|
2|b|192.168.1.0/24|
1|a|192.168.1.100|08:00:2b:01:02:03
(3rows)
postgres=#updatetestsetmac='08:00:2b:01:02:04'whereid=2;
UPDATE1
postgres=#updatetestsetmac='08:00:2b:01:02:05'whereid=3;
UPDATE1
postgres=#select*fromtest;
id|name|ip|mac
----+------+----------------+-------------------
1|a|192.168.1.100|08:00:2b:01:02:03
2|b|192.168.1.0/24|08:00:2b:01:02:04
3|c|192.168.0.0/16|08:00:2b:01:02:05
(3rows)
查询使用
postgres=#select*fromtestwheremac='08:00:2b:01:02:03';
id|name|ip|mac
----+------+---------------+-------------------
1|a|192.168.1.100|08:00:2b:01:02:03
(1row)

postgres=#select*fromtestwheremac>'08:00:2b:01:02:03';
id|name|ip|mac
----+------+----------------+-------------------
2|b|192.168.1.0/24|08:00:2b:01:02:04
3|c|192.168.0.0/16|08:00:2b:01:02:05
(2rows)

Postgresql默认还不支持iprange,需要安装ip4r的扩展,详见:http://pgfoundry.org/projects/ip4r/

原文链接:https://www.f2er.com/postgresql/195016.html

猜你在找的Postgre SQL相关文章