tags:

views:

1286

answers:

5

I’m in need of some clarification of the maximum length of a varchar field in MySQL.

I’ve always thought the max length was 255 (255 what? Characters I’ve assumed, but this might be a source of my confusion). Taking a look at the tables of a database set up by an external company we’re working with, I see a field set-up as varchar(20000), holding chunks of xml longer than 255 characters. Why does this work? Is 20000 a valid value?

A bit of googling has revealed that in mysql varchar has a limit of 65,535 bytes, and I see varchar(65535) in use, so how does the 255 limit relate to this?

+4  A: 

It is valid since version 5.0.3, when they changed the maximum length from 255 to 65535.

CHAR has always been 255 max.

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

Wouter van Nifterick
+2  A: 

If you are expecting too big inputs, its a better idea to use TEXT, MEDIUMTEXT or LONGTEXT instead.

Havenard
I agree. It's what I've always read and what I do. But alas, this database isn't mine, I'm just trying to make sense of what I'm seeing. But thanks for backing up my thinking!
Zoe
+9  A: 

Note the MySQL Versions.

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.

What version of MySQL do I have?

Run the following query...

select version() as myVersion
Jonathan Sampson
Ah! So it was just a change with the versions, simple as that (what a relief). Thanks for your help.
Zoe
You're welcome Delilah. Keep up the good work! Glad to see people feeding their knowledge with questions like this.
Jonathan Sampson
FWIW, MySQL accepts `VARCHAR(65536)` too, but it implicitly promotes the column to `MEDIUMTEXT`.
Bill Karwin
A: 

The MySQL manual states

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.

So it depends on the version you're using.

pavium
+1  A: 

Though the index can not be built on varchar(2000)

shantanuo