What is the best column type for a URL field for SQL Server?
Type: VARCHAR or NVARCHAR?
Length?
Similar question for MySQL.
A Data URL is a string that starts with data: followed by the MIME type format. For example a PNG image has mime type image/png . This is followed by a comma and then by the actual data. Text is usually transferred in plain text, while binary data is usually base64 encoded.
Since URLs are variable length strings the VARCHAR data type would seem the obvious choice.
In most circumstances, VARCHAR provides better performance, it's more flexible, and can be fully indexed. If you need to store longer strings, use MEDIUMTEXT or LONGTEXT, but be aware that very large amounts of data can be stored in columns of these types.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With