URL的最佳列类型是什么?

时间:2021-11-25 22:18:39

What is the best column type for a URL field for SQL Server?

SQL Server的URL字段的最佳列类型是什么?

Type: VARCHAR or NVARCHAR?

类型:VARCHAR或NVARCHAR吗?

Length?

长度呢?

Similar question for MySQL.

类似的问题为MySQL。

4 个解决方案

#1


45  

If you are prepared to always URL encode your URLs before you store them (an example turned up by Google was 中.doc URL encoding to %E4%B8%AD.doc) then you are safe sticking with varchar. If you want the non-ASCII characters in your URLs to remain readable in the database then I'd recommend nvarchar. If you don't want to be caught out, then go for nvarchar.

如果你准备总是存储在URL编码的URL之前(谷歌中出现的一个例子。文档URL编码到% e4% % b8% .doc)那么您就可以安全地使用varchar了。如果您希望url中的非ascii字符在数据库中保持可读性,那么我建议使用nvarchar。如果你不想被抓住,那就去nvarchar。

Since IE (the most restrictive of the mainstream browsers) doesn't support URLs longer than 2083 characters, then (apart from any considerations you might have on indexing or row length), you can cover most useful scenarios with nvarchar(2083).

由于IE(主流浏览器中最受限制的浏览器)不支持超过2083个字符的url,因此(除了在索引或行长度上的考虑),您可以使用nvarchar(2083)来覆盖最有用的场景。

#2


10  

Will you be storing multilingual URLs? If so, use nvarchar, otherwise use varchar.

您将存储多语言url吗?如果是,使用nvarchar,否则使用varchar。

Edit: As for length, since IE limits URLs to being 2,083 characters you could use that as the maximum length of your field. In cases like these you want to use the lower common denominator as your URLs should be usable in all browsers. Really this is a practical cap on a field that most likely will never contain data the will get anywhere close to even IE's limits.

编辑:至于长度,由于IE将url限制为2083个字符,您可以将其作为字段的最大长度。在这种情况下,您希望使用较低的公分母,因为您的url在所有浏览器中都应该是可用的。实际上,这是对一个极有可能永远不会包含数据的领域的一个实际的限制,即使是IE的极限也不会包含数据。

#3


3  

For something like that I'd always err on the side of caution and use the nvarchar.

对于这样的事情,我总是会犯错误,小心使用nvarchar。

#4


2  

For SQL Server, you'll be wanting to use NVARCHAR I'd have thought, as there are plans (if not action already) afoot for non-Roman characters in URLs. I can't really see any problems these days in the extra storage requirements for NVARCHAR over VARCHAR.

对于SQL Server,您可能想要使用NVARCHAR,因为对于url中的非罗马字符,正在计划(如果还没有行动的话)。这些天我没有看到任何问题在VARCHAR上的NVARCHAR的额外存储需求。

#1


45  

If you are prepared to always URL encode your URLs before you store them (an example turned up by Google was 中.doc URL encoding to %E4%B8%AD.doc) then you are safe sticking with varchar. If you want the non-ASCII characters in your URLs to remain readable in the database then I'd recommend nvarchar. If you don't want to be caught out, then go for nvarchar.

如果你准备总是存储在URL编码的URL之前(谷歌中出现的一个例子。文档URL编码到% e4% % b8% .doc)那么您就可以安全地使用varchar了。如果您希望url中的非ascii字符在数据库中保持可读性,那么我建议使用nvarchar。如果你不想被抓住,那就去nvarchar。

Since IE (the most restrictive of the mainstream browsers) doesn't support URLs longer than 2083 characters, then (apart from any considerations you might have on indexing or row length), you can cover most useful scenarios with nvarchar(2083).

由于IE(主流浏览器中最受限制的浏览器)不支持超过2083个字符的url,因此(除了在索引或行长度上的考虑),您可以使用nvarchar(2083)来覆盖最有用的场景。

#2


10  

Will you be storing multilingual URLs? If so, use nvarchar, otherwise use varchar.

您将存储多语言url吗?如果是,使用nvarchar,否则使用varchar。

Edit: As for length, since IE limits URLs to being 2,083 characters you could use that as the maximum length of your field. In cases like these you want to use the lower common denominator as your URLs should be usable in all browsers. Really this is a practical cap on a field that most likely will never contain data the will get anywhere close to even IE's limits.

编辑:至于长度,由于IE将url限制为2083个字符,您可以将其作为字段的最大长度。在这种情况下,您希望使用较低的公分母,因为您的url在所有浏览器中都应该是可用的。实际上,这是对一个极有可能永远不会包含数据的领域的一个实际的限制,即使是IE的极限也不会包含数据。

#3


3  

For something like that I'd always err on the side of caution and use the nvarchar.

对于这样的事情,我总是会犯错误,小心使用nvarchar。

#4


2  

For SQL Server, you'll be wanting to use NVARCHAR I'd have thought, as there are plans (if not action already) afoot for non-Roman characters in URLs. I can't really see any problems these days in the extra storage requirements for NVARCHAR over VARCHAR.

对于SQL Server,您可能想要使用NVARCHAR,因为对于url中的非罗马字符,正在计划(如果还没有行动的话)。这些天我没有看到任何问题在VARCHAR上的NVARCHAR的额外存储需求。