question

bhanupratapsngh9 avatar image
bhanupratapsngh9 asked

what is best datatype to store IP Address ?

Dear Sir, I want to store ip address i have taken varchar(20) to store it is it best practice?
ip-address
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Fatherjack avatar image
Fatherjack answered
You know your use for the data, and the IP ranges you will be working with better than anyone else so you will need to consider that along with the answers that you get here. Personally I would say that varchar(20) is wrong because you know that the largest IPv4 address will be NNN.NNN.NNN.NNN which is 15 characters, the smallest could be N.N.N.N but would you store it as that or 00N.00N.00N.00N? If you store it with leading zeros then a char(15) could be better than a varchar(15). Things to consider - will you be wanting to join on subnet values? Will you be searching this table by specific IP address or ranges or will record access be carried out on other column values and the IP address will simply be data that is stored/displayed but not used to search for specific records?
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.