What is the best column type for URL?

纵饮孤独 提交于 2019-11-30 00:19:39

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.

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

Will you be storing multilingual URLs? If so, use nvarchar, otherwise use 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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!