>  기사  >  데이터 베이스  >  Mysql 中存储IP地址

Mysql 中存储IP地址

WBOY
WBOY원래의
2016-06-07 16:42:11990검색

本文介绍IP字段在mysql数据表中的优化。 创建测试表 mysql create table hosts( - `id` int(8) not null auto_increment, - `ip` int unsigned not null, - `hostname` varchar(30) not null, - `desc` varchar(100) , - PRIMARY KEY(`id`)) ENGINE=InnoDB;Q

本文介绍IP字段在mysql数据表中的优化。

创建测试表

mysql> create table hosts( 
 -> `id` int(8) not null auto_increment,
 -> `ip` int unsigned not null,
 -> `hostname` varchar(30) not null,
 -> `desc` varchar(100) , 
 -> PRIMARY KEY(`id`)) ENGINE=InnoDB;
Query OK, 0 rows affected (0.03 sec)

查看表结构

mysql> desc hosts
 -> ;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(8) | NO | PRI | NULL | auto_increment |
| ip | int(10) unsigned | NO | | NULL | |
| hostname | varchar(30) | NO | | NULL | |
| desc | varchar(100) | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

插入测试数据

mysql> insert into hosts (`ip`,`hostname`,`desc`) values (inet_aton('192.168.1.106'),'test','this is test host.');
Query OK, 1 row affected (0.03 sec)
mysql> select * from hosts
 -> ;
+----+------------+----------+--------------------+
| id | ip | hostname | desc |
+----+------------+----------+--------------------+
| 1 | 3232235882 | test | this is test host. |
+----+------------+----------+--------------------+
1 row in set (0.00 sec)

查询

mysql> select inet_ntoa(ip) from hosts;
+---------------+
| inet_ntoa(ip) |
+---------------+
| 192.168.1.106 |
+---------------+
1 row in set (0.00 sec)
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.