views:

3066

answers:

8

I need to store a url in a MySQL table. What's the best practice for defining a field that will hold a URL with an undetermined length?

+9  A: 

VARCHAR(512) (or similar) should be sufficient. However, since you don't really know the maximum length of the URLs in question, I might just go direct to TEXT. The danger with this is of course loss of efficiency due to CLOBs being far slower than a simple string datatype like VARCHAR.

Daniel Spiewak
The answer below is sooo much better.
ftrotter
+3  A: 

varchar(max) for SQLServer2005

varchar(65535) for MySQL 5.0.3 and later

This will allocate storage as need and shouldn't affect performance.

TrickyNixon
In your snippet, is `max` a magic ANSI SQL specifier to grow the VARCHAR size as necessary, or is it just a meta-variable for the sake of example?
Daniel Spiewak
It's a SQL2005 syntax. Editing . . .
TrickyNixon
In MySQL you most likely can't have a varchar that large unless it is the only column in the table.
carson
+1  A: 

Most browsers will let you put very large amounts of data in a URL and thus lots of things end up creating very large URLs so if you are talking about anything more than the domain part of a URL you will need to use a TEXT column since the VARCHAR/CHAR are limited.

carson
+1  A: 

I don't know about other browsers, but IE7 has a 2083 character limit for HTTP GET operations. Unless any other browsers have lower limits, I don't see why you'd need any more characters than 2083.

matt b
A: 

Most web servers have a URL length limit (which is why there is an error code for "URI too long"), meaning there is a practical upper size. Find the default length limit for the most popular web servers, and use the largest of them as the field's maximum size; it should be more than enough.

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

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

  3. So ...
    < MySQL 5.0.3 use TEXT
    or
    >= MySQL 5.0.3 use VARCHAR(2083)

micahwittman
Good answer, but personaly I would limit the length. Depending on the project you might want to limit the accepted urls. Who uses url longet than 200?
John
+1  A: 

See the below URL for the size limits of the popular browsers and web servers. http://www.boutell.com/newfaq/misc/urllength.html

A: 

Hello, I created a MYSQL table and stored a hyperlink in the 'url' field in my table. Would you please explain to me:- how can display the hyperlink in a web form when it's connected to the 'url' field in my table.

For example: If url: http://www.w3schools.com, how can I show the hyperlink as well as go to that the website when the user clicks the url?

I use Dreamweaver CS4, PHP and MYSQL Database to display the web form.

Thank you very much for the help.

ja
Answers should not be questions. Either add a comment or start your own question.
Jimmy Cuadra