user455318
user455318

Reputation: 3346

mysql -field for text

I need a field in my table to store some text. 300 characters is the limit for the text.

What is the best choice the option: varchar (300) or text?

Upvotes: 0

Views: 124

Answers (3)

Ted Hopp
Ted Hopp

Reputation: 234795

VARCHAR and TEXT columns are very similar. The manual says:

In most respects, you can regard a BLOB column as a VARBINARY column that can be as large as you like. Similarly, you can regard a TEXT column as a VARCHAR column.

Here are a few differences:

  1. TEXT columns are blank-padded at the end for indexing purposes
  2. TEXT columns cannot have default values
  3. VARCHAR columns have only 1 byte of overhead if the number of bytes needed for the text is less than 256. (The number of bytes needed for a particular text depends on the encoding.) TEXT columns always have 2 bytes of overhead.
  4. All text in VARCHAR columns counts against the row limit of 65,535 bytes. The text of TEXT columns is stored separately and does not count against this limit.

Upvotes: 1

OMG Ponies
OMG Ponies

Reputation: 332541

Use VARCHAR(300) - if the spec calls for a limit of 300 characters, you don't want to support going over that. I'll have to dig up my answer, but someone reported a substantial improvement by migrating their TEXT columns to VARCHAR.

Upvotes: 1

Jon Egeland
Jon Egeland

Reputation: 12613

if you have any reason to be concerned with memory, varchar will only allocate the used memory. I'm not sure how text does it, but I would use varchar (300)

Upvotes: 1

Related Questions