What is the most appropriate data type for storing

2019-01-10 05:26发布

This question already has an answer here:

What should be the most recommended datatype for storing an IPv4 address in SQL server?

Or maybe someone has already created a user SQL data-type (.Net assembly) for it?

I don't need sorting.

15条回答
聊天终结者
2楼-- · 2019-01-10 05:32

For space efficient storage and when the values are to be processed (matched or compared to a range), I use an int. The IP address really is just a 32 bit value.

For a simple solution where you just want to store the value to view it, I use a varchar(15) to store the string representation of the IP adress.

查看更多
成全新的幸福
3楼-- · 2019-01-10 05:36

Best way (when no need sorting and other control on the IPs) is store it as int, storing it as varchar etc. would cost way more performance than just a simple innocent int.

There is a property IPAddress.Address but it's obsolete, I don't know why, since if you don't need sorting or control over the IP classes, the best way is to store it as unsigned integer (that has a max value of 0xffffffff which equals to 255.255.255.255 in decimal representation.

Also the IPAddress class has a constructor that accepts a long argument.

And according to VS debugger visualizer, that IPAddress class itself stores its internal variable as one number (not byte array).

Read more on workarounds storing a unit in MS SQL Server:

查看更多
乱世女痞
4楼-- · 2019-01-10 05:36

I'm newbie @ php,sql , but i think fastest way to store something in sql db is to convert it to int value and save as int.

I used function in php -

function ip_convert() {
    $ip = $_SERVER['REMOTE_ADDR'];
    $intip = str_replace(".","0",$ip);
    return $intip;
}

And then i just replace all dots with zeros. Then if i need use this ip from sql.. if($ip == ip_convert())

But this only if you use PHP.

查看更多
何必那么认真
5楼-- · 2019-01-10 05:38

Regarding this comment in the accepted answer

sorting them is a pain unless you pad zeros.

Here's a trick for SQL Server 2008 (From Itzik Ben-Gan in this book)

with ip_addresses as
(
SELECT '131.33.2.201' AS ip_address UNION ALL
SELECT '2.12.4.4' AS ip_address UNION ALL
SELECT '131.33.2.202' AS ip_address UNION ALL
SELECT '2.12.4.169' AS ip_address UNION ALL
SELECT '131.107.2.201' AS ip_address 
)
select ip_address
from ip_addresses
ORDER  BY CAST('/' + ip_address + '/' AS hierarchyid)

Returns

ip_address
-------------
2.12.4.4
2.12.4.169
131.33.2.201
131.33.2.202
131.107.2.201
查看更多
做自己的国王
6楼-- · 2019-01-10 05:41

I'm reading a lot of similar questions on here, and none of the replies in this one mention the number one answer in others: "For IPv4 addresses, you may want to store them as an int unsigned and use the INET_ATON() and INET_NTOA() functions to return the IP address from its numeric value, and vice versa." I think this is what I'm going to go with in my db, unless I decide to use the php functions mentioned above.

查看更多
该账号已被封号
7楼-- · 2019-01-10 05:47

Storing an IPv4 address as a binary(4) is truest to what it represents, and allows for easy subnet mask-style querying. However, it requires conversion in and out if you are actually after a text representation. In that case, you may prefer a string format.

A little-used SQL Server function that might help if you are storing as a string is PARSENAME, by the way. Not designed for IP addresses but perfectly suited to them. The call below will return '14':

SELECT PARSENAME('123.234.23.14', 1)

(numbering is right to left).

查看更多
登录 后发表回答