Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

int

mysql

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).

like image 526
WildBill Avatar asked Dec 04 '13 15:12

WildBill


2 Answers

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

like image 156
Alex Vogt Avatar answered Oct 09 '22 12:10

Alex Vogt


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.

like image 45
Ike Walker Avatar answered Oct 09 '22 10:10

Ike Walker