We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


Posted By

zingo on 04/09/08


Tagged

mysql database data ip4 optmize


Versions (?)


Storing an IP address in a database table


Published in: SQL 


URL: http://arjen-lentz.livejournal.com/44290.html

You can store an IP address in an INT UNSIGNED (4 bytes) which is of course much more efficient and faster than a CHAR(15).
MySQL has two built-in functions: INET_ATON() and INET_NTOA(). These two functions are used allover the place in any TCP/IP stack implementation or even application.
The INET_ATON() function converts Internet addresses from the numbers-and-dots notation into a 32-bit unsigned integer, and INET_NTOA() does the opposite.


  1. SELECT INET_ATON('192.168.0.10') AS ipn;
  2. SELECT INET_NTOA(3232235530) AS ipa;
  3. INSERT INTO tbl VALUES (..., INET_ATON('192.168.0.10'), ...);

Report this snippet 

You need to login to post a comment.