New in v2.0:INET数据类型存储IPv4或IPv6地址。

语法

INET的常量值可以使用解释文本( interpreted literal)表示,或者使用类型INET注释的字符串文本(a string literal annotated with type INET),或强制类型为INET表示。

INET常量可以使用以下格式表示:

Format Description
IPv4 标准RFC791指定的4个八位字节格式,以十进制数字表示,并以句点分隔。地址之后可选择性添加子网掩码。

示例:'190.0.0.0''190.0.0.0 / 24'
IPv6 标准RFC8200指定格式的8个冒号分隔的4个十六进制数字组。 IPv6地址可以映射到IPv4地址。地址之后可选择性添加子网掩码

示例: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1', '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120', '::ffff:192.168.0.1/24'

IPv4地址将在IPv6地址之前排序,包括IPv4映射的IPv6地址。

大小

使用INET值存储IPv4占用32位,存储IPv6占用128位。

示例

> CREATE TABLE computers (
    ip INET PRIMARY KEY,
    user_email STRING,
    registration_date DATE
  );
> SHOW COLUMNS FROM computers;
+-------------------+--------+-------+---------+-------------+
|       Field       |  Type  | Null  | Default |   Indices   |
+-------------------+--------+-------+---------+-------------+
| ip                | INET   | false | NULL    | {"primary"} |
| user_email        | STRING | true  | NULL    | {}          |
| registration_date | DATE   | true  | NULL    | {}          |
+-------------------+--------+-------+---------+-------------+
> INSERT INTO computers
  VALUES
    ('192.168.0.1', 'info@cockroachlabs.com', '2018-01-31'),
    ('192.168.0.2/10', 'lauren@cockroachlabs.com', '2018-01-31'),
    ('2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120', 'test@cockroachlabs.com', '2018-01-31');
> SELECT * FROM computers;
+--------------------------------------+--------------------------+---------------------------+
|                  ip                  |        user_email        |     registration_date     |
+--------------------------------------+--------------------------+---------------------------+
| 192.168.0.1                          | info@cockroachlabs.com   | 2018-01-31 00:00:00+00:00 |
| 192.168.0.2/10                       | lauren@cockroachlabs.com | 2018-01-31 00:00:00+00:00 |
| 2001:4f8:3:ba:2e0:81ff:fe22:d1f1/120 | test@cockroachlabs.com   | 2018-01-31 00:00:00+00:00 |
+--------------------------------------+--------------------------+---------------------------+

支持的转换

INET值可以转换为以下数据类型:

See Also