Best Database Field Type for a Url

Best database field type for a URL


  1. Lowest common denominator max URL length among popular web browsers: 2,083 (Internet Explorer)

  1. http://dev.mysql.com/doc/refman/5.0/en/char.html

    Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 255 before MySQL 5.0.3, and 0 to 65,535 in 5.0.3 and later versions. The effective maximum length of a VARCHAR in MySQL 5.0.3 and later is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.

  1. So ...

    < MySQL 5.0.3 use TEXT

    or

    >= MySQL 5.0.3 use VARCHAR(2083)

What is the best datatype for storing URLs in a MySQL database?

If by "links" you mean links to web pages, I'm guessing you want to store URLs.

Since URLs are variable length strings the VARCHAR data type would seem the obvious choice.

What is the best column type for URL?

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

What data type does a URL correspond to in MySQL?

Simply put the data type should be VARCHAR

URLs can contain any number of characters, and can be any length (within reason on the smaller end). A CHAR field can only contain the number of characters that is set in the table definition. A VariableCharacter (VARCHAR) field can contain a variable number of characters. So since not all URL's are of equal length you need the variability. You could make an argument to use a TEXT field if you needed to store really long URLs; however, for most use cases VARCHAR will suffice.

What is the best datatype for storing URLs in a POSTGRESSQL database IN RAILS?

Datatype depends upon the length of your URL.

If your url fits in 255 characters(which is default max limit set by rails in migration for string column), you can use string datatype(varchar in Postgresql).

If you think, your urls can be greater than 255 characters, then blindly go for text, it will fit all urls.

What's the proper column type to save urls in MySQL?

Use TEXT, it's enough for every URL.

Note that with long URLs, you won't be able to create an index that covers the whole URL. If you need a UNIQUE index, you should calculate the URL hash, store the hash separately and index the hash instead.

Database field type for storing web pages

You'd want to use VARBINARY(MAX) or NVARCHAR(MAX), depending on the type of data being stored and what you want to do with it. If you going to store files (which it sounds like, especially with mixed extensions), use VARBINARY(MAX). You can full-text index off that data type too -- although PDF's require an additional iFilter (at least it did with our SQL Server 2005 instance -- it may be there by default in 2008).

Keep in mind that you don't want to use IMAGE, as that data type (along with TEXT and NTEXT) is deprecated and is being removed in a future version of SQL Server. Here's the link about that.

Hope this helps.

Which SQL Data Type can accommodate links

Any of the string data types (e.g. varchar, nvarchar, etc) can accomodate the href attributes from links. And another string column can accommodate the inner text. So something like:

CREATE TABLE T (
--Various columns
LinkHref nvarchar(max) not null,
LinkBody nvarchar(max) not null
)

Then, construct the surrounding anchor tag when you're extracting them for display. This also makes the links more searchable in the database in the future.

Best database field type for CONTENT_LENGTH Http Header

No, you shouldn't be storing as VARCHAR type cause it's Numeric type.

As per W3C RFC 2616-sec14.13 specification

The Content-Length entity-header field indicates the size of the
entity-body, in decimal number of OCTETs, sent to the recipient or, in
the case of the HEAD method, the size of the entity-body that would
have been sent had the request been a GET.

An example is

   Content-Length: 3495

You can see the full spec here Content-Length

A look here for complete list of SQL Server supported data types Data Types (Transact-SQL)

So essentially you should be storing as INT or BIGINT data type in SQL SERVER



Related Topics



Leave a reply



Submit