Which MySQL datatype use for store an IP address?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog I will show you which mysql data type use for store an ip address on database table. Here if we want to store IP address in our mysql database then we don’t mistake to use varchar datatype because if we can use INT UNSIGNED 4(BYTE) datatype. By using the integer datatype we can save more space in our database.

So when we need to fire a insert query on that time by use INET_ATON() and then select query at that time INET_NTOA() use. Now how to use this function I will give in bellow example.

Create Table :

CREATE TABLE IF NOT EXISTS `ip_addresses` (

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,

  `ip_address` INT(4) UNSIGNED NOT NULL,

  PRIMARY KEY (`id`)

);

Insert Data :

INSERT INTO `ip_addresses` (`ip_address`) VALUES (INET_ATON("127.0.0.1"));

Select Data :

SELECT id, INET_NTOA(`ip_address`) as ip FROM `ip_addresses`;

Read Also : Best 10 AngularJS Development Company

Thanks for read this, I hope it will you. You can also follow us on Facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →