Mysql 中存储IP地址

WBOY
Lepaskan: 2016-06-07 16:42:11
asal
1025 orang telah melayarinya

本文介绍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)
Salin selepas log masuk

查看表结构

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)
Salin selepas log masuk

插入测试数据

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)
Salin selepas log masuk
mysql> select * from hosts
 -> ;
+----+------------+----------+--------------------+
| id | ip | hostname | desc |
+----+------------+----------+--------------------+
| 1 | 3232235882 | test | this is test host. |
+----+------------+----------+--------------------+
1 row in set (0.00 sec)
Salin selepas log masuk

查询

mysql> select inet_ntoa(ip) from hosts;
+---------------+
| inet_ntoa(ip) |
+---------------+
| 192.168.1.106 |
+---------------+
1 row in set (0.00 sec)
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan