tags:

views:

116

answers:

3

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

The ideal would be something that doesn't take up a lot of space but can easily handle variable length URLs.

+6  A: 

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.

Dave Webb
note that VARCHAR as a maximum lenght of 255 chars.
yoda
@yoda - not since MySQL 5.0.3. It can be up to 64k now, depending on what you're storing in the other columns in the row.
Dave Webb
@yoda: "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" (from Dave's link)
AdaTheDev
Way to give an accurate answer to an "empty" question! +1
marcgg
got it donethanks
sangeetha
A: 

I recommend a VARCHAR field and the length is really personal opinion on what kind of URLs you are anticipating storing. You can get away with VARCHAR(255) without increasing the storage requirements for a VARCHAR field without penalty.

CREATE TABLE example(
    id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    url varchar(125)  
);

$url = 'http://www.google.com';
$sql = 'INSERT INTO example SET url = ' . mysql_real_escape_string($url);
cballou
A: 

You can store URLs as simple strings, but be careful when comparing values since URLs may come in various forms while actually representing the same location (for example with/without the initial protocol identifier, or with/without a trailing slash).

Konamiman