What is the optimal way to store port numbers in a MySQL database?

Viewed 2313

If I want to store a port number in MySQL, what is the most efficient way (minimizing wasted space) to do so? Is it INT(5) or INT(3)?

I'm looking here and I think the answer is INT(3) (or perhaps MEDIUMINT).

2 Answers

Port number is an unsinged 16-bit integer. That means the highest value can be 65535. So you can use SMALLINT.

If you want to store the values as compactly as possible, the choice would be SMALLINT UNSIGNED which takes 2 bytes of storage.

See the manual for details.

Also FYI INT(5) and INT(3) both use 4 bytes of storage. The (5) and (3) are only used for display, not for storage.

Related