- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Which MySQL datatype to used to store an IP address?
We can store an IP address with the help of INT unsigned. While using INSERT, include INET_ATON() and with SELECT, include INET_NTOA(). IP address is in dotted format.
Let us see an example.
Creating a table.
mysql> create table IPV4AddressDemo -> ( -> `IPV4Address` INT UNSIGNED -> ); Query OK, 0 rows affected (0.52 sec)
Inserting IP address into the table, with INET_ATON.
mysql> insert into IPV4AddressDemo values(INET_ATON("18.104.22.168")); Query OK, 1 row affected (0.17 sec)
To display all records.
mysql> select *from IPV4AddressDemo;
The following is the output, but definitely we want it to be in IP address format.
+-------------+ | IPV4Address | +-------------+ | 2013265921 | +-------------+ 1 row in set (0.00 sec)
As the above output is giving a sequence of integers, but we can convert them into the original IP address format. For that, use INET_NTOA
mysql> SELECT INET_NTOA(`IPV4Address`) FROM IPV4AddressDemo;
The following is the output that shows IP address in the actual format.
+--------------------------+ | INET_NTOA(`IPV4Address`) | +--------------------------+ | 22.214.171.124 | +--------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to hide an IP address
- MySQL datatype to store month and year only?
- C Program to validate an IP address
- For timestamp, which datatype is used in MySQL?
- What is the MySQL datatype to store DATALINK object in JDBC
- Which MySQL Datatype should be used for storing BloodType?
- Defanging an IP Address in Python
- Which MySQL Data Type can be used to store Negative Number?
- Difference between Static IP Address and Dynamic IP Address
- Python program to remove leading zeros from an IP address
- MySQL isn’t inserting binary data properly? Which datatype should be used?
- Order a column in MySQL with IP Address records?
- Python program to remove leading 0's from an IP address
- MySQL query to copy IP address from varchar column to integer in the same table?